Django 文档(http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests) 表示您可以通过指定单个测试用例来运行它们:
$ ./manage.py test animals.AnimalTestCase
这假设您的测试位于 Django 应用程序的tests.py 文件中。如果这是真的,那么该命令将按预期工作。
我在测试目录中对 Django 应用程序进行了测试:
my_project/apps/my_app/
├── __init__.py
├── tests
│ ├── __init__.py
│ ├── field_tests.py
│ ├── storage_tests.py
├── urls.py
├── utils.py
└── views.py
The tests/__init__.py
文件有一个 suite() 函数:
import unittest
from my_project.apps.my_app.tests import field_tests, storage_tests
def suite():
tests_loader = unittest.TestLoader().loadTestsFromModule
test_suites = []
test_suites.append(tests_loader(field_tests))
test_suites.append(tests_loader(storage_tests))
return unittest.TestSuite(test_suites)
为了运行我所做的测试:
$ ./manage.py test my_app
尝试指定单个测试用例会引发异常:
$ ./manage.py test my_app.tests.storage_tests.StorageTestCase
...
ValueError: Test label 'my_app.tests.storage_tests.StorageTestCase' should be of the form app.TestCase or app.TestCase.test_method
我尝试按照异常消息所说的操作:
$ ./manage.py test my_app.StorageTestCase
...
ValueError: Test label 'my_app.StorageTestCase' does not refer to a test
当我的测试位于多个文件中时,如何指定单个测试用例?