简体   繁体   中英

Import conftest.py from peer package

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.

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

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