简体   繁体   English

使用 function 范围夹具设置 class

[英]Using function scoped fixture to setup a class

I have a fixture in conftest.py with a function scope.我在conftest.py中有一个夹具,带有 function scope。

@pytest.fixture()
def registration_setup(
    test_data, # fixture 1
    credentials, # fixture 2
    deployment # fixture 3
    deployment_object # fixture 4
):
    # pre-test cleanup
    do_cleanup()
    yield
    # post-test cleanup
    do_cleanup()

I use it in a test class like this:我在这样的测试 class 中使用它:

class TestClass:

    @pytest.fixture(autouse=True)
    def _inventory_cleanup(self, registration_setup):
        log('Cleanup Done!')
    
    def test_1():
        ...

    def test_2():
        ...
    
    def test_3():
        ...

Now I want to create a new test class where I run the registartion_setup fixture once for the entire class. The desired behaviour here is, First the pre-test cleanup executes and then all the tests in the new test class execute, followed by the post-test cleanup.现在我想创建一个新测试 class,我在其中为整个 class 运行一次registartion_setup fixture。这里期望的行为是,首先执行预测试清理,然后执行新测试 class 中的所有测试,然后执行 post - 测试清理。 How can I achieve this, thanks for the help.我怎样才能做到这一点,感谢您的帮助。

Option 1选项1

You can use the same approach you did on your other test class, but set the fixture scope to class :您可以使用与其他测试 class 相同的方法,但将夹具 scope 设置为class

class TestClass:

    @pytest.fixture(scope='class', autouse=True)
    def _inventory_cleanup(self, registration_setup):
        log('Cleanup Done!')
    
    def test_1():
        ...

    def test_2():
        ...
    
    def test_3():
        ...

But you will then need to change the scope of the fixture registration_setup to class to avoid a ScopeMismatch error.但是您随后需要将夹具registration_setup的 scope 更改为class以避免ScopeMismatch错误。

Option 2选项 2

To keep using it with a function scope, I suggest having two fixtures with the same behavior, but with different scopes, like this:为了继续将它与function scope 一起使用,我建议使用两个行为相同但作用域不同的灯具,如下所示:

@pytest.fixture()
def registration_setup_for_function(
    test_data, # fixture 1
    credentials, # fixture 2
    deployment # fixture 3
    deployment_object # fixture 4
):
    # pre-test cleanup
    do_cleanup()
    yield
    # post-test cleanup
    do_cleanup()


@pytest.fixture(scope='class')
def registration_setup_for_class(
    test_data, # fixture 1
    credentials, # fixture 2
    deployment # fixture 3
    deployment_object # fixture 4
):
    # pre-test cleanup
    do_cleanup()
    yield
    # post-test cleanup
    do_cleanup()

If your other fixtures 1, 2, 3 and 4 have function scope, you will have to change them also.如果您的其他灯具 1、2、3 和 4 有function scope,您也必须更改它们。

Option 3选项 3

If you don't want to have two identical fixtures with different scopes, you can do something like this:如果您不想拥有两个具有不同范围的相同灯具,您可以执行以下操作:

In a conftest.py file in the project root:在项目根目录下的conftest.py文件中:

def pytest_configure(config):
    config.first_test_executed = False

Then, wherever you have your fixture:然后,无论你有什么夹具:

@pytest.fixture()
def registration_setup(
    test_data, # fixture 1
    credentials, # fixture 2
    deployment, # fixture 3
    deployment_object, # fixture 4
    request # Note the request fixture here
):
    if 'TestClassWhereFixtureShouldRunOnlyOnce' in request.node.nodeid:
        if not request.config.first_test_executed:
            # pre-test cleanup
            do_cleanup()
            yield
            # post-test cleanup
            do_cleanup()
            request.config.first_test_executed = True
    else:
        # pre-test cleanup
        do_cleanup()
        yield
        # post-test cleanup
        do_cleanup()

I know it is still a bit repeated, but this way your tests inside the class will call the registration_setup fixture only once for the whole class, while other tests will call it always.我知道它仍然有点重复,但这样你在 class 中的测试将只为整个 class 调用registration_setup fixture 一次,而其他测试将始终调用它。 Maybe you will find a better way knowing this now.也许您现在会找到更好的方法来了解这一点。

More info on the documentation:有关文档的更多信息:

Fixtures can introspect the requesting test context 夹具可以内省请求的测试上下文

A session-fixture which can look at all collected tests 可以查看所有收集的测试的会话夹具

pytest_configure(config) pytest_configure(配置)

Huge thanks to @Marco.S for his answer.非常感谢@Marco.S 的回答。 Just a small additional check to get it to work只需进行少量额外检查即可使其正常工作

def reg_setup(request):
    if 'TestClassWhereFixtureShouldRunOnlyOnce' in request.node.nodeid:
        if not request.config.first_test_executed:
            # pre-test cleanup
            do_cleanup()
            request.config.first_test_executed = True
        yield
        if 'test_last' in request.node.nodeid: # Check for the last test case.
            # post-test cleanup
            do_cleanup()
    else:
        # pre-test cleanup
        do_cleanup()
        yield
        # post-test cleanup
        do_cleanup()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM