Pieter
Pieter

Reputation: 32775

Running unit tests from Django libraries without manage.py

I'm trying to write unit tests for a Python library that depends on Django. They work fine when I try to run them as part of a Django project:

>python manage.py test mexaminer.tests
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK
Destroying test database for alias 'default'...

When I try to run them on their own, this happens:

>python -m unittest mexaminer.tests
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 38, in _set
up
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "C:\Python34\lib\os.py", line 631, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'

The same thing happens when I run python -m django.utils.unittest mexaminer.tests. Is there a way to run my unit tests without setting up a full Django project? Source code is here.

Upvotes: 3

Views: 1746

Answers (1)

amdorra
amdorra

Reputation: 1556

you are seeing this error because django can't figure out where is your settings file.

when you use manage.py test django set the path to the settings.

if you want to run your tests in a different environment you need to define a setup method that sets the DJANGO_SETTINGS_MODULE and creates test databases.

preferably you will create a tear down that removes the test database.

you can use the documentation to find out how to do so.

Upvotes: 2

Related Questions