Allure简介
Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架。它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。下面就Pytest如何与Allure集成做详细介绍。
Pytest框架集成Allure
Pytest是Python的单元测试框架,非常方便和易用。强烈推荐对于用Python进行测试工作的小伙伴使用这个测试框架,相比与Python自带的UnitTest好用太多太多。今天我们主要是介绍如何将测试报告生成工具Allure集成到Pytest中。目前现在已经有allure2了,我们要使用的就是这个allure2
之前我们写了Pytest结合Allure生成测试报告的环境搭建: 【Pytest篇】Allure生成漂亮的HTML图形化测试报告(一))
今天我们来写一下测试报告的定制详解:
一、Features、Story定制详解
@allure.feature # 用于定义被测试的功能,被测产品的需求点,模块
@allure.story # 用于定义被测功能的用户场景,即子功能点,用例
import pytest,os
import allure
class Test(object):
@allure.feature('登录功能')
@allure.story('登录成功')
def test_login(self):
assert 1 == 1
def test2(self):
assert 1==1
if __name__=="__main__":
#生成测试报告json
pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
#将测试报告转为html格式
split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
os.system(split)
print(split)
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
..
2 passed in 0.06s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean
Process finished with exit code 0
二、title用例标题和description用例描述定制详解
@allure.title(用例的标题)
@allure.description(用例的描述)
或用例描述也可写成这样
"""
这里是登录成功测试用例
"""import pytest,osimport allureclass Test(object): @allure.feature('登录功能') @allure.story('登录成功') @allure.title('用例的标题')#用例的标题 @allure.severity('blocker') @allure.issue('https://www.baidu.com/')#添加权限对应链接 @allure.testcase('https://www.baidu.com/')#添加用例对应链接 def test_login(self): """ 这里是登录成功测试用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成测试报告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #将测试报告转为html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.09sReport successfully generated to .\report\htmlallure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0
三、Severity定制标记用例级别详解根据测试用例的重要性划分测试用例等级,如果没指定等级,默认为normal级别
Allure中对严重级别的定义:
1、 Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)@allure.severity('blocker')
2、 Critical级别:临界缺陷( 功能点缺失)
@allure.severity('critical')
3、 Normal级别:普通缺陷(数值计算错误)
@allure.severity('normal')
4、 Minor级别:次要缺陷(界面错误与UI需求不符)
@allure.severity('minor')
5、 Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)
@allure.severity('trivial')
import pytest,osimport allureclass Test(object): @allure.feature('登录功能') @allure.story('登录成功') @allure.severity('blocker') def test_login(self): """ 这里是登录成功测试用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成测试报告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #将测试报告转为html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.10sReport successfully generated to .\report\htmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0四、Step和attach定制详解allure.step("调用登录"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中allure.attach('账号', '18221124104') # attach可以打印一些附加信息import pytest,osimport allure @allure.feature('购物车功能') # feature定义功能class Test(object): @allure.story('加入购物车') # story定义用户场景 def test_add_shopping_trolley(self): login('橙子', '登录密码') # 调用“步骤函数” with allure.step("浏览商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤2 allure.attach('商品1', 'NIKE球鞋') # attach可以打印一些附加信息 allure.attach('商品2', '大众速腾') with allure.step("点击商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤3 pass with allure.step("校验结果"): allure.attach('期望结果', '添加购物车成功') allure.attach('实际结果', '添加购物车失败') assert 'success' == 'failed' @allure.story('修改购物车') def test_edit_shopping_trolley(self): pass @pytest.mark.skipif(reason='本次不执行') @allure.story('删除购物车') def test_delete_shopping_trolley(self): pass @allure.step('账号登录') # 还可以将一个函数作为一个步骤,调用此函数时,报告中输出一个步骤,步骤名字通常是函数名,我把这样的函数叫“步骤函数”def login(user, pwd): print(user, pwd)if __name__=="__main__": #生成测试报告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #将测试报告转为html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py橙子 登录密码F.s================================== FAILURES ===================================_______________________ Test.test_add_shopping_trolley ________________________ self = <test.test01.Test object at 0x0000024F3425C898> @allure.story('加入购物车') # story定义用户场景 def test_add_shopping_trolley(self): login('橙子', '登录密码') # 调用“步骤函数” with allure.step("浏览商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤2 allure.attach('商品1', 'NIKE球鞋') # attach可以打印一些附加信息 allure.attach('商品2', '大众速腾') with allure.step("点击商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤3 pass with allure.step("校验结果"): allure.attach('期望结果', '添加购物车成功') allure.attach('实际结果', '添加购物车失败')> assert 'success' == 'failed'E AssertionError test01.py:51: AssertionError1 failed, 1 passed, 1 skipped in 0.18sReport successfully generated to .\report\htmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0五、Issue缺陷链接和TestCase用例链接定制详解@allure.issue() 缺陷 对应缺陷管理系统里面的链接,在测试报告中可以点击跳转的
@allure.testcase() 测试用例的链接地址 对应功能测试用例系统里面的case链接,在测试报告中可以点击跳转的
import pytest,osimport allureclass Test(object): @allure.feature('登录功能') @allure.story('登录成功') @allure.severity('blocker') @allure.issue('https://www.baidu.com/')#添加缺陷对应链接 @allure.testcase('https://www.baidu.com/')#添加用例对应链接 def test_login(self): """ 这里是登录成功测试用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成测试报告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #将测试报告转为html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.05sReport successfully generated to .\report\htmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0五、link链接定制详解@allure.link(‘https://www.baidu.com/’)六、attachment附件制定@allure.attachment()