fixture 之 本身自带固定参数
1 import pytest 2 source:https://github.com/pytest-dev/pytest/blob/master/doc/en/fixture.rst 3 4 @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)]) 5 def data_set(request): 6 print(request) 7 print(request.param) 8 return request.param 9 10 11 def test_data(data_set): 12 pass 13 14 15 @pytest.fixture(scope="module", params=["mod1", "mod2"]) 16 def modarg(request): 17 param = request.param 18 print(" SETUP modarg", param) 19 yield param 20 print(" TEARDOWN modarg", param) 21 22 23 @pytest.fixture(scope="function", params=[1, 2]) 24 def otherarg(request): 25 param = request.param 26 print(" SETUP otherarg", param) 27 yield param 28 print(" TEARDOWN otherarg", param) 29 30 31 def test_0(otherarg): 32 print(" RUN test0 with otherarg", otherarg) 33 34 35 def test_1(modarg): 36 print(" RUN test1 with modarg", modarg) 37 38 39 def test_2(otherarg, modarg): 40 print(" RUN test2 with otherarg {} and modarg {}".format(otherarg, modarg)) 41 42 if __name__ == '__main__': 43 print('start') 44 pytest.main(["-s", "-v", "testfixture.py"]) 45 print('end')
执行结果:
testfixture.py::test_data[0] <SubRequest 'data_set' for <Function test_data[0]>> 0 PASSED testfixture.py::test_data[1] <SubRequest 'data_set' for <Function test_data[1]>> 1 PASSED testfixture.py::test_data[2] SKIPPED testfixture.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 PASSED TEARDOWN otherarg 1 testfixture.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 PASSED TEARDOWN otherarg 2 testfixture.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 PASSED testfixture.py::test_2[mod1-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 PASSED TEARDOWN otherarg 1 testfixture.py::test_2[mod1-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 PASSED TEARDOWN otherarg 2 testfixture.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 PASSED testfixture.py::test_2[mod2-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 PASSED TEARDOWN otherarg 1 testfixture.py::test_2[mod2-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2