Тёмный

Enable Unittest for Django Projects in VSCode 

John Solly
Подписаться 294
Просмотров 7 тыс.
50% 1

Take full advantage of Visual Studio Code's support for unittesting
tests.py
from django.test import TestCase
from django import setup
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
setup()
Create your tests here.
from .models import Post
class PostTestCase(TestCase):
def test_queryset_exists(self):
qs = Post.objects.all()
self.assertTrue(qs.exists())

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 12   
@plexsheep1296
@plexsheep1296 2 года назад
This was very helpful, thanks. I couldn't figure out how to do it.
@ibragdzh
@ibragdzh 2 года назад
Thanks, but this is quite inconvenient to write those setup() in every test file
@johnsolly
@johnsolly 2 года назад
I agree, If you find a better way, let me know!
@johnsolly
@johnsolly 2 года назад
Just learned if you add "django": true to your config, you can debug the django HTML templates! See -> code.visualstudio.com/docs/python/debugging under 'Specific app types" You might also want to add these two lines of code to the end of your test file so you can re-run the test after hitting a breakpoint. if __name__ == "__main__": unittest.main()
@keivanpourzang4633
@keivanpourzang4633 8 месяцев назад
great thanks!
@xuelinli3813
@xuelinli3813 2 года назад
I'm currently using the same setup, but I find it really confusing as manage.py test will leverage a test database while setting up like this will use the production database. Any idea how to configure it to use the test database? Trying out but no luck so far
@johnsolly
@johnsolly 2 года назад
I also had this same issue. The workaround I came up with is to check for keywords in sys.argv to find out what has started the Django instance. If it's pytest, I switch the database out. import sys found_count = len( {item for item in ["pytest"] if any(item in arg for arg in sys.argv)} ) if found_count > 0: DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(BASE_DIR, "db.sqlite3"), } } You can see my whole settings.py here github.com/jsolly/blogthedata/blob/master/django_project/django_project/settings.py
@xuelinli3813
@xuelinli3813 2 года назад
@@johnsolly Thanks for the info! I'm using unittest so I did figure out a workaround solution. I created another settings.py file and ask all vscode tests routing to the test database. I find that the setup only works for vscode, so this is what I end up doing at least for now.
@johnsolly
@johnsolly 2 года назад
@@xuelinli3813 I recently did the same. I'm using two postgres databases. One for testing, one for development. You can see how I split up my settings files here github.com/jsolly/blogthedata/tree/master/django_project/django_project/settings
@ajinzrathod
@ajinzrathod 8 месяцев назад
I m getting: Subprocess exited unsuccessfully with exit code 1 and signal null on workspace. path/to/my/project/ Creating and sending error discovery payload. pytest test discovery error for workspace: The python test process was terminated before it could exit on its own, the process errored with: Code: 1, Signal: null for workspace path/to/my/project/
@americanconsumer4496
@americanconsumer4496 7 месяцев назад
same issue here
@ajinzrathod
@ajinzrathod 7 месяцев назад
Hey, I tried a lot. So after doing everything as he mentioned in the video, just restart your vs code. Even if that doesn’t work, try to reboot. That might solve the problem
Далее
How to turn on logging in your Django App
13:17
Просмотров 4,2 тыс.
Day 2 | IEM Rio 2024 | Playoffs | КРNВОЙ ЭФИР
6:11:51
Unit Tests and Test Doubles like Mocks, Stubs & Fakes
17:32
Как Подключить React К Django?
17:19
Просмотров 31 тыс.
How To Write Unit Tests in Python • Pytest Tutorial
35:34
Django Testing Tutorial - Testing Views #3
16:28
Просмотров 79 тыс.