Repository navigation
python.testing.cwd ignored when discovering or running tests #8678
Description
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 How to update the language server to the latest stable version #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_somethingor tests insome_test.pyfile. - 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.pywould be root test. I mean thattests/pywould be omitted in the test explorer since I have"python.testing.cwd": "tests/py"in my settings.
Currently directoriestests/pyare 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.cwdand 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.

Activity
brettcannon commentedon Nov 21, 2019
Thanks for the bug report! We just wanted to quickly acknowledge we received it and we will triage this as soon as we can.
kimadeline commentedon Nov 22, 2019
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.cwdinto 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.cwdin the list of pytest arguments, let me know if the following works for you:{ "python.pythonPath": "/path/to/python", "python.testing.pytestArgs": [ "./tests/py", "-v", "-s", "--log-cli-level", "INFO", ], "python.testing.unittestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.pytestEnabled": true }[-]Wrong python.testing.cwd setting handling[/-][+]python.testing.cwd ignored when running tests using pytest[/+][-]python.testing.cwd ignored when running tests using pytest[/-][+]python.testing.cwd ignored when discovering or running tests using pytest[/+]29 remaining items
twsl commentedon Sep 21, 2021
Karthik Nadig (@karthiknadig) great to see that you took over.
karthiknadig commentedon Sep 24, 2021
Merged a fix for this just now to main. Fix is essentially what #8678 (comment) said. If there is
--rootdirinpython.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.
botsman commentedon Sep 24, 2021
Works for me
Had to restart VS Code in order tests to appear in the sidebar without being nested inside
testsdirectorydanstreeter commentedon Sep 24, 2021
(Apologies if this is a rediculously silly question...)
Can I install the aformentioned vsix extension on my day-to-day working VSCode instance without risk of completely breakign it without any way back?!
Can it be uninstalled once installed when this fix reaches launch?
karthiknadig commentedon Sep 24, 2021
Dan Streeter (@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.
danstreeter commentedon Sep 24, 2021
Thanks Karthik Nadig (@karthiknadig) , I've got it installed in my main 'day-to-day' instance however still not quite working for me...
Using the default provided config after 'configuring tests', I end up with the following in my settings file:
This matches my project layout, as all tests are stored in the
./testsdirectory with all actual code in./srcRunning my tests with my normal workflow still works fine by running
make testwhich actually runs:export PYTHONPATH=./src pipenv run python -m pytest testsWhen performing discovery with the above config, I get the following in the Python output:
I also get the following warning:
Also, if I add any
--rootdirto the config as follows:The error change as 'expected' with the root directory and trys to target a path on root:
Why is it trying to target root?
danstreeter commentedon Sep 24, 2021
OOH OOH OOH - I FIXED IT!!!
I needed an absolute path in the rootdir... Using my pseudo, sanitised example above - this now works!
--- Update ---
This is much nicer:
karthiknadig commentedon Sep 24, 2021
Dan Streeter (@danstreeter) can you try:
danstreeter commentedon Sep 24, 2021
Doing the above gives:
karthiknadig commentedon Sep 24, 2021
Dan Streeter (@danstreeter) Are you missing the '.' before the './tests'? Just checking because '/tests' will not work.
danstreeter commentedon Sep 24, 2021
Definately there:

The config in the last part of my previous post up a bit (#8678 (comment)) works perfect though.
karthiknadig commentedon Sep 24, 2021
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
cwdgets set to `` (empty) string instead of.which seems to be the problem here. But that is unrelated to this.