Import conftest.py from peer package

Viewed 182

Currently, I have two packages as part of my test repository with the following folder structure:

Package_A
|--__init__.py
|--conftest.py
|--test_A.py

Package_B
|--__init__.py
|--test_B.py

As part of this, my requirement is to call fixtures defined in Package_A/conftest.py from Package_B/test_B.py. I understand that this could be done easily if Package B was within Package A. However, because of certain requirements that is not possible. Is there a way I could just import all fixtures defined in conftest of Package A into Package B instead of duplicating them into the package. Thanks.

2 Answers

Just take it easy, if you are using some IDE like Pycharm or VSC. Whenever you want to import a package you need to call exactly the directory of this. But in IDE, you can do as follow: If you want to import to file test_B.py in Package_B

from Package_A import test_A 

Or

from Package_A.test_A import (Exact class or function you want)

After searching a little further, was able to solve this through the following re-structuring and using pytest plugins instead:

Package_A
|--__init__.py
|--fixtures.py - Define the fixtures to be shared across packages
|--conftest.py (Import fixtures.py by using 'pytest_plugins')
|--test_A.py

Package_B
|--__init__.py
|--conftest.py (Import fixtures.py from Package A by using 'pytest_plugins')
|--test_B.py
Related