七安 七安
  • 首页
  • 七安
  • Python
  • Tools
  • 导航
  • 9
allure

Allure

七安
2024-01-12 22:22:31
点击查看报告展示

下载地址

  • https://github.com/allure-framework/allure2/releases
  • https://pan.67an.com/allure 有最新版
  • 环境变量bin目录 allure --version

安装allure-pytest模块

pip install allure-pytest

配置项

[pytest]
# allure-results为报告生成地址
# clean-alluredir为清除之前记录
addopts = --alluredir ./allure-results --clean-alluredir

生成报告

  1. allure serve ./report
  2. allure generate report + allure open allure-report
import os

import pytest

if __name__ == '__main__':
    pytest.main()
    os.system("allure generate report -o report")
    os.system("allure open allure-report")

Allure模块方法

使用方法参数值参数说明
@allure.epic()epic描述定义项目、当有多个项目使用。往下是feature
@allure.feature()模块名称用例按照模块区分,有多个模块时给每个起名字
@allure.story()用例名称一个用例的描述
@allure.titlle()用例标题一个用例的标题
@allure.testcase()测试用例的链接地址自动化用例对应的功能用例存放系统的地址
@allure.issue()缺陷地址对应缺陷管理系统里边的缺陷地址
@allure.description()用例描述对测试用例的详细描述
@allure.step()测试步骤测试用例的操作步骤
@allure.severity()用例等级blocker、critical、normal、minor、trivial
@allure.llink()定义链接用于定义一个需要在测试报告中展示的链接
@allure.attachment()附件添加测试报告附件

钩子函数完成失败截图

钩子函数

pytest中的钩子函数按功能一共分为6类:
引导钩子、初始化钩子、用例收集钩子、用例执行钩子、报告钩子、调试钩子

conftest.py
global driver
# 钩子函数,结果  
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    """
    :param item:代表测试函数或方法,包含测试相关的信息,比如名称,位置,标记
    :param call:包含测试函数执行的详细信息,比如结果,执行时间
    :return:
    when = setup:前置
    when = call:执行测试用例
    when = teardown:后置
    """
    # 获取钩子函数的结果
    out = yield
    # 获取测试报告(钩子结果)  
    report = out.get_result()
    # print(f"测试报告:{report}")
    # print(f"步骤:{report.when}")
    # print(f"nodeid:{report.nodeid}")
    # print(f"运行结果:{report.outcome}")
    # 失败测试用例截图
    if report.when == 'call' and report.failed:
        # 截图,get_screenshot_as_png()二进制数据
        # 使用allure.attach将二进制附件到allure报告中
        # save_screenshot()保存本地
allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)

test_allure.py
@allure.epic("七安自动化")
@allure.feature("web自动化登录")
class TestLogin:
    @allure.story("自动登录")
    @allure.title("登陆测试")
    @allure.testcase("http://www.baidu.com", "testcase链接")
    @allure.issue("http://www.baidu.com", "缺陷的链接")
    @allure.description("登录测试用例")
    @allure.link("http://www.baidu.com", name="link链接")
    @allure.severity("blocker")
    @pytest.mark.parametrize("username,password", read_yaml()['userinfo_list'])
    def test_login(self, driver, username, password):
        allure.dynamic.title("登陆功能,账号为:" + username)
        with allure.step("打开登录地址"):
            driver.get("https://***.***.***/***")
        with allure.step("输入用户名"):
            driver.find_element(By.XPATH, '//input[@id="user_login"]').send_keys(username)
        with allure.step("输入密码"):
            driver.find_element(By.XPATH, '//input[@id="user_pass"]').send_keys(password)
        with allure.step("点击登录"):
            driver.find_element(By.XPATH, '//input[@id="wp-submit"]').click()
        if username == 'admin':
            text = driver.find_element(By.XPATH, '//*[@id="login_error"]/p/strong[1]').text
            assert text == '错误'

动态定义

allure.dynamic.title("登陆功能,账号为:" + username)

创建allure环境信息

在report文件夹新建environment.properties

SystemVersion=win11
Browser=Chrome
pythonVersion=3.8.10
author=7an
作者设置了回复可见
立即评论,阅读全文
allure
9
Pytest
上一篇
PO
下一篇

聚合文章

PlayWright
Fluent
Pyside6
Anaconda
logging
PO
Allure

文章目录

猜你喜欢

PlayWright

七安
373 5

Fluent

七安
1,627 1

Pyside6

七安
1,551 2

Anaconda

七安
1,373 4
9

关于

采用知识共享署名 4.0 国际许可协议进行许可。
This work is licensed under a Creative Commons Attribution 4.0 International License.
资源均来自网络整理收集,仅供学习和研究使用,如有侵权请联系我们删除。

七安

网站地图 sitemap

七安

网站地图 sitemap
Copyright © 2018-2025 七安. . 京ICP备18054419号-6 鲁公网安备 37048102005039号
  • 首页
  • 七安
  • Python
  • Tools
  • 导航

搜索

  • thai
  • s3501
  • 百年好合
  • AdSense
  • ITX
  • 蹦蹦炸弹
  • Selenium
  • Pytest
  • allure
  • PageObject

七安

20
文章
0
评论
420
喜欢