参数化parametrize
parametrizing
1.使用pytest.mark.parametrize装饰器实现测试用例参数化
import pytest
@pytest.mark.parametrize("test_input,expected",
[('3+5', 8),
('1+3', 4),
('6*4', 30), ])
def test_eval(test_input, expected):
assert eval(test_input) == expected
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
2.可以标记单个测试实例在参数话,如使用内置的mark.xfail
import pytest
@pytest.mark.parametrize("test_input,expected",[
('3+5',8),
('1+4',5),
pytest.param('4*6', 4, marks=pytest.mark.xfail),
])
def test_eval(test_input, expected):
print("---------开始用例---------")
assert eval(test_input) ==expected
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
参数组合
如果想获得多个参数化参数的所有组合,可以堆叠参数化装饰器
# 参数组合
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
print("测试数据组合:x->%s,y->%s" % (x, y))
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])