You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为pytest同时配置session与class级别的setup/teardown?

如何为Pytest配置Session级和测试类级的Setup/Teardown

问题核心

你的Session级fixture未执行,原因是Pytest的fixture仅在被显式引用时才会触发,同时代码中存在几处语法错误,导致功能异常:

  1. test_suite_2.py中@pytest.fixtures多了一个s,正确应为@pytest.fixture
  2. @pytest.mark.usefixture应为复数形式@pytest.mark.usefixtures,才能同时指定多个fixture
  3. 测试类中的测试方法未接收self参数,不符合类方法定义规范

解决方案

方法1:在测试类中显式引用Session级Fixture

修改测试文件,在usefixtures中同时指定类级和Session级fixture,同时修正语法错误:

修正后的test_suite_1.py

import logging
import pytest
from common_setup import set_session_data

@pytest.fixture(scope="class")
def set_data():
    # Setup
    logging.info("In test suite 1 setup")
    # Teardown
    yield
    logging.info("In test suite 1 teardown")

@pytest.mark.usefixtures("set_data", "set_session_data")
class TestClass:
    def test_case_1(self):
        logging.info("In test suite 1, test case 1")

    def test_case_2(self):
        logging.info("In test suite 1, test case 2")

修正后的test_suite_2.py

import logging
import pytest
from common_setup import set_session_data

@pytest.fixture(scope="class")
def set_data():
    # Setup
    logging.info("In test suite 2 setup")
    # Teardown
    yield
    logging.info("In test suite 2 teardown")

@pytest.mark.usefixtures("set_data", "set_session_data")
class TestClass:
    def test_case_1(self):
        logging.info("In test suite 2, test case 1")

    def test_case_2(self):
        logging.info("In test suite 2, test case 2")

方法2:使用conftest.py自动加载Session级Fixture

如果不想在每个测试文件中手动导入Session fixture,可以将common_setup.py的内容迁移到conftest.py(Pytest会自动识别该文件中的fixture,无需导入):

新建conftest.py

import logging
import pytest

@pytest.fixture(scope="session")
def set_session_data():
    logging.info("In session setup")
    yield
    logging.info("In session teardown")

此时测试文件无需再导入common_setup,直接在usefixtures中指定set_session_data即可,比如test_suite_1.py可简化为:

import logging
import pytest

@pytest.fixture(scope="class")
def set_data():
    logging.info("In test suite 1 setup")
    yield
    logging.info("In test suite 1 teardown")

@pytest.mark.usefixtures("set_data", "set_session_data")
class TestClass:
    def test_case_1(self):
        logging.info("In test suite 1, test case 1")

    def test_case_2(self):
        logging.info("In test suite 1, test case 2")

验证执行结果

运行测试后,日志将符合预期顺序:

2022-11-28 15:16:25 INFO In session setup
2022-11-28 15:16:25 INFO In test suite 1 setup
2022-11-28 15:16:25 INFO In test suite 1, test case 1
2022-11-28 15:16:25 INFO In test suite 1, test case 2
2022-11-28 15:16:25 INFO In test suite 1 teardown
2022-11-28 15:16:25 INFO In test suite 2 setup
2022-11-28 15:16:25 INFO In test suite 2, test case 1
2022-11-28 15:16:25 INFO In test suite 2, test case 2
2022-11-28 15:16:25 INFO In test suite 2 teardown
2022-11-28 15:16:25 INFO In session teardown

额外说明

  • Session级fixture的scope="session"确保它在整个测试会话中仅执行一次(启动时setup,所有测试完成后teardown)
  • 类级fixture的scope="class"确保每个测试类执行一次setup和teardown
  • 如果希望Session fixture自动生效(无需在每个类中指定),可以在conftest.py中使用@pytest.fixture(scope="session", autouse=True),Pytest会自动触发该fixture

内容的提问来源于stack exchange,提问作者NevNein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:10:17