参数化使用
@pytest.mark.parametrize(argnames, argvalues) - argnames:要参数化的变量,string(逗号分隔), list, tuple
- argvalues:参数化的值,list, list(tuple)
参数使用string方式传递
代码:
import pytest
@pytest.mark.parametrize("a, b", [(1, 2), (30, 89)])
def test_login(a, b):
print(f"a={a}, b={b}")
if __name__ == '__main__':
pytest.main(["-v"])
运行结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/a5c39584b9a74bfdbe0bc744d6693b3b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZmxvcmFjaHk=,size_20,color_FFFFFF,t_70,g_se,x_16)
参数使用list方式传递
代码:
import pytest
@pytest.mark.parametrize(["a", "b"], [(1, 2), (30, 89)])
def test_login(a, b):
print(f"a={a}, b={b}")
if __name__ == '__main__':
pytest.main(["-v"])
运行结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/a5c39584b9a74bfdbe0bc744d6693b3b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZmxvcmFjaHk=,size_20,color_FFFFFF,t_70,g_se,x_16)
参数使用tuple方式传递
代码:
import pytest
@pytest.mark.parametrize(("a", "b"), [(1, 2), (30, 89)])
def test_login(a, b):
print(f"a={a}, b={b}")
if __name__ == '__main__':
pytest.main(["-v"])
运行结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/a5c39584b9a74bfdbe0bc744d6693b3b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZmxvcmFjaHk=,size_20,color_FFFFFF,t_70,g_se,x_16)
yaml数据参数化
yaml用法:
- 10
- 20
- 30
name: flora
age: 28
sex: female
-
name: flora
age: 28
sex: female
-
name: jane
age: 30
sex: female
示例:
data.yaml文件如下:
-
- 10
- 20
-
- 11
- 22
test_main.py代码如下:
import pytest
import yaml
class TestDemo:
@pytest.mark.parametrize(("a", "b"), yaml.safe_load(open("./data.yaml")))
def test_login(self, a, b):
print(f"a={a}, b={b}")
if __name__ == '__main__':
pytest.main(["-v"])
运行结果如下: ![在这里插入图片描述](https://img-blog.csdnimg.cn/9ccbfce73b6a4e498acb11a3ecfb7844.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZmxvcmFjaHk=,size_20,color_FFFFFF,t_70,g_se,x_16)
|