3👍
Please consider the following checks:
-
you should have
__init__.py
in your test directory -
in vscode on test configuration use pytest framework
-
use:
pip install pytest-django
-
copy
pytest.ini
in the root with this content:
# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = <your-web-project-name>.settings (like mysite.settings)
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
Now it should work as you wish.
You can see this stackoverflow link
1👍
I’ve been looking into this as well. The thing is that python unittest pytest and nose are not alternative to Django tests, because they would not be able to load everything Django tests do.
Django Test Runner might work for you:
https://marketplace.visualstudio.com/items?itemName=Pachwenko.django-test-runner
— I was having trouble with this still since my project root does not directly contain my app(s), but judging on your project structure may work for you.
0👍
Here is generic way to get Django tests to run with full vscode support
- Configure python tests
- Choose unittest
- Root Directory
test*.py
- Then each test case will need to look like the following:
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
- Django + Forms: Dynamic choices for ChoiceField
- Django nginx Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- How to get field names when running plain sql query in django