michael-mammut
michael-mammut

Reputation: 2783

Django Unittest runs into Error

Environment: Intellij PyCharm, Python Django, Sqlite3

I use a standard Django setup project. I try to write some Unittests, but I run into the following error. After some research, I ended up here.

DB Config

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Code of tests.py

from django.test import TestCase as djangoTestCase
import os
import unittest

# Create your tests here.
class TestExample(djangoTestCase):

    def setUp(self):
        print('setup')
        print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    def test_abc(self):
        self.assertEqual(1,1)

    def tearDown(self):
        print('tearDown')

if __name__ == '__main__':
    unittest.main()

This is the output

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

I tried to find the mistake by reading documentation, but without success. What could be the problem?

Upvotes: 1

Views: 582

Answers (2)

Big Pumpkin
Big Pumpkin

Reputation: 4467

I ran into the same problem in PyCharm. My DB config is in file config.settings_dev. By default, PyCharm is not aware of it.

To solve it,

  1. Open PyCharm's Run/Debug Configurations in menu Run -> Edit Configurations
  2. Expand Templates, then Djano tests.
  3. Add environment variable DJANGO_SETTINGS_MODULE with value config.settings_dev (replace it with your own settings file)

PyCharm Run Config Templates

  1. Delete any existing configurations under Django tests

enter image description here 5. Run the tests in PyCharm

Upvotes: 1

neverwalkaloner
neverwalkaloner

Reputation: 47354

With django test you don't need this part:

if __name__ == '__main__':
    unittest.main()

you just running tests with python manage.py test command.

Upvotes: 1

Related Questions