python - pytest fixtures 以什么顺序执行?

标签 python fixtures pytest

对于我正在测试的应用程序,我想创建一个 autouse=True猴子修补的固定装置smtplib.SMTP.connect如果他们尝试意外发送电子邮件,则测试失败。

但是,在我确实希望测试发送电子邮件的情况下,我想使用不同的 fixture 来记录这些电子邮件(最有可能通过使用 smtpserver 中的 pytest-localserver fixture 并猴子修补 connect 方法来使用该装置返回的主机/端口)

当然,只有当 autouse fixture 在另一个 fixture(作为 funcarg 加载)之前执行时才能工作。是否有任何执行固定装置的特定顺序和/或是否有保证执行顺序的方法?

最佳答案

控制 fixture 执行顺序的最简单方法是只在后面的 fixture 中请求前一个 fixture。所以要确保 ba 之前运行:

@pytest.fixture(autouse=True, scope="function")
def b():
    pass

@pytest.fixture(scope="function")
def a(b):
    pass

有关一般 fixture 解析顺序的详细信息,请参阅 Maxim's excellent answer below或者看看 the documentation .

关于python - pytest fixtures 以什么顺序执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660064/

相关文章:

java - 简化和改进数据类型映射器的单元测试

ruby-on-rails - rails : running code once after loading all global fixtures

python - 具有 session 范围的 PyTest fixture 不保持数据库数据的连续性

python - pytest收集了0个项目

python - 如何在Python中引用重写的类函数

python - 使用 MatPlotLib 绘制连续数据流

python - 使用 Google OR 工具对类变量添加约束

python - 如何将 fixture 作为参数传递给另一个 fixture

unit-testing - 覆盖 FastAPI 依赖项以进行测试的最佳方法,每个测试都有不同的依赖项

python - virtualenv 是如何工作的?