vscode-python: Tests not being discovered since 2019.4.0
Environment data
- VS Code version: 1.33.1
- Extension version (available under the Extensions sidebar): 2019.4.11987
- OS and version: Windows 10
- Python version (& distribution if applicable, e.g. Anaconda): 3.6.6 in my virtualenv
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
- Relevant/affected Python packages and their versions: tests?
Expected behaviour
Discover Python unit tests should pick up unit tests (excluding ones I want ignored), mark the unit tests in the source file with the ‘debug test|run test’ annotations, and running unit tests should only run my tests. This was working prior to yesterday.
Actual behaviour
Discover unit tests includes directories I’ve asked to be ignored in python.testing.pyTestArgs, unit test source files are not marked with annotations, and all tests run. Also I’m not seeing the results of Discover Unit Tests anywhere after run it.
Steps to reproduce:
- My workspace settings file has these settings:
``
Logs
Output for Python
in the Output
panel (View
→Output
, change the drop-down the upper-right of the Output
panel to Python
)
"python.linting.ignorePatterns": [
".vscode/*.py",
"**/site-packages/**/*.py",
"cps_python_packages",
"tests",
"context.py"
],
"python.linting.pylintArgs": [
"--rcfile",
"C:\\Users\\me\\source\\repos\\mason\\.pylintrc"
],
"python.testing.pyTestArgs": [
"--ignore=${workspaceFolder}/mason/cps_python_packages/cps_datamodel/tests"
],
and cps_python_packages shouldn’t be linted or tested, but is being linted and tested. I have no user settings affecting this.
Output from Console
under the Developer Tools
panel (toggle Developer Tools on under Help
; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging
)
None relevant so far as I can tell
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 17
- Comments: 52 (5 by maintainers)
I wanted to give some transparency about this whole situation and how we got here as I know some people are a bit frustrated at the moment with our pytest support has been fluctuating a bit over the last few releases.
We used to use a hack to discover tests in pytest and the pytest team specifically asked us to stop (and rightfully so) 😁 . Pytest didn’t have any stability guarantees for the hack we were using, hence why the last few releases there has been skew for what tests we discovered in the extension (totally our fault for using the hack BTW, it’s just the usual software issue of having the time to do the proper fix versus patching what we had).
And so in this release we finally got around to using pytest’s plug-in system for test discovery which is a more proper way. Unfortunately there seems to be a an issue yet again in terms of what pytest reports and what we used to discover using the hack (and I’m not placing blame on pytest, just that discovery is not universally broken, just that it’s hard to pin down). Add to that the fact that pytest is very flexible in its discovery and it makes finding reproducers a bit hard without repos we can clone (thanks to those providing such repos!). We did increase our testing of test discovery as well, so I would like to think this isn’t due to sloppiness on our part as much as the complexity of the whole situation as people’s testing setups are not always simple.
Regardless of everything I have said above, we’re obviously sorry this has caused trouble for folks and we are actively trying to figure out what’s going on.
Hi everyone:
Python Test Log
output panel. Please provide the details of the failure.Once again, we apologize for the inconvenience caused by this issue.
Seems like a bug in the latest version of VS Code Python extension. I had the same issue, then I downgraded the Python extension to 2019.3.6558 and then it works again. So we should go to our VS Code extensions list, select the Python extension and “Install another version…” from the setting of that extension.
@chkpnt when you look in the
settings.json
in the repo i pointed to, i specified the root directory as test directory -->python.testing.pyTestArgs = [ "." ]
.Anyway, i think it should not be such a hassle to discover tests, which are somewhere in the workspace folder tree - with or without
__init__.py
. I am wondering that this is somehow broken, because the same setup worked a few days before (maybe it has to do with python extension update?!)…As i said, when running pytest from the commandline from the root folder with the same commands from the
pyTestArgs
the tests are discovered without any problem - with and without__init__.py
. Within vscode tests are only discovered when i leave out the__init__.py
. For replication check my repo: https://github.com/xibalba01/vscode_python_ext_bug@r-beer i second that. More clear error messages would be helpful.
Strangely when i set up a clean repo i get not problems… since my repo/filebase where the error occured is largely crap i dont like to make it official 😃 But maybe it helps:
https://github.com/xibalba01/vscode_python_ext_bug
Some interesting thing happened while playing around with my repo: I figured out that the error did NOT occur in vscode when i leave out the
__init__.py
file! Tests are discovered successfully. I have to add, that an existing__init__.py
has no effect on manually running pytest! Hope it helps discovering the bug.I’m hitting this issue as well with my previously testing & working reference repo: https://github.com/noelbundick/azure-python-boilerplate
Repro steps on MacOS
python.unitTest.*
topython.testing.*
in settings.jsonTest discovery failed
errorRemediate
python.unitTest.*
againNote that tests are discovered and tests pass
@brettcannon @ericsnowcurrently
I believe that’s one of the problems. We’ve been using
-m
which adds current DIR into path, whereas the current speech doesn’t. Solution is to just add the workspace into the path.Tried the debug extension, the abbreviated error message is: “E ModuleNotFoundError: No module named ‘mylib’” ./mylib is one of module folder in the project root dir.
This is the same error I get when I run “pytest” in my root project dir from command line. My work around was to run the following instead: “PYTHONPATH=. pytest”
The fix is probably ensuring that PYTHONPATH includes the project root dir when you launch ‘pytest’.
I have found that removing all args, and leaving it as a blank list appears to work.
after update pytest and vscode are latest version,I met same issue with exit code 2
Test Discovery failed: Error: Traceback (most recent call last): File “c:\Users\wuguilin.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\run_adapter.py”, line 16, in <module> main(tool, cmd, subargs, toolargs) File “c:\Users\wuguilin.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter_main_.py”, line 90, in main parents, result = run(toolargs, **subargs) File “c:\Users\wuguilin.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter\pytest.py”, line 43, in discover raise Exception(‘pytest discovery failed (exit code {})’.format(ec)) Exception: pytest discovery failed (exit code 1)
my code
@DonJayamanne, unfortunately I can’t provide my repo. I tried to drag some modules and their companion tests into a fresh repo but VSCode/Python were well-behaved.
From Python output window after running VSCode in the original repo:
I have the same problem with pytest! I didn’t change anything to the test configuration and it worked fine yesterday. Environment data (vscode and python extension version) is more or less the same as from the OP except that i use Miniconda python v3.7.1.
Here are my workspace settings:
Additionally i get a large error message in the console, which could help you guys debug this error:
I would like to add that i love vscode and its capabilities - you are doing great work! Please fix this error, because i also need vscode and testing at work 😃