前言
pytest的参数化(parametrize)能够实现只需维护测试数据,就能生成不一样的测试用例目的。能够在参数化的时候加 ids 参数对每一个用例说明使用场景。
最终咱们但愿在 allure 报告上能详细的展现出每一个用例的标题描述,这样才能更直观的知道每一个用例是干什么的。
函数
参数化parametrize
先看一个简单的pytest参数化案例演示test_a.py测试
# test_a.py import pytest import allure # 做者:上海-悠悠 QQ交流群:779429633 def login(username, password): '''登陆''' print("输入帐号:%s" % username) print("输入密码:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 测试数据 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure.story("登陆用例") @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
cmd命令行运行用例优化
> pytest --alluredir ./report test_a.py > allure serve ./report
生成报告spa
这样生成的报告在用例列表里面并不能很友好的展现出每一个用例的执行场景,只知道哪一个用例报错了。
因而须要对每一个用例加上描述,加一个 ids 参数
命令行
ids 参数使用
在上面用例部分代码里面加个 ids 参数,用于描述每一个用例的运行场景。3d
# 做者:上海-悠悠 QQ交流群:779429633 @allure.story("登陆用例") @pytest.mark.parametrize("test_input,expected", test_datas, ids=[ "输入正确帐号,密码,登陆成功", "输入错误帐号,密码,登陆失败", "输入正确帐号,密码,登陆成功", ] ) def test_login(test_input, expected): '''测试登陆用例''' # 获取函数返回结果 result = login(test_input["username"], test_input["password"]) # 断言 assert result["msg"] == expected
cmd命令行运行用例code
> pytest --alluredir ./report test_a.py > allure serve ./report
生成报告
orm
allure.title描述用例
上面是经过在 parametrize 里面添加 ids 参数解决,接下来再讲一个用 allure.title("用例描述") 添加用例描述的方式解决。
使用 @allure.title("用例描述") 时,能够加上传入的参数,如传入的参数 "test_input,expected"
,需拼接test_input参数的值,能够这样写
blog
@allure.title("用例描述,测试输入:{test_input}")input
在 allure_pytest/utils.py 源码里面能够找到对应的代码
# allure_pytest/utils.py def allure_name(item, parameters): name = escape_name(item.name) title = allure_title(item) return title.format(**parameters) if title else name
当没有加allure.title()时候,用例的描述就是 item.name 值(也就是上面的 ids 用例的名称),
若是加了allure.title(),那么用例的描述就是添加的title值,这两个地方取其中的一个。
import pytest import allure # 做者:上海-悠悠 QQ交流群:779429633 def login(username, password): '''登陆''' print("输入帐号:%s" % username) print("输入密码:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 测试数据 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure.story("登陆用例") @allure.title("用例描述,测试输入:{test_input}") @pytest.mark.parametrize("test_input,expected", test_datas, ids=[ "输入正确帐号,密码,登陆成功", "输入错误帐号,密码,登陆失败", "输入正确帐号,密码,登陆成功", ] ) def test_login(test_input, expected): '''测试登陆用例''' # 获取函数返回结果 result = login(test_input["username"], test_input["password"]) # 断言 assert result["msg"] == expected
cmd命令行运行用例
> pytest --alluredir ./report test_a.py > allure serve ./report
生成报告
优化用例title
结合上面两种实现方式,把用例描述当成一个测试输入的参数,继续优化后以下
需注意的是 parametrize 里面三个参数 test_input,expected,title
跟 test_login(test_input, expected, title) 里面三个参数保持一致
import pytest import allure # 做者:上海-悠悠 QQ交流群:779429633 def login(username, password): '''登陆''' print("输入帐号:%s" % username) print("输入密码:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 测试数据 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!", "输入正确帐号,密码,登陆成功"), ({"username": "yoyo2", "password": "123456"}, "failed!", "输入错误帐号,密码,登陆失败"), ({"username": "yoyo3", "password": "123456"}, "success!", "输入正确帐号,密码,登陆成功"), ] @allure.story("登陆用例") @allure.title("{title}") @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