@pytest.fixture defff(): logging.info("This is secend setup function") yield logging.info("This is secend teardown function") @pytest.fixture deffix(ff): logging.info("This is first fixture before") yield logging.info("This is first fixture after") defadd(a, b): return a + b classTestServer: defsetup_method(self, method): logging.info("This is class setup method %s" %(method.__name__)) defteardown_method(self, method): logging.info("This is class teardown method %s"%({method.__name__})) deftest_add_int(self,fix): assert add(1, 2) == 3
@pytest.fixture defff(): logging.info("This is secend setup function") yield logging.info("This is secend teardown function") @pytest.fixture(autouse=True) deffix(ff): logging.info("This is first fixture before") yield logging.info("This is first fixture after")
..\testCase\test_demo01.py::test_case_01 PASSED [ 25%] ..\testCase\test_server.py::test_add_list ------------------------------- live log setup -------------------------------------- 2025-05-0219:34:49 [INFO] This is Setup module testCase.test_server 2025-05-0219:34:49 [INFO] This is secend setup function 2025-05-0219:34:49 [INFO] This is first fixture before PASSED [ 50%] ------------------------------- live log teardown ----------------------------------- 2025-05-0219:34:49 [INFO] This is first fixture after 2025-05-0219:34:49 [INFO] This is secend teardown function ..\testCase\test_server.py::TestServer::test_add_int ------------------------------- live log setup -------------------------------------- 2025-05-0219:34:49 [INFO] This is secend setup function 2025-05-0219:34:49 [INFO] This is first fixture before 2025-05-0219:34:49 [INFO] This is classsetupmethodtest_add_intPASSED [ 75%] ------------------------------- livelogteardown ----------------------------------- 2025-05-02 19:34:49 [INFO] Thisisclassteardownmethod {'test_add_int'} 2025-05-0219:34:49 [INFO] This is first fixture after 2025-05-0219:34:49 [INFO] This is secend teardown function ..\testCase\test_server.py::TestServer::test_add_str ------------------------------- live log setup -------------------------------------- 2025-05-0219:34:49 [INFO] This is secend setup function 2025-05-0219:34:49 [INFO] This is first fixture before 2025-05-0219:34:49 [INFO] This is classsetupmethodtest_add_strPASSED [100%] ------------------------------- livelogteardown ----------------------------------- 2025-05-02 19:34:49 [INFO] Thisisclassteardownmethod {'test_add_str'} 2025-05-0219:34:49 [INFO] This is first fixture after 2025-05-0219:34:49 [INFO] This is secend teardown function 2025-05-0219:34:49 [INFO] This is Teardown module testCase.test_server
如果在测试类中的方法既添加了fixture,
也添加了setup_method和teardown_method,
并且autouse设为True,注意类里面的函数执行先手顺序变了,
secend fixture yield before -> first fixture yield before ->
setup_method -> teardown_method -> first fixture yield after ->
secend fixture yield after .
3. 返回内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@pytest.fixture defff(): logging.info("This is secend setup function") obtain_user = "joharynd" yield obtain_user logging.info("This is secend teardown function") @pytest.fixture deffix(ff): logging.info("This is first fixture before") obtain_pwd = "123456" info = {"username": ff, "password": obtain_pwd} yield info logging.info("This is first fixture after") deftest_add_str(self,fix): logging.info(f"The password is {fix}") assert add("1", "2") == "12"
运行结果:
1 2 3 4 5 6 7 8
--------------------------------- live log setup ---------------------------------------- 2025-05-0220:05:31 [INFO] This is secend setup function 2025-05-0220:05:31 [INFO] This is first fixture before --------------------------------- live log call ----------------------------------------- 2025-05-0220:05:31 [INFO] The password is {'username': 'joharynd', 'password': '123456'} PASSED [100%] --------------------------------- live log teardown ------------------------------------- 2025-05-0220:05:31 [INFO] This is first fixture after 2025-05-0220:05:31 [INFO] This is secend teardown function
..\testCase\test_demo01.py::test_case_01 PASSED [ 25%] ..\testCase\test_server.py::test_add_list ------------------------- live log setup --------------------------------- 2025-05-0220:19:29 [INFO] This is Setup module testCase.test_server 2025-05-0220:19:29 [INFO] This is secend fixture function 2025-05-0220:19:29 [INFO] This is first fixture before PASSED [ 50%] ..\testCase\test_server.py::TestServer::test_add_int ------------------------ live log setup ---------------------------------- 2025-05-0220:19:29 [INFO] This is classsetupmethodtest_add_intPASSED [ 75%] ------------------------ livelogteardown ------------------------------- 2025-05-02 20:19:29 [INFO] Thisisclassteardownmethod {'test_add_int'} ..\testCase\test_server.py::TestServer::test_add_str ------------------------ live log setup ---------------------------------- 2025-05-0220:19:29 [INFO] This is classsetupmethodtest_add_str ------------------------ livelogcall ----------------------------------- 2025-05-02 20:19:29 [INFO] Thepasswordis {'username': 'joharynd', 'password': '123456'} PASSED [100%] ------------------------ live log teardown ------------------------------- 2025-05-0220:19:29 [INFO] This is classteardownmethod {'test_add_str'} 2025-05-0220:19:29 [INFO] This is first fixture after 2025-05-0220:19:29 [INFO] This is secend fixture function 2025-05-0220:19:29 [INFO] This is Teardown module testCase.test_server
..\testCase\test_demo01.py::test_case_01 ------------------------- live log setup ----------------------------------- 2025-05-0220:57:20 [INFO] This is secend fixture function 2025-05-0220:57:20 [INFO] This is first fixture before PASSED [ 25%] ..\testCase\test_server.py::test_add_list ------------------------- live log setup ----------------------------------- 2025-05-0220:57:20 [INFO] This is Setup module testCase.test_server PASSED [ 50%] ..\testCase\test_server.py::TestServer::test_add_int ------------------------- live log setup ----------------------------------- 2025-05-0220:57:20 [INFO] This is classsetupmethodtest_add_intPASSED [ 75%] ------------------------- livelogteardown -------------------------------- 2025-05-02 20:57:20 [INFO] Thisisclassteardownmethod {'test_add_int'} ..\testCase\test_server.py::TestServer::test_add_str ------------------------- live log setup ----------------------------------- 2025-05-0220:57:20 [INFO] This is classsetupmethodtest_add_str ------------------------- livelogcall ------------------------------------ 2025-05-02 20:57:20 [INFO] Thepasswordis {'username': 'joharynd', 'password': '123456'} PASSED [100%] ------------------------- live log teardown -------------------------------- 2025-05-0220:57:20 [INFO] This is classteardownmethod {'test_add_str'} 2025-05-0220:57:20 [INFO] This is Teardown module testCase.test_server 2025-05-0220:57:20 [INFO] This is first fixture after 2025-05-0220:57:20 [INFO] This is secend fixture function