New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature suggestion: run Django unittests #73
Feature suggestion: run Django unittests #73
Comments
From @guhuajun on January 19, 2017 6:51 I did something like following. I think it's supported now. |
From @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. |
From @guhuajun on January 20, 2017 0:22 https://github.com/DonJayamanne/pythonVSCode/wiki/unitest-(Standard-Unit-testing)-Framework#test-discovery Yes, missing DJANGO_SETTINGS_MODULE is annoying. I encountered this issue when writing tests for a django-cms project. |
@jvaesteves Will be closing this issue as @guhuajun has a solution. |
@jvaesteves please confirm @guhuajun suggestion works, so I can close this issue. |
From @jvaesteves on February 2, 2017 10:46 No, it didn't. |
From @james-perretta on March 5, 2017 4:35 It looks to me like the suggestion from @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. 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:
Thoughts? |
From @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 |
@PlugaruT Environment variables can be set for the workspace. Just create a file named |
Any update here?, I have a bunch of tests file, I'm not sure where to put |
You don't need to configure DJANGO_SETTINGS_MODULE if you use the option: "--settings < filesetting >" Just saying because maybe we can make thah unittest plugin doesn't need env variable, just tell him the setting and if not, use default. And just one question about my closed issue. My problem is not implementation of new feature for django. But tests are not DISCOVERED at all. I could make them run at least in part, but if it doesn't discover them I can't. When I use |
Any decision on this? This is somewhat of a small ask, but it will make dev work better for those who use django and not pytest/nose/etc. |
@yardensachs not at the moment (we're tied up with other things that impact all Python developers). If anyone wants to submit a PR to introduce a fix for this we would happily review it. |
I have finally figured out a way to make unittests work with VSCode and Django with only one python file and without any additional configuration or setup. VSCode test runner does test discovery every time, regardless of the amount of tests that are about be run. This is only tested on Ubuntu, also you must update filepaths to reflect your environment.
"""
Hacks to make unittest work desirably.
VSCode + Python + Django + unittest integration is currently broken.
Django tests can be run using `python3 manage.py test` but this is not enough as VSCode unittest
runner only uses `python3 -m unittest`.
The purpose of this file is to simulate what the manage script is doing before any tests are run.
The trick is to make this load first when unittest does test discovery, hence the path `_unittest`.
This clearly cannot work when directly pointing to a test since test discovery is not used,
for example `python3 -m unittest server.component.test`.
However VSCode points to target tests differently and discovery still gets run making this work.
This all hopefully becomes obsolete if Django support is added to VSCode Python.
The issue: https://github.com/microsoft/vscode-python/issues/73
"""
import inspect
import os
import django
from django.conf import settings
from django.test.utils import get_runner
def _check_stack() -> bool:
"""
Dig stack for the filepath of the script that imports this file.
Return True if improting and invoking scripts are as expected.
"""
# Skip first two calls which are this module and this function
for frame in inspect.stack()[2:]:
# Skip internal libraries which start with <
if frame.filename[0] != "<":
importing_script = frame.filename
break
else:
raise RuntimeError("Importing script not found")
# Unittest loader should be used
if importing_script != "/usr/lib/python3.11/unittest/loader.py":
return False
# Django manage script uses the same loader, so check for that as well and skip if used
first_invoking_script = inspect.stack()[-1].filename
if first_invoking_script == "/workspace/backend/manage.py":
return False
return True
# This script should only be imported, so ignore direct execute
if __name__ == "__main__":
pass
# Only allow this hack to work with unittest loader
elif _check_stack():
# Setup Django for testing
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.testing")
django.setup()
settings.ALLOWED_HOSTS += ["testserver"]
# Setup test database for testing
get_runner(settings)(interactive=False).setup_databases() |
Hi. I raised this on mastodon, and @brettcannon suggested to post here. I'm trying work out what we're missing from Django in order for this to work. The I asked:
…to which Brett pointed out that you're calling Any insight welcomed. Thanks. |
maybe it would be possible to patch the extension so it could check if a |
on a new, minimal django proect (polls app from tutorial), so the vscode-python plugin calls its own disovery script: unittest_discovery.py settings.json looks like this: {
"python.testing.unittestArgs": [
"-v",
"-p",
"test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
} full output (vscode > output > python)
similarly as @jvaesteves posted on January 19, 2017 12:13
|
Looping in @eleanorjboyd and @karthiknadig to look into the questions that @carltongibson is asking. |
By using this fork of
this is working by adding the following code to DON'T TRY AT HOME from pathlib import Path
from ast import literal_eval
if Path(start_dir + "/manage.py").is_file():
with open(start_dir + "/manage.py", "r") as management_file:
contents = management_file.readlines()
if any(True for line in contents if line.strip().replace('"""', '') == "Django\'s command-line utility for administrative tasks."):
print("django management file found!")
for line in contents:
if line.strip().startswith("os.environ.setdefault"):
try:
literal_eval(line.strip().replace('os.environ.setdefault("DJANGO_SETTINGS_MODULE",', "", 1))
except:
pass
else:
eval(line.strip()) # this is the not recommended part!!!
try:
import django
django.setup()
except ModuleNotFoundError:
pass |
I have submitted a new pull request
|
Replying to the recent comments:
I didn't look at the details of the workarounds here but, just from a Django perspective, calling |
@carltongibson I'm doing my best to make the changes more reliable and stable.(a new commit has been published) |
Hi, we have solved this problem in our project. import django
from django.apps import apps
from django.test.utils import setup_databases, setup_test_environment
if not apps.ready:
django.setup() # Normal Django setup
setup_test_environment() # This does a lot of stuff inside Django tests
# The next one is very important: it creates test databases and changes settings.DATABASES to point to them
# otherwise tests will run against live database.
setup_databases(verbosity=1, interactive=False, keepdb=True)
# keepdb probably should be a setting inside vscode.
# Our project takes an hour to run migrations from scratch, so we need keepdb,
# but normally no one wants to keep test databases around. As a note if anyone is planning to do it before it is implemented in the extension. |
In the extension, it should be sufficient to do once this before kicking off the test discovery… 🤔 |
Hello! I have begun to review this question of what VS Code would need to get Django test discovery and run working. So far I had a few questions that came up during exploring documentation and your repo. It looks like the Next I saw that the method For running tests, I see the method Generally I want to understand the key differences between Django tests and Unittests and if the Django test objects can just be stored as test_ids/labels (these seems to be paths) and Thank you and I look forward to working with you all! |
Hi @eleanorjboyd. Thanks for picking this up. It would be very exciting to get this working! 🤩
Naïvely, the basics are setting the If you look at the ManagementUtility class (and the execute_from_command_line helper) — I think you'd have most of what you're asking for there. (See also though the call_command function (docs).) The However you need the results... 🤔
So
At a high-level, I'd say we (cough) just wrap Generally labels are import paths to tests:
Hopefully the Schematically, I would think something like this would be Pretty Close™:
Does that give you the guidance you need? Let me know! I don't know my way around the extension, but happy to look at a branch if you
You too! Thanks again. 💃 |
Hello @carltongibson, great to meet you! Thank you for your in-depth reply! I am currently thinking on the next steps and talking with the team so I will get back to you soon. Your answers are extremely helpful in narrowing down what is needed and how to create compatibility. I am excited about the shared types with unittest and how this can make the integration more seamless. Thanks! |
So.. any update on this? I tried setting up our company project with VSCode and the extension is still unable to find django test modules. |
Apparently the next step is here and we need votes to get things going. If the 300+ people who liked this post could make their voices heard. |
@khamaileon Nice. Good spot. I didn't even know about that other issue. |
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 try:
import django
django.setup()
except:
pass 2- Django Project Requirement DJANGO_SETTINGS_MODULE=my_django_project_name.settings |
@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. 🤷♂️ |
hi @khamaileon, |
Yet I'm able to launch it with the manage command inside vscode and outside (from the src folder). {
// 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
}
]
} |
From @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
The text was updated successfully, but these errors were encountered: