Enable Unittest for Django Projects in VSCode

Поделиться
HTML-код
  • Опубликовано: 27 ноя 2024

Комментарии • 12

  • @plexsheep1296
    @plexsheep1296 2 года назад +1

    This was very helpful, thanks. I couldn't figure out how to do it.

  • @ibragdzh
    @ibragdzh 2 года назад +3

    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!

  • @keivanpourzang4633
    @keivanpourzang4633 9 месяцев назад

    great thanks!

  • @johnsolly
    @johnsolly  2 года назад +1

    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()

  • @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 года назад +1

      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 года назад +1

      @@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 9 месяцев назад

    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 9 месяцев назад

      same issue here

    • @ajinzrathod
      @ajinzrathod 9 месяцев назад +1

      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