Test discovery fails if package is not installed
See original GitHub issueEnvironment data
- VS Code version: 1.42.1
- Extension version (available under the Extensions sidebar): 2020.2.64397
- OS and version: Ubuntu 18.04
- Python version (& distribution if applicable, e.g. Anaconda): 3.6.7
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
- Relevant/affected Python packages and their versions: N/A
- Relevant/affected Python-related VS Code extensions and their versions: N/A
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"
set to; more info #3977):false
- Value of the
python.languageServer
setting:Microsoft
Expected behaviour
When launching vscode, the tests from the tests/
directory should be discovered successfully; regardless of whether the project has been installed as a package.
Actual behaviour
Import errors are thrown out in the output tab and test discovery fails.
Example output:
python <path_to_home>/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir <path_to_project> -s --cache-clear -o junit_family=xunit1
Test Discovery failed:
Error: ============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: <path_to_project>
collected 0 items / 4 errors
==================================== ERRORS ====================================
__________________ ERROR collecting <path_to_test_file> ___________________
ImportError while importing test module '<path_to_test_file> '.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
<path_to_test_file>:5: in <module>
import <module_within_project>
E ImportError: cannot import name '<module_name>'
Running pytest separately, with the same launch arguments works fine in a terminal window, but when executed using vscode’s test discovery function, it fails to import unless the project is installed as a package first (i.e. by running python setup.py install --record record.txt
)
Steps to reproduce:
- Clone this sample repository: https://github.com/RobTheFiveNine/vscode-python-bug
- Create a venv:
virtualenv venv -p python3
- Activate it:
source venv/bin/activate
- Install pytest:
pip install pytest
- In a terminal, to verify pytest works, run
python -m pytest
in the project directory - To verify it doesn’t work in vscode, open vscode and configure the Python extension to use pytest, see that test discovery fails
- Open the output tab and view the Python Test Log section to see the errors
- After seeing it fail, run
python setup.py install --record record.txt
to install the project as a package in the venv, and then click on the tests label in the status bar or relaunch vscode, to find that vscode now discovers the tests successfully
Logs
python <path_to_home>/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir <path_to_project> -s --cache-clear -o junit_family=xunit1
Test Discovery failed:
Error: ============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: <path_to_project>
collected 0 items / 4 errors
==================================== ERRORS ====================================
__________________ ERROR collecting <path_to_test_file> ___________________
ImportError while importing test module '<path_to_test_file> '.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
<path_to_test_file>:5: in <module>
import <module_within_project>
E ImportError: cannot import name '<module_name>'
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
VSCode pytest test discovery fails - python - Stack Overflow
It seems that when testing is universally enabled for all python projects and it fails to discover tests at the beginning, it fails...
Read more >Package installation issues | PyCharm Documentation
One of the possible failure cases occurs when the target package is not available in the repositories supported by the Conda package manager....
Read more >discover - PyPI
If test discovery imports a module from the wrong location (usually because the module is globally installed and the user is expecting to...
Read more >Test namespace packages discover fails to load module.
Hello friends. Test discovery with namespaced packages fails when selecting a subpackage because of the way unittest discover evaluates paths.
Read more >Unittest: Fail if zero tests were discovered - Ideas
If tests move between versions, or infrastructure changes, we could go from successfully running a package's test suite to not running any ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mahmoudajawad the Python code is in https://github.com/microsoft/vscode-python/tree/master/pythonFiles/testing_tools, the TS code is in https://github.com/microsoft/vscode-python/tree/master/src/client/testing/pytest
@kimadeline just tried your suggestion of adding
tests/__init__.py
and it seemed to fix the discovery in vscode!