如何手动运行一个django TestCase对其他数据库? [英] How do I run a django TestCase manually / against other database?
问题描述
我有一些方法写入一个 django.test.TestCase
对象,我想从
管理运行。 py shell
在我的真实数据库。但是当我尝试实例化TestCase对象来运行测试方法时,我收到这个错误:
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
有没有办法实例化 TestCase
对象?还是有办法针对非测试数据库运行测试方法?
Is there a way to instantiate the TestCase
objects? Or is there a way to run a test method against a non-test database?
推荐答案
这是我最近发现的一种方法。我还没有找到更好的东西。
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
根据文档,我们应该调用 setup_test_environment()
手动运行测试。 django.test
使用 unittest
进行测试,所以我们可以使用 unittest的/ code> TestResult c>在运行测试时捕获结果。
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.
在Django 1.2中, DjangoTestRunner
可用于更结构化的测试。我还没有尝试过。
In Django 1.2, DjangoTestRunner
could be used for more structured testing. I haven't tried this yet though.
这篇关于如何手动运行一个django TestCase对其他数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!