pytest.mark.parametrize装饰器可以实现测试用例参数化
parametrizing
1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子
# content of test_expectation.py # coding:utf-8 import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])
2.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器
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_canshu1.py"])