I. Fixture validity

Fixture validity, in other words, means that fixture functions can only be requested within the scope of their defined use. For example, if you define a fixture in your class, only the test functions in that class can request it. However, if a fixture defines the scope of the entire module, then every test function in that module can request it.

There is another argument that affects the validity of fixtures: autouse=True. The default is False, and if True is equal to that, that fixture will be executed before other fixtures.

In addition, a fixture function can request any other fixture function. Regardless of where the requested fixture function is defined, the fixture function works as long as the test function requests them. Take a look at the sample code (to get a better idea, I’ve added a couple of fixture functions to the official code: print) :

# content of test_module1.py import pytest @pytest.fixture def order(): Print ("\n run fixture function-order ") return [] @pytest.fixture def outer(order, inner): Fixture def inner(self, order): print(" run fixture function -outer") order.append("outer") class TestOne: @pytest. Fixture def inner(self, order): Print (" fixture-inner") order. Append ("one") def test_order(self, order, outer): assert order == ["one", "outer"] class TestTwo: @pytest.fixture def inner(self, order): Print (" fixture-inner from TestTwo ") order. Append ("two") def test_order(self, order, outer): assert order == ["two", "outer"]Copy the code

Note:

  • There is a fixture functionouterOutside the test class
  • There are two other namesinnerThe fixture function for the test classTestOneandTestTwoIn the.
  • The external fixture functionouterIn, the internal fixture function is requestedinner.

Now I just run the TestOne class and see the result:

Test_module1. Py operation fixture function - the order the operation under the TestOne fixture - inner fixture function - outer. [100%] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1 Passed in 0.01 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0Copy the code

Indicates that the assertion in the test function passes. When the test function is executed, the inner requested by outer is under TestOne. Inner, though, only works with test functions under TestOne. However, since the test function requests the outer outer, the outer outer can also request the inner inner.

The official also gave a schematic diagram, can be combined with the above ideas, to understand.

Note that the scope of the fixture definition is independent of the order in which it will be instantiated: that order is enforced by the calling logic (see this article).

Fixtures: cross file sharing

If you put your fixture functions in the conftest.py file, you can request fixtures in the entire directory where the file resides without importing them.

In a real world scenario, our test directories or packages might have multiple levels of nesting, in which case each directory could have its own ConfTest file. For example, something like this:



The content at each level looks like this:

tests/
    __init__.py

    conftest.py
        # content of tests/conftest.py
        import pytest

        @pytest.fixture
        def order():
            return []

        @pytest.fixture
        def top(order, innermost):
            order.append("top")

    test_top.py
        # content of tests/test_top.py
        import pytest

        @pytest.fixture
        def innermost(order):
            order.append("innermost top")

        def test_order(order, top):
            assert order == ["innermost top", "top"]

    subpackage/
        __init__.py

        conftest.py
            # content of tests/subpackage/conftest.py
            import pytest

            @pytest.fixture
            def mid(order):
                order.append("mid subpackage")

        test_subpackage.py
            # content of tests/subpackage/test_subpackage.py
            import pytest

            @pytest.fixture
            def innermost(order, mid):
                order.append("innermost subpackage")

            def test_order(order, top):
                assert order == ["mid subpackage", "innermost subpackage", "top"]
Copy the code

Again, there is a scope boundary diagram to help you understand.

Knowledge:

  • At the top of theconftestIn theorderandtopAll available to the current and lower levels (one circle corresponds to each scope).
  • A test function can only search up the hierarchy for available fixture functions (out of the loop), but it can’t search down another loop.

    So,tests/subpackage/test_subpackage.py::test_orderYou can find the definition intests/subpackage/test_subpackage.pyIn theinnermost.

    But there’s another definitiontests/test_top.pyChinese, also calledinnermostFixture, righttest_orderIt’s not available.

In fact, to use my vernacular, you have to use the confTest fixture functions at or above the same level. However, other sibling directories or packages in the parent directory and their lower level confTest are not available to you.

But after reading the official document, I think the official circle is quite good and more rigorous.