Pytest 学习(二十五)- 解决pytest参数化测试标题都一样问题
一、前言
使用参数化测试化后,allure的报告如下显示:
源代码如下:
# -*- coding: utf-8 -*-
# @Time : 2020/12/13 17:27
# @Author : longrong.lang
# @FileName: test_param_title.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import allure
import pytest
@pytest.fixture()
def loginTo(request):
# 传入参数,登录
param = request.param
print(f"\nuserName : {param['userName']} ,passWord : {param['passWord']}")
# 登录成功,返回
return {"code": "0", "msg": "success!"}
data = [{"userName": "jojo", "passWord": "1"},
{"userName": "rongrong", "passWord": "1"},
{"userName": "lilei", "passWord": "1"}
]
@allure.story("讲述登录成功,返回成功的故事!")
@allure.title("测试登录接口")
@pytest.mark.parametrize("loginTo", data, indirect=True)
def test_loginTo(loginTo):
assert loginTo['msg'] == 'success!'
从代码看出,其实就是@allure.title("测试登录接口"),写死的呀。
二、如何解决
那么怎么搞?
这个问题问的好呀,动态的标题可以实现吗?必须能呀。
方案一
直接拼接loginTo
示例代码如下:
# -*- coding: utf-8 -*-
# @Time : 2020/12/13 17:27
# @Author : longrong.lang
# @FileName: test_param_title.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import allure
import pytest
@pytest.fixture()
def loginTo(request):
# 传入参数,登录
param = request.param
print(f"\nuserName : {param['userName']} ,passWord : {param['passWord']}")
# 登录成功,返回
return {"code": "0", "msg": "success!"}
data = [{"userName": "jojo", "passWord": "1"},
{"userName": "rongrong", "passWord": "1"},
{"userName": "lilei", "passWord": "1"}
]
@allure.story("讲述登录成功,返回成功的故事!")
@allure.title("测试登录接口 {loginTo}")
@pytest.mark.parametrize("loginTo", data, indirect=True)
def test_loginTo(loginTo):
assert loginTo['msg'] == 'success!'
allure报告
这样会使得报告,更具可读性,直观一些
方案二
使用元祖来做参数化,加一个参数为标题
示例代码如下:
# -*- coding: utf-8 -*-
# @Time : 2020/12/13 17:27
# @Author : longrong.lang
# @FileName: test_param_title.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import allure
import pytest
data = [("jojo", "1", "登录成功的用例"),
("rongrong", "1", "登录成功的用例"),
("lilei", "1", "登录失败的用例")
]
@allure.story("讲述登录成功,返回成功的故事!")
@allure.title("测试登录接口 {title}")
@pytest.mark.parametrize("userName,password,title", data)
def test_loginTo(userName, password, title):
print(userName + "\t" + password + "\t" + title)
allure报告
三、这种做法的优点
- 可以自定义各式各样的标题
- 单独一个值去维护标题值
- 可读性比较好,容易维护
优秀不够,你是否无可替代
软件测试交流QQ群:721256703,期待你的加入!!
欢迎关注我的微信公众号:软件测试君
![](https://www.cnblogs.com/images/cnblogs_com/longronglang/1061549/o_QQ%E6%88%AA%E5%9B%BE20190728134401.jpg)