I'm trying to debug my first django test, but VSCode is returning: Ran 0 tests in 0.000s. On the other hand, when I use an integrated git bash (I'm using a windows OS) in VSCode terminals or externally, it returns: Ran 1 test in 0.0014s. It also echoes FAILED and a traceback for a valid error within the test.
My vscode launch.json:
{
...
"configurations": [
...
{
"name": "Django Tests",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\testprojectname\\manage.py",
"args": [
"test"
],
"django": true,
}
]
}
My vscode settings.json:
{
"python.pythonPath": "C:\\Users\\user\\.virtualenvs\\backend-Vg-KBKTA\\Scripts\\python.exe",
"python.testing.pytestEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"testprojectname"
]
}
So far, I've tried:
This is what the test currently looks like
class Test_Contacts(unittest.TestCase):
def test_create_contact_message(self):
"""Create a new contact message."""
url = reverse('message-list')
data = {
'first_name': 'DabApps',
'last_name': 'jack',
'email': 'giehogho24h@gmo8y9tgail.com',
'message': 'hi, how are you?',
'message_type': 1
}
# this is just random code to cause an error
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Message.objects.count(), 1)
self.assertEqual(Message.objects.get().name, 'DabApps')
I was not able to solve the original problem, but I found a good workaround. I can now see 'debug test' above the test code. I'm not sure why, but it may be because I installed pytest-django or the extension python test explorer ui.
python test explorer ui story
pytest-django story
[pytest]
DJANGO_SETTINGS_MODULE = testprojectname.settings
python_files = tests.py test_*.py *_tests.py
now I noticed that the test I made in tests.py had clickable words for debugging above it. Clicking debug test took a while to initiate, but it allowed the code to stop at the red dot VS code breakpoints. clickable words for debugging above test
Although that was all I needed, I also noticed that I can now run the tests from the VS code test explorer, so I no longer have to use pipenv shell in the bash to run all the tests. VS code test explorer found test
Here is generic way to get Django tests to run with full vscode support
test*.py
from django.test import TestCase
class views(TestCase):
@classmethod
def setUpClass(cls):
import django
django.setup()
def test_something(self,):
from user.model import something
...
Any functions you want to import have to be imported inside the test case (like shown). The setUpClass runs before the test class is setup and will setup your django project. Once it's setup you can import functions inside the test methods. If you try to import models/views at the top of your script, it will raise an exception since django isn't setup. If you have any other preinitialization that needs to run for your django project to work, run it inside setUpClass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.