Debugging unit tests in a sub directory #11587
Description
I'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.
Activity
karrtikr commentedon May 5, 2020
Unittest args require a
-sflag to work. However the following didn't work becauseBecause
./srcinpython.testing.unittestArgsis resolved to./src/srcas cwd is set to./src, and as that directory doesn't exist, discovering tests fail.You can simply use the full path to specify the test location, or better use the variable
${workspaceFolder}Let me know if that works.
martingbrown commentedon May 6, 2020
When you say "the following didn't work", it did work to a degree. With the settings like the following, it discovers the tests and runs them. But the tests are not run with the
.\srcdirectory as the working directory. And also debugging tests doesn't seem to work.I changed my settings as you suggested to the following but it then fails to discover the tests:
karrtikr commentedon May 7, 2020
Hey Martin Brown (@martingbrown) , I am able to spot multiple issues here:
cwdfrom 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
cwdis confusing, so we'll address these as part of #8339.The above alone should get you working, but I found some more issues:
${workspaceFolder}&.in${workspaceFolder}/src&./srcis not being resolved correctly (1), this is an issue (created${workspaceFolder}&.not being resolved in settings in some cases #11671). For now, please try using the full path to the source directory instead,<full path to workspace folder>/srcFor 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.pythonPathand other settings. Created issue Backslashes doesn't work when specifying the paths withpython.testing.unittestArgs#11670I think the cwd is not being set for you because of (2). So try using- This didn't work, I'll open up a new issue"python.testing.cwd": "./src"instead of"python.testing.cwd": ".\\src". Running the tests using the play button should work after that.Seems like
python.testing.cwdis not working anymore. Created issuepython.testing.cwddoes not work properly #11669Apologies for so many issues, let me know if this helps. Yours reminded me of #7176, where the guy used the above to solve his issues.
martingbrown commentedon May 7, 2020
I have now the settings.json set like this:
And I also have this in launch.json
And now it all seems to be working.
Thanks for that. I appreiate the effort. If I had time I'd try and crack open the source code and try to help you out.
karrtikr commentedon May 7, 2020
No worries. But does running tests using the green play button work? From my understanding, as
python.testing.cwddon't work, it shouldn't work either.