简体   繁体   中英

pytest doesn't execute test/__init__.py file

I have the following problem: I have project and a small library there and a test directory for lib, something like that:

project\\

+mylib\\

++test\\

In test__init__.py I have the following code:

SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, "test_data_files")))

So just updating syspath to be able open some files with test data. When I run test by right-click on directory in Pycharm it executes succesfully, but when I run

python -m pytest lib/test

It doesn't executes test/ init .py file and fails with FileNotFoundError, as it can find test data files.

What is the reason of such behavior, and how can I fix it?

Thank you.

If you run pytest in lib/test , it will look for test_*.py files and run the tests therein. As you found out, it does not import the test package so that the __init__ is never imported. If you run pytest "globally", it imports the packages it runs tests in so that your __init__ is imported.

To fix, you could make use of pytest's conftest.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