我知道这个问题很老了,但我没有看到你描述的行为,我被这个 Q&A 真正转移了注意力,直到我想,“它不能那样工作.. .”并为自己测试了它。我试过这个:
platform darwin -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 -- .../bin/python3
我做了这个测试,它通过了:
import collections
import pytest
AppObject = collections.namedtuple("App", ["name", "login"])
@pytest.fixture
def app():
app = AppObject("appname", "applogin")
return app
@pytest.fixture
def login(app):
return app.login
def test_something(app, login):
assert isinstance(app, AppObject) and app.name == "appname"
assert isinstance(login, str) and login == "applogin"
OP 似乎担心 login 夹具接收的 app 对象与应用夹具返回的 app 不同。我不认为会发生这种情况。例如,如果您添加一些有用的 print 语句,如下所示:
import collections
import pytest
AppObject = collections.namedtuple("App", ["name", "login"])
@pytest.fixture
def app():
app = AppObject("appname", "applogin")
print("Object id in app fixture: %d" % id(app))
return app
@pytest.fixture
def login(app):
print("Object id in login fixture: %d" % id(app))
return app.login
def test_something(app, login):
print("Object id in test: %d" % id(app))
assert isinstance(app, AppObject) and app.name == "appname"
assert isinstance(login, str) and login == "applogin"
我看到了这个输出:
Object id in app fixture: 4451368624
Object id in login fixture: 4451368624
Object id in test: 4451368624
...所以这三个地方绝对是同一个对象。像这样的嵌套装置对我来说“只是工作”,所以我要么错过了你问的问题的重点,要么行为发生了变化,或者......其他的东西。但我想把它留在这里,以供其他寻找这样的嵌套/依赖装置的人使用。