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)
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
Python Test Discovery Failed with no error message
Test discovery error with pytest
VSCode cannot detect pytest unit tests, even pytest has been installed, error comes from inside .vscode-serve folder, behind company's proxy
Pytest discovery fails
Videos
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?