如何手动触发共享 py.test 夹具的重新执行?

How can I manually trigger re-execution of a shared py.test fixture?

假设我有一个夹具,我想为我的大部分测试共享它:

@pytest.fixture(scope='session')
def account():
   # create a new account

但现在在其中一项测试中,我想覆盖 scope='session' 位并实际让夹具重新执行(创建一个新帐户)。有没有办法让 fixture "override the cache/memoization" 只进行一次测试?

不用去寻找你问题的答案,我想你的问题可以更容易地解决。只需将帐户创建分解为另一个函数并编写两个固定装置:

def create_account():
    return 'account'

@pytest.fixture(scope='session')
def account():
    return create_account()

@pytest.fixture(scope='something else'):
def another_account():
    return create_account()

简单地从测试中手动调用夹具将导致它重新执行。例如

def test_foo():
    new_account = account()