【问题标题】:How to have a pytest fixture applied to multiple files如何将 pytest 固定装置应用于多个文件
【发布时间】:2019-09-05 00:04:05
【问题描述】:

将pytest 固定装置应用于多个测试文件而不将固定装置范围设置为session(这太全球化)的推荐方法是什么?

这是我希望使用的结构:

test_component_1/conftest.py
test_component_1/test_feature_1.py
test_component_1/test_feature_2.py

test_component_2/conftest.py
test_component_2/test_feature_1.py
test_component_2/test_feature_2.py

使用scope='module' 为每个文件重新应用fixture。

我想要以下行为:

  1. 从test_component_1/conftest.py 设置component_1 固定装置
  2. 运行 component_1 测试
  3. 拆除 component_1 固定装置
  4. 从test_component_2/conftest.py 设置component_2 固定装置
  5. 运行 component_2 测试
  6. 拆除 component_2 固定装置

谢谢!

【问题讨论】:

  • 我猜这就是示波器的工作原理。如果是module,那么每个文件都会调用该夹具。在多个文件中需要夹具的这种情况下,我将其范围保持为session。唯一的问题是 component_1 夹具的拆卸将作为最后一步而不是第 3 步进行。但到目前为止,这并没有打扰我。

标签: python pytest fixtures


【解决方案1】:

如果不使用scope='session',就无法将范围扩展到更多模块(文件)。无论如何,将一个模块的所有测试收集在一个文件中是一种很好的做法。但是如果你有很好的理由这样做,你可以通过重命名来拆分它

test_feature_1.py --> feature_1_tests.py
test_feature_2.py --> feature_2_tests.py

并建立test_component_1.py,它会从这些文件中调用测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多