一、fixture有效性

fixture有效性,说白了就是fixture函数只有在它定义的使用范围内,才可以被请求到。比如,在类里面定义了一个fixture,
那么就只能是这个类中的测试函数才可以请求。但是,如果一个fixture定义的范围是整个模块,那么这个模块下的每个测试函数都可以去请求。

这里还有另一个影响fixture有效性的参数autouse=True,默认为False,等于True的话会在其他fixture之前先执行该fixture,后面有需要
另起一篇,这里简短带过。

另外,一个fixture函数还可以请求任何其他的fixture函数。不管被请求的那个fixture函数在哪里定义,只要测试函数请求了它们,fixture函数就可以。
看示例代码(为了更直观的看效果,在官方代码基础上我加了几个fixture函数的print):

#  content of test_module1.py 
import pytest


@pytest.fixture
def order():
    print("n运行fixture函数-order")
    return []


@pytest.fixture
def outer(order, inner):
    print("运行fixture函数-outer")
    order.append("outer")


class TestOne:
    @pytest.fixture
    def inner(self, order):
        print("运行TestOne下的fixture-inner")
        order.append("one")

    def test_order(self, order, outer):
        assert order == ["one", "outer"]


class TestTwo:
    @pytest.fixture
    def inner(self, order):
        print("运行TestTwo下的fixture-inner")
        order.append("two")

    def test_order(self, order, outer):
        assert order == ["two", "outer"]

注意:

  • 这里有一个fixture函数outer在测试类的外部
  • 另外还有2个名字都叫inner的fixture函数,分别在测试类TestOneTestTwo中。
  • 在外部的fixture函数outer中,又请求了内部的fixture函数inner

现在我只运行类TestOne,看运行结果:

test_module1.py 
运行fixture函数-order
运行TestOne下的fixture-inner
运行fixture函数-outer
.                                                        [100%]

============================== 1 passed in 0.01s ==============================
Process finished with exit code 0

说明测试函数里的断言通过。测试函数执行的时候,外部outer请求的innerTestOne下的。
虽然TestOne类下的inner,只能作用于TestOne下的测试函数。但是,由于测试函数请求了
外部的outer,所以,外部的outer也就可以请到内部的inner

官方还给出一个示意图,可以结合着上述的思路,理解一下。

注意,fixture定义的范围与它将被实例化的顺序无关:实例化顺序由调用逻辑强制执行(可以参考这篇)。

二、跨文件共享fixtures

如果你把fixture函数放到conftest.py文件中,那么在这个文件所在的整个目录下,都可以直接请求里面的fixture,不需要导入。

在实际场景中,我们的测试目录或者包可能有多层的嵌套,这种情况下,每个目录都可以有一个自己的conftest文件。
比如,像这样:


各层级里的内容是这样的:

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"]

同样的,这里也有一张作用域边界图帮助理解。

知识点:

  • 顶层下的conftest里的ordertop对当前层和下层级的所有可用(一个圈就对应各自的作用域)。
  • 测试函数只可以向上层级搜索可用的fixture函数(出圈),但是出圈查找的过程中,不能再进到别的圈子向下查找。
    所以,tests/subpackage/test_subpackage.py::test_order可以找到定义在tests/subpackage/test_subpackage.py里的innermost
    但是,另一个定义在tests/test_top.py中,名字也叫innermost的fixture,对test_order来说就不可用了。

其实对于上述,按照我的白话来说,想用conftest里的fixture函数,你只能用同层级或者上层级的。但是上级里的其他兄弟目录或者包,以及他们
的下层级的conftest,你是不能用的。

但是读了官方文档,我觉得官方的那个圈子描述挺不错的,更严谨。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!