我想分享这个例子和我的逻辑。
假设我有3个应用程序,App1
,App2
,app3
,对于每个应用程序,我都执行SampleSuite。 SampleSuite具有1类范围内的灯具,可以使一些随机用户登录。在测试套件中的每个测试之前,有一个功能范围内的灯具将执行一些导航。
这可以通过下面的示例test_sample.py
和基本的pytest.ini
文件(用于记录)来实现
import logging
import pytest
logger = logging.getLogger(__name__)
@pytest.fixture(scope="session", params=["App1", "App2", "App3"])
def apps_fixture(request):
""" Sets up some generic app """
logger.info(f"Setting up for app {request.param}")
yield
logger.info(f"Tearing down for app {request.param}")
@pytest.mark.usefixtures("apps_fixture", "testsuite_user_fixture", "testcase_navigation_fixture")
class TestSampleSuite:
""" Sample Suite """
@classmethod
@pytest.fixture(scope="class")
def testsuite_user_fixture(cls):
""" fixture executed once for the test suite """
logger.info("Setting up testsuite_user_fixture to sign a random user in")
yield
logger.info("Tearing down testsuite_user_fixture to sign the user out")
@classmethod
@pytest.fixture(scope="function")
def testcase_navigation_fixture(cls):
""" fixture executed before each test case """
logger.info("Setting up testcase_navigation_fixture for navigation")
yield
logger.info("Tearing down testcase_navigation_fixture")
def test_sample_1(self):
"""
A test case
"""
logger.info("Test3")
现在,当我在上面运行此测试时,输出顺序如下...
test_apps.py::TestSampleSuite::test_sample_1[App1]
--------------------------------- live log setup ----------------------------------
2020-09-01 15:48:59 INFO Setting up for app App1
2020-09-01 15:48:59 INFO Setting up testsuite_user_fixture to sign a random user in
2020-09-01 15:48:59 INFO Setting up testcase_navigation_fixture for navigation
---------------------------------- live log call ----------------------------------
2020-09-01 15:48:59 INFO Test3
PASSED [ 33%]
-------------------------------- live log teardown --------------------------------
2020-09-01 15:48:59 INFO Tearing down testcase_navigation_fixture
test_apps.py::TestSampleSuite::test_sample_1[App2]
--------------------------------- live log setup ----------------------------------
2020-09-01 15:48:59 INFO Tearing down for app App1
2020-09-01 15:48:59 INFO Setting up for app App2
2020-09-01 15:48:59 INFO Setting up testcase_navigation_fixture for navigation
---------------------------------- live log call ----------------------------------
2020-09-01 15:48:59 INFO Test3
PASSED [ 66%]
-------------------------------- live log teardown --------------------------------
2020-09-01 15:48:59 INFO Tearing down testcase_navigation_fixture
test_apps.py::TestSampleSuite::test_sample_1[App3]
--------------------------------- live log setup ----------------------------------
2020-09-01 15:48:59 INFO Tearing down for app App2
2020-09-01 15:48:59 INFO Setting up for app App3
2020-09-01 15:48:59 INFO Setting up testcase_navigation_fixture for navigation
---------------------------------- live log call ----------------------------------
2020-09-01 15:48:59 INFO Test3
PASSED [100%]
-------------------------------- live log teardown --------------------------------
2020-09-01 15:48:59 INFO Tearing down testcase_navigation_fixture
2020-09-01 15:48:59 INFO Tearing down testsuite_user_fixture to sign the user out
2020-09-01 15:48:59 INFO Tearing down for app App3
根据我在此输出中看到的,对于每个传递的参数,会话作用域夹具apps_fixture
都已设置并拆除(按预期方式),但类作用域夹具testsuite_user_fixture
仅已设置并拆除了整个运行一次。 (这对我来说是令人困惑的部分)
我期望的是,将为传入的每个参数设置testsuite_user_fixture
并将其拆除,因为类的作用域小于会话。
至于我的调查,我在pytest文档或Google周围找不到太多帮助。
我确实发现,使用pytests --collect-only
有一些有趣的细节。运行pytest test_apps.py --collect-only
后,收集的结构如下所示……
<Module test_apps.py>
<Class TestSampleSuite>
<Function test_sample_1[App1]>
<Function test_sample_1[App2]>
<Function test_sample_1[App3]>
这使我对为什么每个参数仅设置和拆卸一次班级固定装置有一些了解,但是我不明白为什么。而且我尝试调试它,并了解pytest如何收集所有这些信息,但是我迷失了钩子。
注意:如果我在测试运行期间有2个或更多类,则testsuite_user_fixture
可以正常工作,因为每个类将在从apps_fixture
移至下一个参数之前运行。因此,该问题仅在运行1个测试类时出现。
我在pytest上找不到用于传递参数的夹具的出色文档,因此我希望在这里找到一些帮助。我的问题是...
是否有可能为为会话范围固定装置传递的每个参数运行类范围的固定装置?