在 django 1.5 及更早版本中,运行 python manage.py test
将默认运行项目中的所有测试(包括 django.contrib 中的所有测试)。在 1.6 版本之后,默认行为是在当前目录中运行所有测试。
无论是否使用 django.contrib 测试,运行所有测试的最佳方式 (v 1.6) 是什么?
最佳答案
Django 1.6 changed the default test runner到:
TEST_RUNNER = 'django.test.runner.DiscoverRunner'
您可以通过添加到您的 settings.py 来恢复旧行为:
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
如发行说明中所述:
The previous runner (django.test.simple.DjangoTestSuiteRunner) found tests only in the models.py and tests.py modules of a Python package in INSTALLED_APPS.
The new runner (django.test.runner.DiscoverRunner) uses the test discovery features built into unittest2 (the version of unittest in the Python 2.7+ standard library, and bundled with Django). With test discovery, tests can be located in any module whose name matches the pattern test*.py.
新的运行器需要一个模块的点路径列表,在这些模块中可以发现测试,因此您也可以通过 django contrib
以这种方式运行测试:
python manage.py test myproject django.contrib path.to.someotherapp
不过,这将不会自动运行INSTALLED_APPS
中应用程序的所有测试。对于更复杂的解决方案,您可以编写自己的运行器,同时使用新旧运行器。
另请注意,通常不需要从 django.contrib
运行测试,因为这些测试不是在测试您的应用程序,而是在测试 Django 发行版。 Django 附带了更多的测试,这些测试不是由任何一个运行器运行的。
关于django - 在 django 1.6 之后运行所有测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904979/