I spent ages trying to decipher this unhelpful error after creating a test that had import errors. Verify that your test suite can actually be executed before doing any deeper troubleshooting.
pytest --collect-only is your friend.
I spent ages trying to decipher this unhelpful error after creating a test that had import errors. Verify that your test suite can actually be executed before doing any deeper troubleshooting.
pytest --collect-only is your friend.
This is not a complete answer as I do not know why this is happening and may not relate to your problem, depending how you have your tests structured.
I resolved this issue by putting an __init__.py file in my tests folder
E.G.:
ββββ.vscode
β settings.json
β
ββββapp
β myapp.py
β
ββββtests
test_myapp.py
__init__.py
this was working a few days ago without this but the python extension was recently updated. I am not sure if this is the intended behavior or a side effect of how discoveries are now being made
https://github.com/Microsoft/vscode-python/blob/master/CHANGELOG.md
Use Python code for discovery of tests when using pytest. (#4795)
Test Discovery fails with E ModuleNotFoundError: No module named
python - VS Code Pytest Discovery Error Due to ModuleNotFoundError - Stack Overflow
how to set up pytest for subdirectory? Pytest Discovery Error
Pytest discovery stuck
Videos
I am working on a data science project and my project tree looks like this:
data/ notebooks/ scripts/ utils/ - __init__.py - utils1.py - utils2.py - utils3.py - utils4.py - tests/ -- test_utils1.py -- test_utils2.py -- test_utils3.py -- test_utils4.py
I import functions I wrote in utils1.py, utils2.py, etc. to reuse throughout my notebooks and scripts. To run the tests for these, I cd into utils/ and then run python -m pytest from the terminal, which works fine.
I saw that there is a beaker icon for testing in VSCode, but my tests are not being discovered:
screenshotContents of __init__.py look a little like this:
from utils.utils4 import foo, barfrom .utils1 import foo2, bar2from .utils2 import function1, function2from .utils3 import *
See the comment below for Output.
It breaks my flow to switch to the terminal each time I modify a utility function to verify tests are passing. How should I configure VSCode to make better use of the Test Explorer?
Update: see my comment for the solution
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.
Hi there! π
TL;DR
I am working on a project for a course I am doing. I am getting started with pytest so I can run tests against my code as I work on the project.
The project previously had a flat structure where the tests were in the same directory as the project scripts. That worked fine for me.
I have since restructured the code into what I understand is a better package structure so I can better present the project work in a repo that I created to showcase the project once I complete it.
The challenge now is that pytest can't locate my tests in VS Code (I'm using VS Code for my project partly because of the pytest support).
My project structure
This is my current project structure:
.
βββ README.md
βββ __pycache__
β βββ changeImage.cpython-39.pyc
β βββ process_data.cpython-39.pyc
β βββ run.cpython-39.pyc
β βββ supplier_image_upload.cpython-39.pyc
β βββ test_final_project.cpython-39-pytest-7.1.2.pyc
β βββ test_process_data.cpython-39-pytest-7.1.2.pyc
β βββ upload_data.cpython-39.pyc
βββ catalog.egg-info
β βββ PKG-INFO
β βββ SOURCES.txt
β βββ dependency_links.txt
β βββ top_level.txt
βββ environment.yml
βββ final_project
β βββ __init__.py
β βββ __pycache__
β β βββ __init__.cpython-39.pyc
β β βββ changeImage.cpython-39-pytest-7.1.2.pyc
β β βββ changeImage.cpython-39.pyc
β β βββ supplier_image_upload.cpython-39.pyc
β βββ changeImage.py
β βββ emails.py
β βββ example_upload.py
β βββ report_email.py
β βββ reports.py
β βββ run.py
β βββ supplier-data
β β βββ descriptions
β β β βββ 001.txt
β β β βββ ...
β β βββ images
β β βββ 001.jpeg
β β βββ 001.tiff
β β βββ 002....
β βββ supplier_image_upload.py
βββ module_import.ipynb
βββ pyproject.toml
βββ setup.py
βββ supplier-data.tar.gz
βββ tests
βββ __init__.py
βββ __pycache__
β βββ __init__.cpython-39.pyc
β βββ test_final_project.cpython-39-pytest-7.1.2.pyc
βββ test_final_project.pyWhen I created the package, I used what I subsequently realised is a somewhat outdated method, here: https://calmcode.io/pytest/package.html.
I also tried running pip3 install -e . although I can't say I know what I'm really doing here. This is my first time structuring my projects like a grown-up.
My challenge
So my current challenge is that I see this output from pytest in VS Code when I try to discover my tests:
==================================== ERRORS ====================================
_________________ ERROR collecting tests/test_final_project.py _________________
ImportError while importing test module '/Users/pauljacobson/Git/Learn_to_code/giap-final-project/tests/test_final_project.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/miniconda3/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_final_project.py:6: in <module>
from final_project.supplier_image_upload import list_supplier_data, upload_supplier_data
final_project/supplier_image_upload.py:4: in <module>
from changeImage import list_images
E ModuleNotFoundError: No module named 'changeImage'
=========================== short test summary info ============================
ERROR tests/test_final_project.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.08s =====================
Traceback (most recent call last):
File "/Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/get_output_via_markers.py", line 26, in <module>
runpy.run_path(module, run_name="__main__")
File "/opt/miniconda3/lib/python3.9/runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "/opt/miniconda3/lib/python3.9/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/opt/miniconda3/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
main(tool, cmd, subargs, toolargs)
File "/Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
parents, result = run(toolargs, **subargs)
File "/Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)
ERROR conda.cli.main_run:execute(49): `conda run python /Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/get_output_via_markers.py /Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /Users/pauljacobson/Git/Learn_to_code/giap-final-project -s --cache-clear tests` failed. (See above for error)
at ChildProcess.<anonymous> (/Users/pauljacobson/.vscode/extensions/ms-python.python-2022.8.0/out/client/extension.js:2:232793)
at Object.onceWrapper (node:events:510:26)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
When I configured pytest in VS Code, I selected pytest as the test framework, and the tests subdirectory as the location of my tests.
My question
So my question is how to make my tests discoverable here? My import statements in my test_final_project.py file are formatted like this:
from final_project.changeImage import list_images, reformat_images
If I "go to definition" for a module like changeImage, I am taken to the correct file.
What am I missing here?
Not sure if this solution was specific to my problem, but I simply add __init__.py to my tests folder and that solved the problem.
Solution: use the PYTHONPATH env. var
PYTHONPATH=. pytest
As mentioned by @J_H, you need to explicitly add the root directory of your project, since pytest only adds to sys.path directories where test files are (which is why @Mak2006's answer worked.)
Good practice: use a Makefile or some other automation tool
If you do not want to type that long command all the time, one option is to create a Makefile in your project's root dir with, e.g., the following:
.PHONY: test
test:
PYTHONPATH=. pytest
Which allows you to simply run:
make test
Another common alternative is to use some standard testing tool, such as tox.