问题:当您的应用具有测试目录时,在Django中运行特定的测试用例
Django文档(http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests)指出,您可以通过指定单个测试用例来运行它们:
$ ./manage.py test animals.AnimalTestCase
假设您将测试保存在Django应用程序的tests.py文件中。如果是这样,那么此命令将按预期工作。
我在tests目录中有针对Django应用程序的测试:
my_project/apps/my_app/
├── __init__.py
├── tests
│ ├── __init__.py
│ ├── field_tests.py
│ ├── storage_tests.py
├── urls.py
├── utils.py
└── views.py
该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
当我的测试位于多个文件中时,如何指定单个测试用例?
The Django documentation (http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests) says that you can run individual test cases by specifying them:
$ ./manage.py test animals.AnimalTestCase
This assumes that you have your tests in a tests.py file in your Django application. If this is true, then this command works like expected.
I have my tests for a Django application in a tests directory:
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
file has a suite() function:
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)
To run the tests I do:
$ ./manage.py test my_app
Trying to specify an individual test case raises an exception:
$ ./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
I tried to do what the exception message said:
$ ./manage.py test my_app.StorageTestCase
...
ValueError: Test label 'my_app.StorageTestCase' does not refer to a test
How do I specify an individual test case when my tests are in multiple files?
回答 0
结帐django-nose。它允许您指定测试运行方式:
python manage.py test another.test:TestCase.test_method
或如注释中所述,使用以下语法:
python manage.py test another.test.TestCase.test_method
Checkout django-nose. It allows you to specify tests to run like:
python manage.py test another.test:TestCase.test_method
or as noted in comments, use the syntax:
python manage.py test another.test.TestCase.test_method
回答 1
从Django 1.6开始,您可以对要运行的元素使用完整的点符号来运行完整的测试用例或单个测试。
现在,自动测试发现将在工作目录下以test开头的任何文件中找到测试,因此解决了您必须重命名文件的问题,但是现在您可以将其保留在所需的目录中。如果要使用自定义文件名,则可以使用option标志指定一个模式(默认Django测试运行器)--pattern="my_pattern_*.py"
。
因此,如果您在manage.py
目录中,并且想要在app / module下的文件test_a
内的TestCase
子类中运行测试,则可以执行以下操作:A
tests.py
example
python manage.py test example.tests.A.test_a
如果您不想在Django 1.6或更高版本中包含依赖项,那么您将采用这种方式。
有关更多信息,请参见Django文档。
Since Django 1.6 you can run a complete test case, or single test, using the complete dot notation for the element you want to run.
Automatic test discovery will now find tests in any file that starts with test under the working directory, so addressing the question you would have to rename your files, but you can now keep them inside the directory you want. If you want to use custom file names you can specify a pattern (default Django test runner) with the option flag --pattern="my_pattern_*.py"
.
So if you are in your manage.py
directory and want to run the test test_a
inside TestCase
subclass A
inside a file tests.py
under the app/module example
you would do:
python manage.py test example.tests.A.test_a
If you don’t want to include a dependency and are in Django 1.6 or later that’s how you do it.
See the Django documentation for more information
回答 2
我自己遇到了这个问题,发现了这个问题,以防万一其他人出现,这就是我挖的东西。DjangoTestSuiteRuner使用一种称为build_test(label)的方法,该方法根据标签确定要运行哪些测试用例。研究此方法,结果发现他们正在“模型”或“测试”模块上执行getattr()。这意味着,如果您返回一个套件,则测试运行程序不在该套件中寻找您的测试用例,它只会查找那些模块中的一个。
一个快速的解决方法是__init__.py
直接导入测试而不是定义套件。使它们成为“测试”模块的一部分,因此build_test(label)可以找到它们。
对于上面的示例,tests/__init__.py
应仅包含:
from field_tests import *
from storage_tests import *
这不是很优雅,当然,如果您要对套件进行一些更复杂的操作,则此方法将无法正常工作,但在这种情况下可以。
I was having this problem myself and found this question, in case anyone else comes along, here was what I dug up. The DjangoTestSuiteRuner uses a method called build_test(label) that figures out what test cases to run based on the label. Looking into this method it turns out they’re doing a getattr() on either the “models” or “test” module. This means if you return a suite the test runner isn’t looking for your test cases in that suite, it only looks in one of those modules.
A quick work-around is to use __init__.py
to import your tests directly instead of defining a suite. The makes them part of the “test” module and so build_test(label) can find them.
For your example above, tests/__init__.py
should simply contain:
from field_tests import *
from storage_tests import *
This isn’t very elegant and of course if you’re trying to do something more complicated with your suite then this won’t work, but it would for this case.
回答 3
这应该工作-
python manage.py test my_app.tests.storage_tests
This should work-
python manage.py test my_app.tests.storage_tests
回答 4
回答 5
将此代码放在__init__.py中,它将导入包和子包中的所有测试类。这将允许您运行特定的测试,而无需手动导入每个文件。
import pkgutil
import unittest
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
exec ('%s = obj' % obj.__name__)
同样,对于您的测试套件,您可以简单地使用:
def suite():
return unittest.TestLoader().discover("appname.tests", pattern="*.py")
现在,您需要为新测试做的就是编写它们,并确保它们在tests文件夹中。不再需要繁琐的进口维修!
Put this code in your __init__.py and it will import all test classes in the package and subpackages. This will allow you to run specific tests without manually importing every file.
import pkgutil
import unittest
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
exec ('%s = obj' % obj.__name__)
Similarly, for your test suite you can simply use:
def suite():
return unittest.TestLoader().discover("appname.tests", pattern="*.py")
Now all you have to do for new tests is write them and make sure they are in the tests folder. No more tedious maintenance of the imports!