1、定制报告
- Feature:标注主要功能模块
- Story:标注 Features 功能模块下的分支功能
- Severity:标注测试用例的重要级别
- Step:标注测试用例的重要步骤
2、Features 定制详解,标注主要功能模块
import os import allure import pytest @allure.feature("login_module") # 通过 feature 标注用例模块 class Test_case(): def test_case_01(self): assert 0 == 1 def test_case_02(self): assert 0 == 0 if __name__ == '__main__': pytest.main(["-s", "./test_demo2.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
import os import allure import pytest @allure.feature('login_module') # 通过 feature 标注用例模块 def test_case_01(self): assert 0 == 1 @allure.feature('login_module') # 通过 feature 标注用例模块 def test_case_02(self): assert 0 == 0 if __name__ == '__main__': pytest.main(["-s","./test_demo2.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
3、Story定制详解,标注 Features 功能模块下的分支功能
import os import allure import pytest @allure.feature("login_module") class Test_case(): @allure.story("login_case01") # 通过 story 标注用例名称、子功能 def test_case_01(self): assert 0 == 1 @allure.story("login_case02") def test_case_02(self): assert 0 == 0 if __name__ == '__main__': pytest.main(["-s", "./test_demo2.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
4、用例描述定制详解
- 在用例中通过注释来定义用例描述
import os import allure import pytest @allure.feature("login_module") class Test_case(): @allure.story("login_case01") def test_case_01(self): """ 这是一条cms登录接口正向用例 """ assert 0 == 1 @allure.story("login_case02") def test_case_02(self): """ 这是一条cms登录接口反向用例 """ assert 0 == 0 if __name__ == '__main__': pytest.main(["-s", "./test_demo2.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
5、Step定制详解,标注测试用例的重要步骤
- 标注测试用例重要步骤,在报告中会体现该步骤传入的参数明细
import os import allure import pytest import requests @allure.step("post接口请求") # 标注测试用例重要步骤,在报告中会体现该步骤传入的参数明细 def post_request(cms_url, cms_header, cms_param): res = requests.post(url=cms_url, headers=cms_header, data=cms_param) return res @allure.feature("login_module") @allure.story("login_case01") def test_case_01(): """ 这是一条正向用例 """ cms_url = "http://cms.duoceshi.cn/xxx/manager/login" cms_header = {"Content-Type": "application/x-www-form-urlencoded"} cms_param = {"userAccount": "admin","loginPwd": "123456"} res = post_request(cms_url, cms_header, cms_param) assert res.json()["msg"] == "登录成功!" if __name__ == '__main__': pytest.main(["-s","./test_demo4.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
6、为测试用例添加标题 title 和说明 description
import os import allure import pytest import requests @allure.step("post接口请求") def post_request(cms_url, cms_header, cms_param): res = requests.post(url=cms_url, headers=cms_header, data=cms_param) return res @allure.feature("login_module") @allure.story("login_case01") @allure.title("cms登录接口") # 标注测试用例 title @allure.description("这是通过装饰器添加的详细说明") # 标注详细说明 def test_case_01(): """ 这是一条正向用例 """ cms_url = "http://cms.duoceshi.cn/xxx/manager/login" cms_header = {"Content-Type": "application/x-www-form-urlencoded"} cms_param = {"userAccount": "admin","loginPwd": "123456"} res = post_request(cms_url, cms_header, cms_param) assert res.json()["msg"] == "登录成功!" if __name__ == '__main__': pytest.main(["-s","./test_demo4.py", "--alluredir=./result", "--clean-alluredir"]) os.system("allure generate ./result -c -o ./report")
#_data:2022/4/3 #_author:多测师_Evan import os import allure import pytest import requests @allure.step("post接口请求") def post_request(cms_url, cms_header, cms_param): res = requests.post(url=cms_url, headers=cms_header, data=cms_param) return res @allure.feature("login_module") @allure.story("login_case01") @allure.description("这是通过装饰器添加的详细说明") def test_case_01(): allure.dynamic.title("这是用例标题") # 定义用例 title allure.dynamic.description("这是自定义的用例描述") # 定义用例详情描述 cms_url = "http://cms.duoceshi.cn/xxx/manager/login" cms_header = {"Content-Type": "application/x-www-form-urlencoded"} cms_param = {"userAccount": "admin","loginPwd": "123456"} res = post_request(cms_url, cms_header, cms_param) assert res.json()["msg"] == "登录成功!" if __name__ == '__main__': pytest.main(["-s", "./demo.py", "--alluredir=../result", "--clean-alluredir"]) os.system("allure generate ../result --clean -o ../report")
7、attach 添加附件
- allure.attach(body,name,attachment_type,extension)
- body - 要写入文件的原始内容。
- name - 包含文件名的字符串
- attachment_type - 其中一个 allure.attachment_type 值
- extension - 提供的将用作创建文件的扩展名
- allure.attach.file(source,name,attachment_type,extension)
- source - 包含文件路径的字符串。
import allure import pytest @allure.feature('这里是一级标签') class TestAllure(): @allure.title("用例标题0") @allure.story("这里是第一个二级标签") @pytest.mark.parametrize('param', ['青铜', '白银', '黄金']) def test_0(self, param): allure.attach('附件内容是: '+param, '我是附件名', allure.attachment_type.TEXT) @allure.title("用例标题1") @allure.story("这里是第二个二级标签") def test_1(self): allure.attach.file(r'E:\Myproject\pytest-allure\test\test_1.jpg', '我是附件截图的名字', attachment_type=allure.attachment_type.JPG) @allure.title("用例标题2") @allure.story("这里是第三个二级标签") @allure.severity(allure.severity_level.NORMAL) def test_2(self): pass