Debugging unit tests in a sub directory
See original GitHub issueI’m working on Windows 10 with VSCode and the Python plugin. These are all up to date as far as I can tell.
I’ve some unittest tests that sit in a subdirectory of my main working directory called .\src
. These in return load files in a second sub directory .\src\test\test.dat
. The tests simply refer to this as test\test.dat
as they assume the current directory is .\src
.
I’m having real trouble getting testing setup so that this works. I’ve been able to run these in the past but debugging a test wasn’t working. I changed my settings to:
"python.linting.pydocstyleEnabled": true,
"python.testing.unittestArgs": [
"-p",
"*test.py",
"-s",
"./src",
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
And debugging started working, but the tests don’t find the data file. Because the current working directory is just .\
. If I change the settings to this debugging stops working. Surprisingly though vscode can still find and run the tests, even though my reading of this is that it should be looking in .\src\src
which does not exist.
"python.testing.unittestArgs": [
"-p",
"*test.py",
"-s",
"./src",
],
"python.testing.cwd": ".\\src",
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
If I change the settings to this it can’t even discover the tests:
"python.testing.unittestArgs": [
"-p",
"*test.py"
],
"python.testing.cwd": ".\\src",
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
I’m now totally confused as to how to set this up so that running tests works and debugging tests works and they both find the test files.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top GitHub Comments
Hey @martingbrown , I am able to spot multiple issues here:
cwd
from the settingpython.testing.cwd
. Instead you’ve to create the following entry inlaunch.json
- I can confirm this works, so this should get your testing setupMultiple places to set
cwd
is confusing, so we’ll address these as part of https://github.com/microsoft/vscode-python/issues/8339.The above alone should get you working, but I found some more issues:
${workspaceFolder}
&.
in${workspaceFolder}/src
&./src
is not being resolved correctly (1), this is an issue (created https://github.com/microsoft/vscode-python/issues/11671). For now, please try using the full path to the source directory instead,<full path to workspace folder>/src
For instance the following worked for me
But surprisingly using backslashes is path (
C:\\Users\\karraj\\Desktop\\Desktop\\test_demo\\src
) is not working. That’s another issue (2). As it perfectly works withpython.pythonPath
and other settings. Created issue https://github.com/microsoft/vscode-python/issues/11670 ~I think the cwd is not being set for you because of (2). So try using"python.testing.cwd": "./src"
instead of"python.testing.cwd": ".\\src"
. Running the tests using the play button should work after that.~ - This didn’t work, I’ll open up a new issueSeems like
python.testing.cwd
is not working anymore. Created issue https://github.com/microsoft/vscode-python/issues/11669Apologies for so many issues, let me know if this helps. Yours reminded me of https://github.com/microsoft/vscode-python/issues/7176, where the guy used the above to solve his issues.
No worries. But does running tests using the green play button work? From my understanding, as
python.testing.cwd
don’t work, it shouldn’t work either.