参数化使用
@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"])
运行结果:
参数使用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"])
运行结果:
参数使用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"])
运行结果:
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"])
运行结果如下:
|