How do I run a django TestCase manually / against other database?

Viewed 5604

I have some methods written into a django.test.TestCase object that I'd like to run from the manage.py shell on my real database. But when I try to instantiate the TestCase object to run the test method, I get this error:

ValueError: no such test method in <class 'track.tests.MentionTests'>: runTest

Is there a way to instantiate the TestCase objects? Or is there a way to run a test method against a non-test database?

4 Answers

Here's a method that I found recently. I haven't found anything better yet.

from django.test.utils import setup_test_environment
from unittest import TestResult
from my_app.tests import TheTestWeWantToRun

setup_test_environment()
t = TheTestWeWantToRun('test_function_we_want_to_run')
r = TestResult()
t.run(r)
r.testsRun # prints the number of tests that were run (should be 1)
r.errors + r.failures # prints a list of the errors and failures

According to the docs, we should call setup_test_environment() when manually running tests. django.test uses unittest for testing so we can use a TestResult from unittest to capture results when running the test.

In Django 1.2, DjangoTestRunner could be used for more structured testing. I haven't tried this yet though.

Related