The Python for Visual Studio Code extension supports running and debugging unit tests. However, the documentation on how to get that working exactly with the nosetest
runner is a bit light. Below is a working solution to allow debugging of nose tests, as a reference to future self and others who may want to set this up.
Note that in theory, the python extension should also allow running of single tests or files by clicking links above your tests.
However, I’ve found that this tends to break in more complex projects and tests will not be discovered by the extension. As far as I can tell this seems to be an issue relating to nose
specifically. See my issue report on github.
Setup ‘launchpad’
With that out of the way, let’s set up the tests. First, create a file run_nose.py
with the following content:
import nose
nose.run()
I placed this inside .vscode
so it’s out of the way. This is purely a launch pad for the tests.
Configure launch.json
Go to the debug screen (⌘-⇧-D) and click the little gear button at the top to bring up the launch.json
file with the run configurations.
In this file, add a new section called Nose
as follows:
{
"name": "Nose",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${workspaceRoot}/.vscode/run_nose.py",
"cwd": "${workspaceRoot}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
],
"args": [
"${file}", // currently selected file
"-v", // additional nose parameters –
"-s" // tweak to your liking
]
},
And that’s it - now you can run the currently active file by clicking the green ‘debug’ button or by hitting F5.
Virtual environments
Note that if you are running in a virtual environment, you’ll want to adapt pythonPath
to point to that environment, either here in launch.json
or in the project’s settings.json
(which will be used in launch.json
if set). For example:
"pythonPath": "${workspaceRoot}/venv/bin/python",
Environment variables
Your tests may also rely on environment variables being set. To set them up, add an env
dictionary to the configuration:
"env": {
"APP_SETTINGS": "${workspaceRoot}/config/local.cfg"
},