This project has moved and is read-only. For the latest updates, please go here.

Customizing the test runner for a Django app

Jan 4, 2014 at 8:52 PM

I'm working on a Django project using Visual Studio 2013 Ultimate, having installed PTVS through the "new project" wizard, as suggested in the video in the front page of the website.

The problem I'm running into is that I cannot run tests from my Django apps because VS seems to be trying to execute the tests files directly instead of going through, which results in an ImproperlyConfigured exception, as settings.configure() hasn't been executed at that point.

Is there a way to tell VS and PTVS how to run each test?
Jan 10, 2014 at 6:25 PM
We defer almost entirely to the unittest module, so if you want to customize particular tests you will be able to do it by following the Django documentation page on Writing and running tests.

Specifically, it looks like inheriting from django.test.TestCase rather than unittest.TestCase will solve your problem. If not, you still have the full customization available through the standard unittest module.
Jan 22, 2014 at 9:07 AM
Any luck with fixing this?
I also have problem with ImproperlyConfigured exception :(
Just importing something from the Django app, causes the unittests to fail :(

This is the error log I get from running the tests:
Test Name:  test_C
Test Outcome:   Failed
Result StandardError:   
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\django\conf\", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "C:\Python33\lib\", line 676, in __getitem__
    raise KeyError(key) from None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "E:\Projects\Django\DjangoTest\polls\", line 8, in <module>
    from polls.models import Poll
  File "E:\Projects\Django\DjangoTest\polls\", line 1, in <module>
    from django.db import models
  File "C:\Python33\lib\site-packages\django\db\models\", line 5, in <module>
    from django.db.models.query import Q
  File "C:\Python33\lib\site-packages\django\db\models\", line 17, in <module>
    from django.db.models.deletion import Collector
  File "C:\Python33\lib\site-packages\django\db\models\", line 4, in <module>
    from django.db.models import signals, sql
  File "C:\Python33\lib\site-packages\django\db\models\sql\", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "C:\Python33\lib\site-packages\django\db\models\sql\", line 12, in <module>
    from django.db.models.sql.query import Query
  File "C:\Python33\lib\site-packages\django\db\models\sql\", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "C:\wPython33\lib\site-packages\django\db\models\sql\", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\Python33\lib\site-packages\django\db\models\fields\", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\Python33\lib\site-packages\django\conf\", line 54, in __getattr__
  File "C:\Python33\lib\site-packages\django\conf\", line 47, in _setup
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Hope anybody can help with this issue :(
May 4, 2014 at 3:42 AM
Edited May 4, 2014 at 3:44 AM
I'd like to know too. (Googling hasn't helped me.)

I had a quick play and found that you could get around that problem by adding this to the file:
    from django.conf import settings
    if not settings.configured:
Lots wrong with that but in a simple case it should work.

Bad things:
  • it adds django specific stuff to the testlauncher for any project
  • it changes an underlying file MS provided (might get overwritten)
  • it's hardcoded to default settings file
I imagine the correct solution is a django specific test launcher. Perhaps it's possible to do something similar using the mysterious "Test Settings" which I don't know about.
May 4, 2014 at 3:47 AM
Have you derived your test class from the Django TestCase class? That should handle the import.

If not, you can also put that import statement into your test file before any other imports. It's Python, so all the code at the top level will be run when we import the module.