Pytest参数化用例
参数化:通过参数的方式传递数据,从而实现数据与脚本分离,并且可以实现用例的重复生成与执行。
装饰器:@pytest.mark.parametrize
单参数
import pytest
search_list = ["appium","selenium","pytest"]
# 参数化实现测试用例的动态生成
# 第一种:单参数情况,每一条测试数据都会生成一条测试用例
@pytest.mark.parametrize("search_key",["appium","selenium","pytest"," "])
def test_search(search_key):
assert search_key in search_list
多参数+ids参数重命名
import pytest
# 第二种:多参数情况
@pytest.mark.parametrize("username,password",[["right","right"],
["wrong","wrong"],
[" ","right"],
["right"," "]
],
# 用例重命名-添加 ids 参数,将别名放在列表中
# ids=["right username and right password","wrong username and wrong password","username is null","password is null"])
# ids支持中文
ids=["正确的用户名和密码","错误的用户名和密码","用户名为空","密码为空"])
def test_login(username,password):
print(f"登录的用户名:{username}, {password}")
注意:必须在同目录下创建conftest.py文件,将下面内容添加进去,运行脚本后,才可以正常显示中文的重命名。
def pytest_collection_modifyitems(items):
"""
测试用例收集完成时,将收集到的用例名name和用例标识nodeid的中文信息显示在控制台上
"""
for i in items:
i.name=i.name.encode("utf-8").decode("unicode_escape")
i._nodeid=i.nodeid.encode("utf-8").decode("unicode_escape")
笛卡尔积
import pytest
@pytest.mark.parametrize("a",[1,2,3])
@pytest.mark.parametrize("b",["你","好","啊"])
@pytest.mark.parametrize("c",["a","b","c"])
def test_param(a,b,c):
print(f"笛卡尔积形式的参数化中 a={a},b={b},c={c}")
标签:search,right,pytest,mark,Pytest,参数,化用例,password
From: https://www.cnblogs.com/hogwarts/p/18191929