简体   繁体   中英

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

Question:
Is it possible to put the conftest.py file into another package next to Test package as below structure?

Description
The project structure is as following:
--------------------------------------

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(test file): GUI_pytest/G_test(package)

GUI_pytest/C_lib/conftest.py :
there is a fixture function as def a()

test case: GUI_pytest/G_test/test_2.py uses the fixture as def test_2(a)

I want to split the conftest.py and test_case().py into C_lib package and G_test package

But the pytest seems not able to pick up the conftest.py file Error not finding the fixture function a used by def test_2(a)

Thank you in advance
As a beginning learner, anything is welcome for helping this out.

The answer is no, this is not possible without workarounds, and I would advice against it.

If you use a framework, you should use the conventions that the framework provides - this makes the usage easier, and you won't run into problems because of some usage that the framework does not expect. By framework here I mean pytest , which is more than an executable, as it provides (and expects) a certain infrastructure (which certainly could be documented better...). You can find more information for example in answers to the SO question In pytest, what is the use of conftest.py files? .

pytest uses conftest.py files to provide fixtures, plugins and hooks to tests in a hierarchical way.
So, say, you have the structure:

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

where the tests in test_a use a fixture local_fixture , the tests in test_b use another fixture local_fixture , and all tests use the common fixture common_fixture . The easiest way to do this is the following:

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   

There can also be conftest.py files in plugins that are provided by other modules, and all of these fixtures you can just use without importing them, or doing some PYTHONPATH magic. You just have to go with the common conventions instead of using your own to be able to use that power.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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