vscode-python: python.testing.cwd ignored when discovering or running tests
Environment data
- VS Code version: 1.40.1
- Extension version (available under the Extensions sidebar): 2019.11.49689
- OS and version: MacOS Catalina 10.15.1
- Python version (& distribution if applicable, e.g. Anaconda): 3.7.4
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
- Relevant/affected Python packages and their versions: pytest==5.3.0
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"
set to; more info #3977): Jedi
Expected behaviour
Tests are displayed in the test explorer according to python.testing.cwd
setting and I am able to run single test or tests from single file
Actual behaviour
python.testing.cwd
is ignored for displaying tests and I am unable to run single test or tests from single file because I am getting an error file not found
.
Steps to reproduce:
[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]
Sample project: https://github.com/pbotsman/sample_project
- Open project
- Open tests/py/some_test.py to start exploring tests
- Try to execute single test
test_something
or tests insome_test.py
file. - Get the error ERROR: file not found: ./tests/py/some_test.py::test_something (Sometimes in order to reproduce the problem I had to relaunch my VSCode)
Logs
Output for Python
in the Output
panel (View
→Output
, change the drop-down the upper-right of the Output
panel to Python
)
> ~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-4746yxdv7Lw3muwB.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something
cwd: ~/projects/sample_project/tests/py
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
)
console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: getActivatedEnvironmentVariables, Class name = b, completed in 1ms, Arg 1: <Uri:/Users/pavel/projects/sample_project>, Arg 2: undefined, Arg 3: undefined
console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: > ~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-5598WI4lpp4kFEsC.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something
console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: cwd: ~/projects/sample_project/tests/py
Additional info

Looks like something has changed in the last release:
- Previously tests would be displayed in the test explorer just as
some_test.py
would be root test. I mean thattests/py
would be omitted in the test explorer since I have"python.testing.cwd": "tests/py"
in my settings. Currently directoriestests/py
are displayed in the test explorer even though I have this setting. - I can’t run single test because I am always getting an error
file not found
. It looks like something is wrong with the file paths. When the command~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-5598WI4lpp4kFEsC.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something
is called my current working directory is set topython.testing.cwd
and that is why I am getting the error. So in the command mentioned above valid file path should be./some_test.py::test_something
, not./tests/py/some_test.py::test_something
.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 66
- Comments: 37 (9 by maintainers)
Disclaimer: I have about zero days experience with Typescript, and the vscode-python extension… however I think the issue might have stemmed from this commit: “Force the pytest root dir to always be the workspace root.” https://github.com/microsoft/vscode-python/commit/9fe2a76322236bf752fe36e4e4230b2d2036ddff
Hi @pbotsman 👋 Thank you for reaching out and thoroughly filling out the issue template 👍
I can confirm that I could reproduce your issue in the latest release of the extension (2019.11.49689), and that it doesn’t happen in the previous release (2019.10.44104).
Seems like we don’t take
python.testing.cwd
into account anymore when discovering tests, or running single tests using pytest.A possible workaround while waiting for a fix would be to move the content of
python.testing.cwd
in the list of pytest arguments, let me know if the following works for you:@luabud
As @danstreeter and I pointed out before, the issue is that this extension forces the
--rootdir
parameter to pytest to be set to theworkspaceFolder
.The fix is to perhaps have this as the default, but allow users to override the argument in their settings.
The problem was introduced here https://github.com/microsoft/vscode-python/commit/9fe2a76322236bf752fe36e4e4230b2d2036ddff#
FWIW, I just stumbled upon this, and would love to see a fix that changes the working directory to
python.testing.cwd
before runningpytest
.Hey folks, sorry for the frustration with this issue. We’re planning to tackle this and many other testing bugs once we migrate into VS Code’s testing API (https://github.com/microsoft/vscode-python/issues/15750), but we don’t have an ETA for that yet.
In the mean time, adding
"--rootdir","path_to_desired_cwd"
topytestArgs
should give a workaround for this issue. So for the sample in the opening comment of this issue, it would look like this:@flyte you had mentioned setting this up didn’t work for you, would you mind sharing your project structure so we can see what’s going on?
Hi, I can confirm that this issue does not appear to have been resolved and I have similar behaviour.
Any progress or workarounds for this issue? Setting option
"python.testing.pytestArgs": ["--rootdir=path/to/root"]
doesn’t help.The only relevant settings are this:
In my project all my tests are under a unit_tests folder. It reads files relative to that folder but the command used in Test Log
discover pytest -- --rootdir f:\code\cloverleaf\web -s unit_tests
, not--rootdir f:\code\cloverleaf\web\unit_tests
. I’ve also tried without${workspaceFolder}
but have had no luck.Merged a fix for this just now to main. Fix is essentially what https://github.com/microsoft/vscode-python/issues/8678#issuecomment-808210852 said. If there is
--rootdir
inpython.testing.pytestArgs
, we won’t override it. so for the sample repo, the settings change would be:That should work:
Try this out with this build https://github.com/microsoft/vscode-python/suites/3858667759/artifacts/95863817 if that doesn’t work please provide repro steps.
I can confirm that it affects
unittest
tests as well. Interestingly, when running the whole test-suite (“Run All Tests”)cwd
is correctly set to the folder containing the tests files (even without settingpython.testing.cwd
at all). When I run a single test,cwd
is set to the workspace-root, regardless of thepython.testing.cwd
setting.I have also noticed after setting
rootdir
inpython.testing.pytestArgs
in eitheror
The unit test results (pass or fail) are no longer updated in the TEST explorer.
Environments
This bug actually forced me to switch to Pycharm. I experienced the bug using unittest. I hope it get’s resolved soon.
Hi @kimadeline,
Thanks for the workaround. This is exactly what I did 👍
I see. Thanks for the input. This should work for most case with this:
I will tweak it, it seems like for some reason, the
cwd
gets set to `` (empty) string instead of.
which seems to be the problem here. But that is unrelated to this.@danstreeter can you try:
@danstreeter You can install this and uninstall to restore your VS Code to stable extension or any extension version.
Here is how you would do it:
Note, you can also click on the ‘Gear’ icon next to the extension in the extensions tab to install a specific version using “Install Another version…”
If you want to try this out is complete isolation, you could install VS Code Insiders (which can install side by side with your stable one) and install this vsix there and test it out. then uninstall the whole thing. This way it won’t touch your day-to-day VS Code instance.
Works for me
Had to restart VS Code in order tests to appear in the sidebar without being nested inside
tests
directory