Customizing the test runner for a Django app

Jan 4 at 7:52 PM
Hi,

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 manage.py, 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?
Coordinator
Jan 10 at 5: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 at 8: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\__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "C:\Python33\lib\os.py", line 676, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON TOOLS FOR VISUAL STUDIO\2.0\visualstudio_py_testlauncher.py", line 43, in <module>
    __import__(opts.module)
  File "E:\Projects\Django\DjangoTest\polls\tests.py", line 8, in <module>
    from polls.models import Poll
  File "E:\Projects\Django\DjangoTest\polls\models.py", line 1, in <module>
    from django.db import models
  File "C:\Python33\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "C:\Python33\lib\site-packages\django\db\models\query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "C:\Python33\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "C:\Python33\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "C:\Python33\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "C:\Python33\lib\site-packages\django\db\models\sql\query.py", 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\aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\Python33\lib\site-packages\django\db\models\fields\__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
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 at 2:42 AM
Edited May 4 at 2: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 visualstudio_py_testlauncher.py file:
try:
    from django.conf import settings
    if not settings.configured:
        settings.configure()
except:
    pass
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.
Coordinator
May 4 at 2: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.