Search code examples
pythonpytestfixtures

Pytest: use App conftest.py in framework tests


I'm trying to run splinter test cases using pytest. I have an app and a framework, and they both have defined test cases. When I start running the tests my app also runs the framework's test cases using the "main" function (from _pytest.config import main main([<framework_dir>, args, ...])).

The problem is that I want to use the app conftest.py and not the framework's conftest.py. I have tried to achieve that by using --confcutdir= option (main([<framework_dir>, '--confcutdir={}'.format(os.path.dirname(__file__)), ...])) but it doesn't work. When I list the fixtures (with --fixtures option) they are not listed (the ones that are defined in app's conftest.py).

So does anyone have any experience how to share fixtures between app and framework/lib?

I have created a GitHub repo that simulates my project and reproduces the error: https://github.com/karantan/global-fixtures


Solution

  • I got a lot of help on this on websauna IRC chat by the user "enkidulan". He pointed out that I shouldn't be calling framework tests via the main function.

    The root of my problem was the strategy of pytests discovery of fixtures functions seems like the closest to test files conftest.py always wins in overwrite competition, even in case if -p specifies some fixtures to load (not sure if it is a bug in pytests).

    So he suggested that I should rename conftest.py to something different, like fixtures.py (which I did). And then instead of discovering just the App tests (pytest src/app/tests/) I also discover framework tests + add which fixtures to use: pytest -p app.tests.fixtures src/app/tests/

    NOTE: the repo is updated with the last working version if anyone wants to check how the code looks.