繁体   English   中英

除了 pytest 的测试 package 之外,conftest.py 文件是否可以放在另一个 package 中?

[英]Can the conftest.py file be put in another package aside the tests package for pytest?

问题:
是否可以将conftest.py文件放入 Test package 旁边的另一个 package 中,如下结构?

描述
项目结构如下:
--------------------------------------

GUI_pytest
  - C_lib
      --__init__.py
      -- conftest.py
  - G_test<br/>
      --__init__.py
      -- test_2.py

Source_Root: GUI_pytest
conftest.py: GUI_pytest/C_lib(package)
test_2.py(测试文件): GUI_pytest/G_test(package)

GUI_pytest/C_lib/conftest.py
有一个夹具 function 作为def a()

测试用例: GUI_pytest/G_test/test_2.py使用fixture作为def test_2(a)

我想将conftest.pytest_case().py拆分为C_lib packageG_test package

但是 pytest 似乎无法获取 conftest.py 文件错误找不到夹具 function a使用def test_2(a)

先感谢您
作为初学者,任何事情都可以帮助解决这个问题。

答案是否定的,如果没有变通办法,这是不可能的,我建议不要这样做。

如果你使用一个框架,你应该使用框架提供的约定——这使得使用更容易,并且你不会因为一些框架不期望的用法而遇到问题。 这里的框架是指pytest ,它不仅仅是一个可执行文件,因为它提供(并期望)一定的基础设施(当然可以更好地记录......)。 您可以在 SO 问题的答案中找到更多信息在 pytest 中,conftest.py 文件的用途是什么? .

pytest使用conftest.py文件以分层方式为测试提供夹具、插件和挂钩。
所以,比如说,你有这样的结构:

tests
   test_a
       test_a1.py
       test_a2.py
   test_b
       test_b1.py
       test_b2.py   

其中test_a中的测试使用夹具local_fixturetest_b中的测试使用另一个夹具local_fixture ,所有测试都使用通用夹具common_fixture 最简单的方法如下:

tests
   conftest.py -> contains common_fixture
   test_a
       conftest.py -> contains local_fixture for test_a...
       test_a1.py
       test_a2.py
   test_b
       conftest.py -> contains local_fixture for test_b...
       test_b1.py
       test_b2.py   

其他模块提供的插件中也可以有conftest.py文件,所有这些装置您可以直接使用而无需导入它们,或者做一些 PYTHONPATH 魔术。 您只需使用通用约定 go 而不是使用自己的约定即可使用该功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM