Python 如何将pytest夹具应用于多个文件
建议如何将Python 如何将pytest夹具应用于多个文件,python,pytest,fixtures,Python,Pytest,Fixtures,建议如何将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_feat
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'
为每个文件重新应用装置
我希望改为以下行为:
test\u component\u 1/conftest.py
test\u component\u 2/conftest.py
谢谢 如果不使用
scope='session'
,就无法将作用域扩展到更多模块(文件)。无论如何,将一个模块的所有测试收集到一个文件中是一种好的做法。但是如果你有很好的理由这样做,你可以通过重命名来拆分它
test_feature_1.py --> feature_1_tests.py
test_feature_2.py --> feature_2_tests.py
并建立
test\u component\u 1.py
,调用这些文件中的测试。我想这就是作用域的工作方式。如果它是module
,则该夹具将被每个文件调用。在这种场景中,fixture在多个文件中都是必需的,我将其作用域保留为session
。唯一的问题是,组件_1夹具的拆卸将作为最后一步而不是第3步进行。但到目前为止,这还没有困扰我。