python - 覆盖pytest中的子 fixture

标签 python pytest

我将 pytest 与一些复杂的依赖注入(inject)装置一起使用。我有使用长链中其他固定装置的固定装置。我希望能够更改链中间的一些固定装置以进行特定测试。

给定这些(简化的)固定装置:

@pytest.fixture
def cache():
    return Cache()

# Use cache fixture in a new fixture.
@pytest.fixture
def resource(cache):
    return Resource(cache=cache, working=True)

# Use resource fixture in a new fixture.
@pytest.fixture
def service(resource):
    return Service(resource=resource)

还有一些测试:

def test_service_when_resource_working(service):
    assert service.status == "good"

def test_service_when_resource_broken(service):
    assert service.status == "bad"

我怎样才能重写 resource fixture,让它变成这样:

@pytest.fixture
def broken_resource(cache):
    return Resource(cache=cache, working=False)

...但仅针对 test_service_when_resource_broken 测试用例?我可以创建一个使用 broken_resourcebroken_service,但实际情况是依赖链很长,我想重新使用所有 fixture,但有选择地更改一些他们在中间进行选定的测试。

我想做这样的事情(伪代码):

@pytest.override_fixture('resource', 'broken_resource')
def test_service_when_resource_broken(service):
    # service should have been instantiated with broken_resource instead of resource.
    assert service.status == "bad"

最佳答案

您可以使用 markers在你的测试中达到你的期望。 基本上,您标记需要不同行为的测试。在 fixture 方法中,从请求的测试上下文和过程中查找该标记。

这里是你如何做到的。

@pytest.fixture
def cache():
    return Cache()

# Use cache fixture in a new fixture.


@pytest.fixture
def resource(request, cache):
    working = True
    marker = request.node.get_marker("broken")
    if marker:
        working = False

    return Resource(cache=cache, working=working)


# Use resource fixture in a new fixture.
@pytest.fixture
def service(resource):
    return Service(resource=resource)


def test_service_when_resource_working(service):
    assert service.status == "good"


@pytest.mark.broken
def test_service_when_resource_broken(service):
    assert service.status == "bad"

关于python - 覆盖pytest中的子 fixture ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927712/

相关文章:

python - 将整数数组转换为索引字典

python - 保证不存在文件的单元测试 fixture

python - 如何让 py.test 测试接受交互式输入?

python - collective.documentviewer with plone 4.2.1 抛出错误

python - 如何填充ObjectListView每一行的空白?

linux - 用户输入的Python检查日期

python - 如何在测试的不同 python 源中模拟导入?

python - scipy.spatial.transform.Rotation 旋转数组与同一对象内的堆栈

python - 如何将 pytest 与子进程一起使用?

Jenkins 在带有生成报告的 docker 容器中构建