• Allure进阶-动态生成报告内容



    1.参数化结合allure.title()生成不同标题报告

    1.参数化parametrize

    import os
    
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    
    @pytest.mark.parametrize("test_input,expected",test_datas)
    def test_login(test_input,expected):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_01.py','--alluredir','./result','--clean-alluredir'])
        # 基于json数据生成web报告工程
        os.system('allure generate ./result -o ./report_allure/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    2.param+ids参数

    import os
    
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    
    @pytest.mark.parametrize("test_input,expected",test_datas,
                             ids=["输入正确的账号A,密码,登录",
                                  "输入错误的账号B,密码,登录",
                                  "输入正确的账号A,密码,登录"
                                  ]
                             )
    def test_login(test_input,expected):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_ids_02.py','--alluredir','./result2','--clean-alluredir'])
        # 基于json数据生成web报告工程
        os.system('allure generate ./result2 -o ./report_allure2/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    3.allure.title描述用例

    import os
    
    import allure
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    @allure.title("用例描述,测试输入:{test_input}")
    @pytest.mark.parametrize("test_input,expected",test_datas)
    def test_login(test_input,expected):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_title_03.py','--alluredir','./result3','--clean-alluredir'])
        # 基于json数据生成web报告工程
        os.system('allure generate ./result3 -o ./report_allure3/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    4.优化用例title

    import os
    
    import allure
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!","输入正确的账号,密码登录"),
        ({"username":"xz","password":"222222"},"failed!","输入错误的账号,密码登录"),
        ({"username":"qs","password":"333333"},"success!","输入正确的账号,密码登录")
    ]
    
    @allure.title("{title}{test_input}")
    @pytest.mark.parametrize("test_input,expected,title",test_datas)
    def test_login(test_input,expected,title):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_title_new_04.py','--alluredir','./result4','--clean-alluredir'])
        # 基于json数据生成web报告工程
        os.system('allure generate ./result4 -o ./report_allure4/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    2.allure报告清空上一次运行的记录

    import os
    
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    #
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     # 获取函数返回结果
    #     result = login(test_input["username"],test_input["password"])
    #     # 断言
    #     assert result["msg"] == expected
    
    
    def test_case02():
        assert 1 == 1
    
    
    
    if __name__ == '__main__':
        # 1.allure报告可以记录用例每次执行的情况,方便跟踪用例的成功率,数据保留在json文件中
        # 2.会带来一个问题,当你代码里的用例删除或者更换名称后,依然会记录之前的用例报告
        # 3. --clean-alluredir 每次用例执行之前先清空allure的报告记录
        pytest.main(['test_a_05.py','--alluredir','./result','--clean-alluredir'])
        # 基于json数据生成web报告工程
        # 4.这里的clean只是让报告可重新生成,生成的结果,会保留之前的用例执行记录
        os.system('allure generate ./result -o ./report_allure/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    3.allure动态生成用例标题

    import os
    
    import allure
    
    
    # 接口用例描述例子
    import pytest
    
    desc = "请求URL:{}
    "
    \ "请求类型L:{}
    "
    \ "期望结果:{}
    "
    \ "实际结果:{}
    "
    \ .format("http://www.baidu.com","post","200","404") @allure.description("简单描述") def test_dynamic_desc(): # 断言成功之后,变更描述信息 assert 1 == 1 allure.dynamic.description(desc) @allure.title("原始标题") def test_dynamic_title(): assert 2 + 2 == 4 allure.dynamic.title("当断言成功时,用例标题会动态更新") @allure.title("参数化用例标题:添加{param1}和{param2}") @pytest.mark.parametrize('param1,param2,expected',[(2,2,4),(1,2,5)]) def test_with_param_title(param1,param2,expected): assert param1 + param2 == expected allure.dynamic.title("变更标题") if __name__ == '__main__': pytest.main(['-s','test_case_06.py','--alluredir','./result','--clean-alluredir']) os.system('allure generate ./result -o ./report_allure/ --clean')
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    4.allure生成环境配置

    #environment.properties
    在result的目录下添加一个environment.properties文件
    
    systemVersion=win10
    pythonVersion=3.6.0
    allureVersion=2.13.0
    baseUrl=http://192.168.1.x:8080
    projectName=test
    author=zz
    email=123123123@qq.com
    freind=Mr.ma
    idl=wangjie
    
    #environment.xml
    在allure的result目录下添加一个environment.xml
    <environment>
        <parameter>
            <key>Browser</key>
            <value>Chrome</value>
        </parameter>
        <parameter>
            <key>Browser.Version</key>
            <value>63.0</value>
        </parameter>
        <parameter>
            <key>Stand</key>
            <value>Production</value>
        </parameter>
    </environment>
    
    
    import os
    
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    
    @pytest.mark.parametrize("test_input,expected",test_datas)
    def test_login(test_input,expected):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_07.py','--alluredir','./result'])
        # 基于json数据生成web报告工程
        os.system('allure generate ./result -o ./report_allure/ --clean')
    
    import os
    
    import pytest
    
    # # 测试数据
    # test_datas = [
    #     #"username":"zz1","password":"123456" 是用例的输入数据
    #     # "success!" 是预期结果
    #     ({"username":"zz","password":"111111"},"success!"),
    #     ({"username":"xz","password":"222222"},"failed!"),
    #     ({"username":"qs","password":"333333"},"success!")
    # ]
    
    
    
    # pytest用例函数
    # @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
    # def test_login(a,b):
    #     """测试登录用例"""
    #     print("登录")
    #     print(a)
    #     print(b)
    
    # @pytest.mark.parametrize("test_input,expected",test_datas)
    # def test_login(test_input,expected):
    #     """测试登录用例"""
    #     print("登录")
    #     # 获取输入数据
    #     print("获取输入数据")
    #     print(test_input["username"])
    #     print(test_input["password"])
    #     # 获取预期结果
    #     print("获取预期结果")
    #     print(expected)
    
    
    
    
    
    
    # 模拟定义登录接口
    def login(username,password):
        """登录"""
        print("输入账户: %s"% username)
        print("输入密码: %s"% password)
        # 返回
        return {"code":0,"msg":"success!"}
    
    
    # # 测试数据
    test_datas = [
        #"username":"zz1","password":"123456" 是用例的输入数据
        # "success!" 是预期结果
        ({"username":"zz","password":"111111"},"success!"),
        ({"username":"xz","password":"222222"},"failed!"),
        ({"username":"qs","password":"333333"},"success!")
    ]
    
    
    @pytest.mark.parametrize("test_input,expected",test_datas)
    def test_login(test_input,expected):
        """测试登录用例"""
        # 获取函数返回结果
        result = login(test_input["username"],test_input["password"])
        # 断言
        assert result["msg"] == expected
    
    
    
    if __name__ == '__main__':
        # 生成报告数据源到result目录,存储json数据
        pytest.main(['test_a_08.py','--alluredir','./result','--clean-alluredir'])
        # 基于json数据生成web报告工程
        os.system('copy environment.properties result\\environment.properties')
        os.system('allure generate ./result -o ./report_allure/ --clean')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181

    5.Allure报告添加失败截图

    conftest.py
    
    import allure
    import pytest
    from selenium import webdriver
    
    
    @pytest.fixture(scope = "session")
    def browser():
        # 定义全局变量
        global driver
        # 初始化浏览器对象并启动浏览器
        driver = webdriver.Chrome()
        # 返回浏览器对象给用例
        yield driver
        # 用例的后置处理:关闭浏览器
        driver.quit()
        print("test end!!")
    
    """
    
    装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
        a.可以获取测试用例的信息,比如用例函数的描述
        b.可以获取测试用例的结果,yield,返回一个result对象
    """
    @pytest.hookimpl(hookwrapper=True)
    def pytest_runtest_makereport():
        # 可以获取测试用例的执行结果,yield,返回一个result对象
        out = yield
        """
         从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
         report对象的属性
         包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
         outcome(用例的执行结果:passed,failed)
        """
        report = out.get_result()
        # 仅仅获取用例call阶段的执行结果,不包含setup和teardown
        if report.when == 'call':
            # 获取用例call执行结果为结果为失败的情况
            xfail  = hasattr(report,"wasfail")
            if(report.skipped and xfail) or (report.failed and not xfail):
                # 添加allure报告截图
                with allure.step("添加失败截图。。"):
                    # 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
                    allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
    
    from time import sleep
    
    
    def test_baidu_case01(browser):
        driver = browser
        driver.get("http://www.baidu.com")
        sleep(2)
        # 定位到百度搜索框,然后输入关键字
        driver.find_element('id','kw').send_keys('狗狗币')
        sleep(2)
        # 定位到搜索按钮,点击搜索
        driver.find_element('id','su').click()
        sleep(2)
        assert driver.title == "11狗狗币_百度搜索"
    
    import os
    
    import pytest
    
    
    def run():
        pytest.main(['-v', '--alluredir', './result', '--clean-alluredir'])
        os.system('allure generate ./result -o ./report_allure/ --clean')
    
    
    if __name__ == '__main__':
        run()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    使用 MySQL 日志 - Part 1
    Liunx 实时调度策略 SCHED_RR SCHED_FIFO 区别 适用情况
    pytorch中的hook机制register_forward_hook
    SpringBoot2.0---------------6、SpringBoot开发小技巧
    论区块链应用开发中的技术选型
    文件共享服务器(cifs协议 -- 微软开发)
    Python的PyQt框架的使用-构建环境篇
    国标28181 开源WVP-PRO项目部署
    【追求卓越03】数据结构--链表练习题
    【数据结构】分块查找
  • 原文地址:https://blog.csdn.net/BlackEnn/article/details/126070855