Autodetect imports on pytest tests directory

I have a python project with the following (widespread) structure:

project
- src
- mypackage
- __init__.py
- tests
- __init__.py
- test_something.py

I can run the tests from the terminal via

python -m pytest tests

However running the tests from the IDE (clicking on the green play button next to the test method) fails due to the `import mypackage` statement, as it cannot find it.

Doing some searching I found https://stackoverflow.com/questions/23984973/how-to-run-tests-without-installing-package which suggests to put a file named `pytest.ini` at the root of the project. This fixes the problem and I can run the tests.

The problem I have not managed to fix is the import resolving / autodetection. On the IDE, the `import mypackage` still shows as unresolved. I could "fix" it by installing the package, but I would like to avoid installing the package before running the tests whenever I want to use the IDE to travel through function definitions and whatnot. Is there any way to do this?

Thanks!

0

Please sign in to leave a comment.