python - 如何让两个已发布的装置相互依赖?

标签 python unit-testing pytest fixtures

test_something() 中,app 实例应与 login 实例使用的实例相同。

@pytest.fixture
def app():
    # ...
    return app

@pytest.fixture
def login(app):
    # ...
    return login

def test_something(self, app, login):
    pass

我尝试的是从第二个装置返回两个对象,但我不认为这是惯用的。

@pytest.fixture
def app_and_login(app):
    # ...
    return app, login

def test_something(self, app_and_login):
    app, login = login_and_login

有更好的方法吗?

最佳答案

正如您所描述的,默认情况下, fixture 已经为测试运行时共享。

这并没有在任何地方明确记录(或者至少我没有找到它),但它有点隐含:Sharing a fixture across tests in a module描述scope参数,默认范围为function

其他范围例如是 module (共享/缓存同一模块中所有测试的固定装置)或 session (缓存整个测试 session 的固定装置)。

关于python - 如何让两个已发布的装置相互依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100690/

相关文章:

python - 从元组列表中获取唯一元素的问题

python - Atom 模块导入错误

python - 获取 "__init__() got an unexpected keyword argument ' 文档 '"this error in python I' m 使用 Word2Vec 和 gensim

unit-testing - Jasmine 路线 spy - 未定义不是一个对象(评估 'navigate.calls.mostRecent().args' )

python - 有没有办法设置 pytest 来运行 webapp2 服务器以获取覆盖率报告?

python - '类型错误: 'NoneType' object is not subscriptable' about lists

c# - 使用 OR 条件断言

java - 使用 Mockito 模拟助手类

python - `py.test` 和 `__init__.py` 文件

pytest - Airflow - DAG 完整性测试 - sqlalchemy.exc.OperationalError : (sqlite3. OperationalError)没有这样的表:变量