关于conftest文件与夹具feature方法的作用范围及原理

conftest文件
官方文档示例:

import pytest


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


@pytest.fixture
def outer(order, inner):
    order.append("outer")


class TestOne:
    @pytest.fixture
    def inner(self, order):
        order.append("one")

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


class TestTwo:
    @pytest.fixture
    def inner(self, order):
        order.append("two")

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

如果一个测试文件里使用了某个夹具,那么pytest就会从当前测试文件为起点向上找conftest文件,并且寻找里面的是否存在调用的夹具,如果不存在则继续往上一个文件夹寻找,直到找到第一个含有正确夹具函数的conftest文件为止。

image