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?
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.
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'...
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_ .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With