赞
踩
pytest.fixture
fixture可以定义参数化pytest.mark.parametrize
可以让测试函数和类定义多组参数和fixturepytest_generate_tests
可以定义自定义参数化方案或扩展测试数据和期望结果不一样,但是操作步骤都一样的测试用例可以使用参数化。举个例子:
def test_01():
assert 3 + 5 == 9
def test_02():
assert 2 + 4 == 6
def test_03():
assert 2 + 9 == 42
@pytest.mark.parametrize("input, expected"), [["3+5", 9], ["2+4", 6], ["2+9", 42]])
def test_04(input, expected):
assert eval(input) == expected
def parametrize(self, argnames, argvalues, indirect=False, ids=None, scope=None):
"arg1, arg2, arg3"
["arg1", "arg2", "arg3"]
("arg1", "arg2", "arg3")
@pytest.mark.parametrize("name, passwd", [["ZhangSan", "zs123"], ["LiSi", "ls123"], ["WangEr", "we123"]])
@pytest.mark.parametrize(["name", "passwd"], [["ZhangSan", "zs123"], ["LiSi", "ls123"], ["WangEr", "we123"]])
@pytest.mark.parametrize(("name", "passwd"), [["ZhangSan", "zs123"], ["LiSi", "ls123"], ["WangEr", "we123"]])
@pytest.mark.parametrize("username", ["test01", "test02", "test03"])
@pytest.mark.paramatrize("username, passwd", [("ZhangSan", "zs123"), ("LiSi", "ls123"), ("WangEr", "we123")])
data = [ (4, 5, 9), (1, 2, 3), (2, 4, 6) ] @pytest.mark.parametrize("a, b, expect", data) class TestParamatrize: def test_paramatrize_01(self, a, b, expect): print(f"测试函数为: 01, 测试数据为: {a}+{b}, 期望值为: {expect}") assert a + b == expect def test_parametrize_02(self, a, b, expect): print(f"测试函数为: 02, 测试数据为: {a}+{b}, 期望值为: {expect}") assert a + b == expect
============================= test session starts ============================== collecting ... collected 6 items test_01.py::TestParamatrize::test_paramatrize_01[4-5-9] PASSED [ 16%] 测试函数为: 01, 测试数据为: 4+5, 期望值为: 9 test_01.py::TestParamatrize::test_paramatrize_01[1-2-3] PASSED [ 33%] 测试函数为: 01, 测试数据为: 1+2, 期望值为: 3 test_01.py::TestParamatrize::test_paramatrize_01[2-4-6] PASSED [ 50%] 测试函数为: 01, 测试数据为: 2+4, 期望值为: 6 test_01.py::TestParamatrize::test_parametrize_02[4-5-9] PASSED [ 66%] 测试函数为: 02, 测试数据为: 4+5, 期望值为: 9 test_01.py::TestParamatrize::test_parametrize_02[1-2-3] PASSED [ 83%] 测试函数为: 02, 测试数据为: 1+2, 期望值为: 3 test_01.py::TestParamatrize::test_parametrize_02[2-4-6] PASSED [100%] 测试函数为: 02, 测试数据为: 2+4, 期望值为: 6 ============================== 6 passed in 0.08s ===============================
data_1 = [1, 2, 3]
data_2 = ['a', 'b']
@pytest.mark.parametrize("a", data_1)
@pytest.mark.parametrize("b", data_2)
def test_parametrize_01(a, b):
print(f"笛卡尔积,测试数据为: {a}-{b}")
collecting ... collected 6 items test_01.py::test_parametrize_01[a-1] PASSED [ 16%] 笛卡尔积,测试数据为: 1-a test_01.py::test_parametrize_01[a-2] PASSED [ 33%] 笛卡尔积,测试数据为: 2-a test_01.py::test_parametrize_01[a-3] PASSED [ 50%] 笛卡尔积,测试数据为: 3-a test_01.py::test_parametrize_01[b-1] PASSED [ 66%] 笛卡尔积,测试数据为: 1-b test_01.py::test_parametrize_01[b-2] PASSED [ 83%] 笛卡尔积,测试数据为: 2-b test_01.py::test_parametrize_01[b-3] PASSED [100%] 笛卡尔积,测试数据为: 3-b
@pytest.param(values, marks, id)
标记数据@pytest.mark.parametrize("test_input, expected", [
("3+5", 8),
("2+4", 6),
pytest.param("6*9", 42, marks=pytest.mark.xfail),
pytest.param("6*6", 42, marks=pytest.mark.skip)
])
def test_parametrize_02(test_input, expected):
assert eval(test_input) == expected
=========================================================================== test session starts ============================================================================
collected 4 items
test_01.py::test_parametrize_02[3+5-8] PASSED [ 25%]
test_01.py::test_parametrize_02[2+4-6] PASSED [ 50%]
test_01.py::test_parametrize_02[6*9-42] XFAIL [ 75%]
test_01.py::test_parametrize_02[6*6-42] SKIPPED (unconditional skip) [100%]
================================================================= 2 passed, 1 skipped, 1 xfailed in 0.08s ==================================================================
data_3 = [
(1, 2, 3),
(4, 5, 9)
]
ids = [f"a: {a} + b: {b} = expect: {expect}" for a, b, expect in data_3]
@pytest.mark.parametrize("a, b, expect", data_3, ids=ids)
def test_parametrize_03(a, b, expect):
assert a + b == expect
============================= test session starts ==============================
collecting ... collected 2 items
test_01.py::test_parametrize_03[a: 1 + b: 2 = expect: 3] PASSED [ 50%]
test_01.py::test_parametrize_03[a: 4 + b: 5 = expect: 9] PASSED [100%]
============================== 2 passed in 0.04s ===============================
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。