前面https://mp.csdn.net/postedit/100534776 这篇博文讲了一点使用pytest+allure生成实际测试报告的例子。
接下来记录下一些allure报告中其他的功能:
1、Allure报告中的链接
#链接测试
@allure.link('https://docs.pytest.org/en/latest',name='pytest帮助文档')
@allure.issue('http://baidu.com', name='点击我跳转百度')
@allure.testcase('http://bug.com/user-login-Lw==.html', name='点击我跳转禅道')
def test_other():
'''测试链接,测试链接 ,测试链接'''
pass
生成报告后:
点击链接可以跳转成功,一般在测试报告中可以用在跳转 项目地址,客户端地址 什么的。
2、Allure报告中的注释
@allure.description('这是一个测试case')
def test_other():
'''测试链接,测试链接 ,测试链接'''
pass
生成的报告:
结论:注释功能,可用在你需要详细描述用例的地方。 如果你case里有‘’‘×××’‘’ 这个的注释,那么会被覆盖。
3、Allure报告中的参数
@pytest.mark.parametrize() 这个装饰器可以用来 在同一测试用例种同时传入不同的参数,实现编写一条用例,跑多条case的目的。
(1)
list1 = ['full', 'lisa']
# @allure.description('这是一个测试case')
@pytest.mark.parametrize('param1', list1)
def test_other(param1):
print('param1 = ' + param1)
pass
或:
@pytest.mark.parametrize('param1', ['full', 'lisa'])
def test_other(param1):
print('param1 = ' + param1)
pass
结果:
(2)同时传多个参数,生成多条用例
eg1:
@pytest.mark.parametrize('param1 ,param2', [('full','1'), ('lisa','2')])
def test_other(param1,param2):
allure.attach('param1 ={},param2 ={}'.format(param1,param2),'param')
pass
结果:
eg2:
@pytest.mark.parametrize('a,b,expected', [(2,1,1),(10,5,4),(0,0,0)])
def test_v0(a, b, expected):
diff = a - b
assert diff == expected
提供了几组数据那么就会生成几组用例。
eg3:
从上面的例子可以看出:红框中的数据,是根据测试数据中得出的,其实是个字符串,那么如果想要修改这个字符串要怎么办?
@pytest.mark.parametrize('a,b,c', [(1,2,3),(3,2,1)],ids=['test1,2,3','test3,2,1'])
def test_v0(a, b, c):
allure.attach('a = {},b = {},c = {}'.format(a,b,c),'依次a->b->c')
assert 0 == 0
生成的报告:
(3)分别提供每个参数的值,自动进行参数组合
@pytest.mark.parametrize('a', [1,2])
@pytest.mark.parametrize('b', [0,1])
@pytest.mark.parametrize('c', [3])
def test_v0(a, b, c):
allure.attach('a = {},b = {},c = {}'.format(a,b,c),'依次a->b->c')
assert 0 == 0
可以看到,自动将a,b,c3个参数进行了组合,生成了4条测试用例。
(。・∀・)ノ゙
这里勉强学习了下pytest的一点功能,后续还是想去继续了解下pytest的其他强大功能~
可能遇到的问题:
1、当在使用pytest执行case的时候,可能会报这样的警告:
其实这个case是通过的,那我们大可以直接忽略掉:在pytest命令 后面加个参数即可:
--disable-warnings ,表示忽略掉warning。
if __name__ == '__main__':
pytest.main(['-s','-q','test_login01.py','--alluredir','./report','--disable-warnings'])
可能导致这种情况的原因:
可能在pytest里invoke 了ipython 或 jupyter ,python.main 在pytest初始化的时候也会引入pytest 某些特殊模块,当你重新运行python.main 的时候,这时候就会抛出警告。