You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am working on a repository with unit tests in a Tests subfolder, and the specific test needs to read a file from a subdirectory of Tests. The read fails with 'file not found', and when I print os.getcwd() before the read it reports ${workspaceFolder} regardless of what I put into python.unittest.cwd. I am using Python unittest (python.unitTest.unittestEnabled is true) framework.
Apologies if I am not understanding how this is supposed to work! Thanks
Extension version: 2019.3.6558
VS Code version: Code 1.33.1 (51b0b28134d51361cf996d2f0a1c698247aeabd8, 2019-04-11T08:22:55.268Z)
OS version: Darwin x64 18.5.0
System Info
Item
Value
CPUs
Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 x 3200)
Setting python.testing.cwd to ${workspaceFolder}/api then pytest reports the rootdir as the ${workspaceFolder} same with relative paths, settings it to an absolute path will work.
I recently investigated this probably briefly and found that anytime that python.testing.cwd is changed, the window needs to be reloaded for the setting to be picked up. With that method, I have found that it works as expected.
I just realized that python.testing.cwd doesn't seem to do what I expected it to do.
It looks to my like it is used as the CWD for the test discovery too. I thought it would only be used for running the tests.
Can anyone confirm this?
And to make matters worse: Doesn't seem to get used when debugging a unittest.
It does work for test discovery too, which I'm pretty sure is intentional lest code would likely crash on discovery and not when running (or vice-versa) as you'd be running in different environments with different python paths.
The second issue you are referencing can be found here: #8678
Funny, I tested the reloading and that did indeed fix running the tests through the Test Explorer UI and Python Test explorer (which use the python settings) but not through the normal python extension, it finds the tests, but then running the tests gives me a file or directory not found with a path relative to the workspace folder but not the testing cwd.
What I mean is the vscode-python test explorer, it doesn't run the tests correctly, but the Test Explorer UI (which is now deprecated but you can still use) with the python text explorer extension actually works for me, so they must be doing something right that the python extension is doing wrong, because it uses the same settings.
Hello! I have reviewed this issue and given the architectural changes of the testing rewrite, this issue should no longer exist on the rewrite. Therefore I am going to close this issue but please comment or open a new issue if you are still seeing a problem when you try this yourself!
To use the rewrite yourself just add ”python.experiments.optInto": ["pythonTestAdapter"] to your user settings. You can confirm you have the rewrite enabled by setting your log level to trace, via the Developer: Set Log Level command in the command palette. Then check to see if Experiment 'pythonTestAdapter' is active is in your python logs.
Activity
DonJayamanne commentedon Apr 15, 2019
What version of python are you using?
And are there any errors logged in the
Python Testoutput panel.rob-miller commentedon Apr 15, 2019
Python 3.7.3
If I hardcode the absolute dir for the file read there are no errors in the test output panel:
If I try to read from the subdir as intended (with python.unittests.cwd set to the absolute path or ${workspaceFolder}/Tests), I get:
DonJayamanne commentedon Apr 18, 2019
I'm unable to replicate this issue at my end, please could you:
settings.jsonas wellhvdklauw commentedon May 2, 2019
I have the same thing with Pytest.
Setting
python.testing.cwdto${workspaceFolder}/apithen pytest reports the rootdir as the${workspaceFolder}same with relative paths, settings it to an absolute path will work.DonJayamanne commentedon May 15, 2019
Got it, thanks
8 remaining items
cebor commentedon Dec 3, 2020
I also have this issue with python.testing.unittestEnabled.
Mutantpenguin commentedon Sep 8, 2021
Having the same problem python.testing.unittestEnabled.
When I enter
${workspaceFolder}/../..intopython.testing.cwdit has no effect whatsoever.os.getcwd()always returns my${workspaceFolder}jaron-l commentedon Sep 8, 2021
I recently investigated this probably briefly and found that anytime that python.testing.cwd is changed, the window needs to be reloaded for the setting to be picked up. With that method, I have found that it works as expected.
Mutantpenguin commentedon Sep 10, 2021
Jaron (@jaron-l) Thank you very much, that worked.
I just realized that
python.testing.cwddoesn't seem to do what I expected it to do.It looks to my like it is used as the CWD for the test discovery too. I thought it would only be used for running the tests.
Can anyone confirm this?
And to make matters worse: Doesn't seem to get used when debugging a unittest.
jaron-l commentedon Sep 10, 2021
It does work for test discovery too, which I'm pretty sure is intentional lest code would likely crash on discovery and not when running (or vice-versa) as you'd be running in different environments with different python paths.
The second issue you are referencing can be found here: #8678
hvdklauw commentedon Sep 13, 2021
Funny, I tested the reloading and that did indeed fix running the tests through the Test Explorer UI and Python Test explorer (which use the python settings) but not through the normal python extension, it finds the tests, but then running the tests gives me a file or directory not found with a path relative to the workspace folder but not the testing cwd.
This is with pytest btw.
jaron-l commentedon Sep 13, 2021
Harro van der Klauw (@hvdklauw), what do you mean by "the normal python extension"? Are you talking about the CodeLens buttons? What's your workflow?
hvdklauw commentedon Sep 13, 2021
What I mean is the vscode-python test explorer, it doesn't run the tests correctly, but the Test Explorer UI (which is now deprecated but you can still use) with the python text explorer extension actually works for me, so they must be doing something right that the python extension is doing wrong, because it uses the same settings.
eleanorjboyd commentedon Oct 30, 2023
Hello! I have reviewed this issue and given the architectural changes of the testing rewrite, this issue should no longer exist on the rewrite. Therefore I am going to close this issue but please comment or open a new issue if you are still seeing a problem when you try this yourself!
To use the rewrite yourself just add
”python.experiments.optInto": ["pythonTestAdapter"]to your user settings. You can confirm you have the rewrite enabled by setting your log level to trace, via theDeveloper: Set Log Levelcommand in the command palette. Then check to see ifExperiment 'pythonTestAdapter' is activeis in your python logs.Thanks!