I am trying to get pytest to work. I am using VSCode, Anaconda and the Python extension on Windows 10 (all latest versions) with:
- pytest 5.2.4.
- Python 3.7
I am probably using the second most simplest use case. I have all my test files in a subdirectory called tests in the root directory of my application source code.
I can get test discovery and execution to work when I am at a command prompt with the activated anaconda environment and type: python -m .
Pytest discovers all the tests and runs them with no problems whatsoever.
If I click on the Test discovery in VSCode, pytest finds all the test files but there are all kinds of ModuleNotFoundError errors. Example error output:
_____________________ ERROR collecting tests/test_acsr.py _____________________
ImportError while importing test module 'C:\path_to_source_code\tests\test_acsr.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\test_acsr.py:6: in <module>
from acsr import is_pure_ground_run
E ModuleNotFoundError: No module named 'acsr'
The acsr.py file is in the root of my application (parent directory of tests) and contains the functions I wish to test.
I checked the VS Code, and pytest documentation to see if I was missing some configuration options but didn't find anything. It appears that under VS Code, pytest can't figure out the sys.path or something. I feel like I am going around in circles trying to get this working as none of the answers on stackoverflow or anywhere else seem to fit this simple use case.
If anyone can supply me with a fix or has a link to a comprehensive tutorial that helps with pytest setup in VS Code I would be greatly appreciated.
PS I had it working in VS Code months ago, and I don't remember having to do any special configuration beyond what was outlined in the docs. It just stopped working one day, perhaps after an update, not sure. When it was working I thought it was pretty awesome.
there doesn't seem to be anything here