I'm using python 3.6.5, django 2.0.3 and VSCode 1.21.1.
In my project I have couple test files (test_*.py
) in the following location app/tests/
. I'm able to simply run them using cmd: python manage.py test app.tests
. I wanted to use VSCode tests debugging so I decided to configure built-in unittest handler.
First I edited workspace settings.json
file adding following code:
"python.unitTest.unittestEnabled": true,
"python.unitTest.unittestArgs": [
"-s",
"app.tests",
"test_*.py",
It turned out that __init__.py
files are required both for app
and tests
folders in order to detect files containing tests. I recently switched to Python 3 and was really happy __init__.py
files are no longer a requirement (PEP420). But yeah, I had to add them just to make test detection work.
Then I found out that without calling django.setup()
and setting DJANGO_SETTINGS_MODULE
in the first place I still won't be able to run my tests. Therefore I had to add the following code at the beginning of every test file:
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyAwesomeWebApp.settings')
django.setup()
Is it all really required or there is some other way to configure unittest in VSCode?
I had the same issue. So, I have manage to run django test in VS with next configs in debug configurations.
{
"name": "Python: Django Test",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/manage.py",
"cwd": "${workspaceRoot}",
"args": [
"test",
],
"env": {},
"envFile": "${workspaceFolder}/.env",
},
I can use breakpoints and explore test variables state. So I hope it will help you or anyone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With