Feature suggestion: run Django unittests #73
Copy link
Copy link
Closed
Labels
area-testingfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds PRReady to be worked onReady to be worked on
Description
From João Esteves (@jvaesteves) on January 16, 2017 22:28
Django unittest extends from unittests, but you can't run as the former on this extension. For this, you need to "manage.py test", but it would be awesome if there were those same shortcuts.
Copied from original issue: DonJayamanne/pythonVSCode#648
Reacted by Kaleb Hornsby, Fabian Weisshaar, Remigiusz Dymecki, Diego, Ignas Kiela, Missing Link, Christopher Lansing, Michael Hays, Sergey Buchko, Japillow and 325 more
Metadata
Metadata
Assignees
Labels
area-testingfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds PRReady to be worked onReady to be worked on
Activity
DonJayamanne commentedon Nov 13, 2017
From greggu (@guhuajun) on January 19, 2017 6:51
I did something like following. I think it's supported now.
DonJayamanne commentedon Nov 13, 2017
From João Esteves (@jvaesteves) on January 19, 2017 12:13
I tried your suggestion, copying the same parameters on the settings.json file, but running the "Run Unit Test Method..." command still doesn't work. The Output Console gives me the following message:
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
And then, when I configure this environment variable to my global_settings.py path, the message change to: ImportError: Import by filename is not supported.
DonJayamanne commentedon Nov 13, 2017
From greggu (@guhuajun) on January 20, 2017 0:22
https://github.com/DonJayamanne/pythonVSCode/wiki/unitest-(Standard-Unit-testing)-Framework#test-discovery
Please try to place a
__init__.py.Yes, missing DJANGO_SETTINGS_MODULE is annoying. I encountered this issue when writing tests for a django-cms project.
DonJayamanne commentedon Nov 13, 2017
João Esteves (@jvaesteves) Will be closing this issue as greggu (@guhuajun) has a solution.
DonJayamanne commentedon Nov 13, 2017
João Esteves (@jvaesteves) please confirm greggu (@guhuajun) suggestion works, so I can close this issue.
DonJayamanne commentedon Nov 13, 2017
From João Esteves (@jvaesteves) on February 2, 2017 10:46
No, it didn't.
DonJayamanne commentedon Nov 13, 2017
From James Perretta (@james-perretta) on March 5, 2017 4:35
It looks to me like the suggestion from greggu (@guhuajun) gets us part of the way there for running Django test cases. Unless I'm mistaken, you can't really use unittest to run tests for any serious Django project.
When you run
python manage.py test, it first does general setup (figuring out which settings module to use and calling django.setup()). After that, it hands the rest off to a test-runner class that does even more important things like creating and destroying the databases that the test cases need to operate on.I did a bit of digging, and although I'm not particularly familiar with the implementation details of your plugin, I think this is a starter summary of what it would require to add this:
manage.py testoptions should always be passed, such as --no-inputThoughts?
DonJayamanne commentedon Nov 13, 2017
From Tudor Plugaru (@PlugaruT) on October 3, 2017 11:15
Because I have different settings files for each environment (production, local, test), I can't run tests because with this plugin because
DJANGO_SETTINGS_MODULEis not set when trying to run the tests. I would be nice if I could set this env variable in in theworkspace.jsonor somewhere else.DonJayamanne commentedon Nov 13, 2017
Tudor Plugaru (@PlugaruT) Environment variables can be set for the workspace. Just create a file named
.envin your workspace root and add the necessary variables in there.69 remaining items
mh-firouzjah commentedon Feb 5, 2024
As long as the proposed procedure is taking approval, I have tested a new idea, and it worked well for me. I am awaiting feedback and information on whether it works properly for you and others, or if there are any issues.
1- Vscode Python Extension Requirement
edit the
__init__.pymodule in~/.vscode/extensions/ms-python.python-VERSION/pythonFiles/unittestadapter/__init__.pyand add the code below:2- Django Project Requirement
create a
.envfile and inside of it declareDJANGO_SETTINGS_MODULE. an example is:khamaileon commentedon Feb 6, 2024
Mahdi (@mh-firouzjah) I tried but it didn't work out.
Here's my vscode settings:
{ "editor.codeActionsOnSave": { "source.organizeImports": "explicit" }, "editor.formatOnSave": true, "python.defaultInterpreterPath": "~/.virtualenvs/acme-api/bin/python", "python.envFile": "${workspaceFolder}/.env.local", "editor.rulers": [100], "python.testing.unittestArgs": ["-v", "-s", "./src", "-p", "test*.py"], "python.testing.pytestEnabled": false, "python.testing.unittestEnabled": true }And here's the kind of output I got:
DJANGO_SETTINGS_MODULE=project.settings is read correctly, otherwise I'm told it's necessary.
The problem may be that my code is in a src folder and not in the root. 🤷♂️
mh-firouzjah commentedon Feb 6, 2024
hi khamaileon,
yes, DJANGO_SETTINGS_MODULE is read. but for the error you got it seems that django.setup() had not been called at the beginning of the process.
being in a subdirectory could be the case but if you also have error when using manage.py check/runserver/...
otherwise I don't think thats the problem here.
khamaileon commentedon Feb 8, 2024
Yet I'm able to launch it with the manage command inside vscode and outside (from the src folder).
Here is my launch.json config file.
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Django", "type": "python", "request": "launch", "program": "${workspaceFolder}/src/manage.py", "args": ["runserver"], "django": true, "envFile": "${workspaceFolder}/.env.local", "justMyCode": true } ] }mardukbp commentedon May 29, 2024
Thank you Mahdi (@mh-firouzjah)! Your solution works great :)
Django Test Compatibility (microsoft#23935)
eleanorjboyd commentedon Aug 15, 2024
Hello Everyone! The long requested ask is live!! Django tests are now supported in the VS Code python extension! Currently this is pre-release and would greatly appreciate Django users try this out and give feedback / submit bugs! (there will likely be bugs so I appeciate your patience as we work through them)
Steps:
v2024.13.2024081501or newerMANAGE_PY_PATH='path-string-to-manange.py-path'- below are steps specifically if you aren’t sure how to do this:MANAGE_PY_PATH='path-string-to-manange.py-path'to the file"python.envFile": "${workspaceFolder}/.env”to pick up this file as part of the environment (edit path as necessary if .env not at root)"python.testing.unittestEnabled": true,)"python.testing.unittestArgs": [], maybe the tests just aren’t being foundpython manage.py test --argto get the same in VS Code makeMANAGE_PY_PATH='./manage.py'"python.testing.unittestArgs": [--arg]and check the output as it should print the final command that is actually runpython: report issue..command, follow steps there and add your logs copied aboveThank you!
swebra commentedon Aug 15, 2024
Eleanor Boyd (@eleanorjboyd) just some quick spelling mistakes to save anyone copy pasting:
manange.pyshould be fixed tomanage.pyinMANAGE_PY_PATH=..., i.e. it should beMANAGE_PY_PATH='path-string-to-manage.py-path'") rather than a closing (curly) double quote (”)And some very initial feedback:
manage.pyseems to be called with a verbosity-vflag without providing a verbosity level, causing the test discovery to error out:This is reproducible against a generated Django v5.1 project created with
django-admin startproject mysiteas per the Django docs tutorial. Also note the-s ./mysiteargument, which also does not seem to be supported.LMK if you want me to flesh this out into a proper GH issue.
eleanorjboyd commentedon Aug 16, 2024
Hi Eric Claerhout (@swebra) thank you for trying it! Ill make those fixes to the instructions. So for the args, you should be able to go into
python.testings.unittestArgsin your workspace settings.json and remove all those args and just add the args you need for Django (not required just if you want a verbosity level or anything). The-vis a carry-over from unittest args but since they share the same setting for both we will have to update the default there. Thanks!swebra commentedon Aug 16, 2024
Ah of course. Test execution now works as expected in the generated project!
The only thing else of note there is a rogue error without any message during test discovery, just before the
Found X test(s)info log. Seem to happen both on manual discovery (shown) and automatic discovery, and I similarly see it in "real" (non-generated) projects:eleanorjboyd commentedon Aug 19, 2024
hm yes I see that error- ill see if I can track it down. Does it change the outcome of the discovery or just appears in the logs?
swebra commentedon Aug 19, 2024
Just appears in the logs as far as I can tell.