[英]How to find if this is the first call or second call for a fixture in pytest
我试图弄清楚是否有任何方法可以在我使用固定装置时,如果这是第一次返回 true,否则返回 false。 比方说:
@pytest.fixture( scope = "session" )
def first() :
# first call should return true
我要测试的 scope 是 session,我知道这个夹具每 session 只运行一次,但我想以任何方式修改此行为。
由于您使用的 scope 是session ,因此def first()
中的代码将运行一次并生成一个 object 。 然而,这个返回的 object 可以是你想要的任何东西,并且可以以你想要的任何方式表现。
例如,您可以返回一个无穷无尽的生成器,它会在第一次返回True
时返回False
,对于next
的任何其他调用,如下所示:
import itertools
import pytest
@pytest.fixture(scope="session")
def first():
# first call should return true
return itertools.chain(itertools.repeat(True, 1), itertools.repeat(False))
def test_one(first):
assert next(first)
def test_two(first):
assert not next(first)
def test_three(first):
assert not next(first)
这就是您在测试中区分这是否是第一次的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.