简体   繁体   English

pytest如何处理调用其他灯具的灯具?

[英]How does pytest deal with fixtures calling other fixtures?

I have two pytest fixtures, client and app . 我有两个pytest灯具, clientapp client calls app . client来电app

The test function test_register has arguments client and app and hence calls both fixtures. 测试函数test_register具有参数clientapp ,因此调用两个fixture。

My question is if the instance of app used in test_register is always going to be the one that client called, and if this is how pytest works in general (the assertion in test_register passes, so it is true in this case) . 我的问题是,如果test_register中使用的app实例总是将成为client调用的实例,并且如果这通常是pytest的工作方式(test_register中的断言通过,那么在这种情况下也是如此)。

In other words, does pytest generate unrelated instances for each argument in a test function that calls a fixture or does it call the fixtures and the instances returned also reference each other? 换句话说,pytest是否为调用fixture的测试函数中的每个参数生成不相关的实例,或者它是否调用了fixture并且返回的实例也相互引用?

Here's the code: 这是代码:

@pytest.fixture
def app():
    app = create_app({
        'TESTING': True,
    })

    yield app


@pytest.fixture
def client(app):
    return app.test_client()



def test_register(client, app):
    assert client.application is app

All fixtures have a scope, the implicit scope being function but there's also class , module and session scopes. 所有灯具都有一个范围,隐式范围是function但也有classmodulesession范围。 Within each scope there will only ever be one instance created of a fixture. 在每个范围内,只有一个实例创建了一个夹具。

So in your example both app and client are using the function-scope. 所以在你的例子中, appclient都在使用函数范围。 When executing test_register it enters the function-scope of this test and creates the fixture intances. 执行test_register它进入该测试的功能范围并创建夹具内容。 Hence both test_register and client get the same instance of app . 因此, test_registerclient获得相同的app实例。

See the docs for more details on how this all works. 有关这一切是如何工作的更多详细信息,请参阅文档

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

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