django - 在 django 1.6 之后运行所有测试

标签 django testing django-testing django-1.6

在 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/

相关文章:

selenium - 如何在日志中打印作为 TestNG 中一组的一部分运行的测试数?

用于检查 header token 的 Django 自定义中间件。使用有效请求 header token 进行测试仍然失败

django - 具有多个应用程序和数据库的单元测试项目 - TEST [DEPENDENCIES] 中的循环依赖

Django 测试客户端忽略 Django 模板中的 "include"项

python - Django 主键序列化器 "This field may not be null"同时allow_null=True

Django,从带注释的最大值中选择一个最大值(几个字段中的最大值)

loops - 使用 Selenium WebDriver 在一次测试中遍历多个浏览器是个好主意吗?

python - 管理端修改django

javascript - 如何在 Javascript/Python/Django 中向数字添加千位分隔符

PHPUnit 测试总是成功,无论是 $this->once() 还是 $this->never()?