Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 769
Leopd Avatar asked Jul 30 '10 19:07

Leopd


People also ask

How do I run a test case in Django?

Open /catalog/tests/test_models.py.TestCase , as shown: from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

How do I run a parallel test in Django?

Running tests in parallel To leverage them, you can use the --parallel flag. Django will create additional processes to run your tests and additional databases to run them against. You will see something like this: > python3 manage.py test --parallel --keepdb Using existing test database for alias 'default'...

Which function allows you to run test cases in Python?

pytest. pytest supports execution of unittest test cases. The real advantage of pytest comes by writing pytest test cases. pytest test cases are a series of functions in a Python file starting with the name test_ .


1 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.

like image 107
Trey Hunner Avatar answered Sep 29 '22 16:09

Trey Hunner