Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django AppRegistryNotReady when running Unittests from vscode

My UnitTest test cases in a Django project run fine when running them directly from the Django utility (python manage.py test). However, when trying to run them from VSCode's test explorer, I get django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.. I found several similar questions that were already answered here on SO, but none of the solutions seem to work. Here's what I tried:

  • import my classes (e.g. models) within the SetUpTestData method and the actual test methods of the test class, rather than at the top of the file. This seemed to help a bit, in the sense that the tests now do show up in the test explorer (previously they didn't), but I still get an error when running them.

  • set the DJANGO_SETINGS_MODULE environment variable at the top of the file containing the test class:

     import os
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
    
  • add a .env file to my root directory, containing DJANGO_SETINGS_MODULE = my_project.settings

  • add a configuration with purpose debug-test to launch.json:

     "configurations": [
         {
             "name": "Django test",
             "type": "python",
             "request": "launch",
             "program": "${workspaceFolder}\\manage.py",
             "args": [
                 "test"
             ],
             "purpose": ["debug-test"],
             "django": true,
             "justMyCode": true
         }
     ]
    
  • remove tests.py from my app folder, as my tests are stored in a separate folder (and apparently Django can't have both)

My folder structure looks like this:

root
  |_ my_app
      |_ tests
          |_ __init__.py
          |_ test_models.py
  |_ my_project

For the sake of completeness, I have the following in settings.json:

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true
}

Is there any way to make VSCode's test explorer play nice with Django and UnitTest?

like image 876
NiH Avatar asked Oct 22 '25 21:10

NiH


1 Answers

You can try to do the following steps:

  1. Create a __init__.py file in your tests directory (if it doesn't already exist), and add the following code:
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
django.setup()

These codes manually set the Django settings module and initializes Django.

  1. In your settings.json, change the python.testing.unittestArgs to point to your tests directory:
{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./my_app/tests", 
        "-p",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true
}

This tells VS Code to look for tests in the my_app/tests directory.

like image 87
MingJie-MSFT Avatar answered Oct 25 '25 11:10

MingJie-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!