3

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.

  • Put it into the root path, if the content shall be used for all tests, and put separate `conftest.py` files with the test-specific stuff into the test paths, if needed. – MrBean Bremen Jun 08 '20 at 13:04
  • @MrBeanBremen: May the ```conftest.py``` is put into another package as sibling to the ```test package```, I do not want to let the ```conftest.py``` under the root directory or mixing test files in test package. Thank you very much! It is my first question's first replying. – user7784148 Jun 08 '20 at 13:18
  • `conftest.py` is not a test file, it is a configuration file - you can have as many as you like in different locations, and they will be used in the directory they are found in, or any subdirectories. [Here](https://stackoverflow.com/a/34520971/12480730) are some related explanations (not sure I understood you correctly, though). – MrBean Bremen Jun 08 '20 at 13:32
  • @MrBeanBremen, thanks, not quite exact, the question is: 1) **root/lib/conftest.py** 2) **root/test/test_case.py**, how does **test_case.py** use the root/lib/conftest.py file? Because the pytest does not automatically load the **root/lib/conftest.py**? Thanks for your link which is also useful. – user7784148 Jun 08 '20 at 14:57
  • The lookup of `conftest.py` is the same as the test lookup. I'm still not sure I get your problem. You have a fixture that is specific to a test, but you don't want to put it alongside the test, or at the parent directory? Something like this? Why has `conftest.py` to be under `C_lib`? – MrBean Bremen Jun 08 '20 at 15:15
  • @MrBeanBremen, yes, yes, you got me, my god, yes, I really do not want to put the `conftest.py` under the root or alongside to the test, I want to put the `conftest.py` into another package `root\anotherpackage\conftest.py`, is that possible? Thank you again!!! – user7784148 Jun 08 '20 at 15:29
  • @MrBeanBremen, as the conftest.py is like a library file(from my opinion), so I make a `root/C_lib/` package to contain any lib files. So I put the conftest.py into `root/C_lib/` package. – user7784148 Jun 08 '20 at 15:35
  • 1
    The `conftest.py` is an extension of `pytest` specifically for your test suite. Keep it with your tests, not with your production code. – hoefling Jun 08 '20 at 20:04
  • @MrBeanBremen, thank you for clarifying that. Thank you! – user7784148 Jun 09 '20 at 02:21

1 Answers1

7

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.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46