pytest fixture作用域用于会话和类的设置/拆除



我知道这是一个老问题,成千上万的人回答了类似的问题,但我仍然没有明白…我应该怎么做才能对整个测试会话以及每个测试类使用setup/teardown ?

例如,我有以下测试文件结构:
  • common_setup.py
  • testronguite_1.py
  • testronguite_2.py

文件如下:

# common_setup.py
import logging
import pytest
@pytest.fixture(scope="session")
def set_session_data():
# Setup
logging.info("In session setup")
# Teardown
yield
logging.info("In session teardown")
# test_suite_1.py
import logging
import pytest
import common_setup
@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")
class TestClass:
def test_case_1():
logging.info("In test suite 1, test case 1")

def test_case_2():
logging.info("In test suite 1, test case 2")
# test_suite_2.py
import logging
import pytest
import common_setup
@pytest.fixtures(scope="class")
def set_data():
# Setup
logging.info("In test suite 2 setup")
# Teardown
yield
logging.info("In test suite 2 teardown")
@pytest.mark.usefixture("set_data")
class TestClass:
def test_case_1():
logging.info("In test suite 2, test case 1")

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

我希望会话设置/拆除("common_setup.py")中的内容应该在每个会话中执行,每个测试套件也有自己特定的设置/拆除。

到目前为止,我有下面的日志,这意味着会话范围的方法没有被调用。我知道我没有使用它,但是我不能像@pytest.mark.usefixture("set_data", "set_session_data"那样简单地将它添加到usefixture中
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

欢迎任何讨论。谢谢!

尝试了不同的选项,上面的代码已经是我能走的最远的…
预期的执行应该是:

  1. 会话设置(当前缺失)
  2. suite 1 setup
  3. suite 1 case 1
  4. suite 1 case 2
  5. suite 1拆卸
  6. suite 2 setup
  7. suite 2 case 1
  8. suite 2 case 2
  9. suite 2拆卸
  10. 会话删除(当前丢失)

感谢@MrBean Bremen这个评论把我带到了这个页面,上面给出了完整的答案:https://docs.pytest.org/en/7.1.x/example/special.html

我不仅应该放置autouse=True,我还应该将common_setup.py文件重命名为conftest.py,并且它不需要导入。

所以,总的来说,这些东西应该是这样的:

  • common_setup.py→conftest.py
  • testronguite_1.py
  • testronguite_2.py

文件如下:

# conftest.py
import logging
import pytest
@pytest.fixture(scope="session", autouser=True)
def set_session_data():
# Setup
logging.info("In session setup")
# Teardown
yield
logging.info("In session teardown")
# test_suite_1.py
import logging
import pytest
@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")
class TestClass:
def test_case_1():
logging.info("In test suite 1, test case 1")

def test_case_2():
logging.info("In test suite 1, test case 2")
# test_suite_2.py
import logging
import pytest
@pytest.fixtures(scope="class")
def set_data():
# Setup
logging.info("In test suite 2 setup")
# Teardown
yield
logging.info("In test suite 2 teardown")
@pytest.mark.usefixture("set_data")
class TestClass:
def test_case_1():
logging.info("In test suite 2, test case 1")

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

和日志:

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

最新更新