在Django中测试特定应用
据我所知,可以对所有安装的应用程序或单个应用程序进行测试。对所有应用程序进行测试似乎有点过于复杂,因为这通常还包括Django自带的模块(比如django.contrib.auth
等)。那么,有没有办法创建一个只包含我指定的应用程序的测试套件呢?这样的话,就可以包含我项目文件夹中所有应用程序的所有测试。
3 个回答
1
看起来你需要在不使用Django测试工具的情况下运行测试。
https://docs.djangoproject.com/en/dev/topics/testing/#running-tests-outside-the-test-runner
1
6
你可以写一个专门的测试运行器,只测试你的应用。在之前,我用过类似这样的东西:
tests/runner.py
:
from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings
class AppsTestSuiteRunner(DjangoTestSuiteRunner):
""" Override the default django 'test' command, include only
apps that are part of this project
(unless the apps are specified explicitly)
"""
def run_tests(self, test_labels, extra_tests=None, **kwargs):
if not test_labels:
PROJECT_PREFIX = 'my_project.'
test_labels = [app.replace(PROJECT_PREFIX, '')
for app in settings.INSTALLED_APPS
if app.startswith(PROJECT_PREFIX)]
return super(AppsTestSuiteRunner, self).run_tests(
test_labels, extra_tests, **kwargs)
然后你可以在 settings.py
中把它设置为默认的测试运行器。
TEST_RUNNER = 'my_project.tests.runner.AppsTestSuiteRunner'