【Pytest】allure

安装

前提:java环境配置完成。可以参考:【java】安装及配置_G.O的博客-CSDN博客

1.下载解压allure工具:Releases · allure-framework/allure2 · GitHub

2.添加bin目录到环境变量中,使用allure --version验证是否安装成功

3.安装allure-pytest插件:pip install allure-pytest

allure注解说明

举个栗子

import allure


class Calculator:
    def add(self, a, b):
        return a + b


@allure.epic("计算器模块-epic")
@allure.feature("加法模块-feature")
class TestAdd:
    @allure.story("加法有效用例-story")
    @allure.title("加法有效用例-title")
    @allure.severity(allure.severity_level.BLOCKER)
    @allure.description("这是加法有效测试用例的测试")
    @allure.link("https://www.baidu.com/")
    def test_add_normal(self):
        with allure.step("步骤一:实例化calc"):
            calc = Calculator()
        with allure.step("步骤二:计算两数之和"):
            result = calc.add(1, 2)
            allure.attach("E:\1.png",
                          name="截图",
                          attachment_type = allure.attachment_type.PNG,
                          extension=".png")
        with allure.step("步骤三:断言"):
            assert result == 3

    @allure.story("加法错误用例-story")
    def test_add_error(self):
        calc = Calculator()
        result = calc.add(1, 2)
        assert result == 4

    def test_add_other(self):
        pass


@allure.epic("这是单独的epic")
@allure.feature("这是单独的feature")
@allure.story("这是单独的story")
def test_1():
    pass


def test_2():
    pass


@allure.epic("计算器模块-epic")
@allure.feature("减法模块-feature")
class TestSub:
    @allure.story("减法有效用例-story")
    def test_sub_normal(self):
        pass

 

allure常用命令 

生成报告:--alluredir=报告目录名(相对路径或绝对路径)

allure的原始数据,测试中查看的临时报告,文件常为xml/json/txt/attach等格式。

pytest test.py --alluredir=./results

常用参数:-s 输出打印,-q 简化打印信息 

pytest test.py -s -q --alluredir=./results

清除之前报告记录:--clean-alluredir

多次执行测试用例生成报告时,会保存每一次的执行记录。当需要清空之前的记录,只保留新的报告时,需要添加--clean-alluredir命令。

pytest test.py --alluredir=./results --clean-alluredir

生成在线版本报告:allure serve 报告目录名

生成在线版本报告,自动开启浏览器报告服务,打开生成的报告

allure serve ./results

生成最终版本报告:allure generate 报告目录名

在原始数据基础上,本地生成的最终报告,默认文件名为allure-report,带js、css等文件的html格式的报告

allure generate ./results

常用参数:-o 指定目录名称,-c 覆盖路径

allure generate ./results -o ./report -c

打开html报告:allure open

本地也可以直接使用浏览器打开查看。如果需要给其他人观看,需要 allure open 开启一个web服务供其他主机查看。

allure open

猜你喜欢

转载自blog.csdn.net/Yocczy/article/details/130265480