python - 将 Django 的系统检查合并到单元测试套件中?

标签 python django unit-testing testing

我最近将一些损坏的代码部署到我们的暂存环境中。新代码失败Django's system checks (下面转载了错误消息,尽管这个问题更笼统)。我们的单元测试套件运行得很干净。我的问题是:确保在部署代码之前运行系统检查的正确方法是什么?

最初我猜测测试能够在不执行系统检查的情况下运行,因为我们使用 pytest 而不是 Django 的测试运行器。但是,添加一个简单的测试并调用 manage.py test 显示 Django 测试运行器也在不执行系统检查的情况下运行。

我的一个想法是在我们的构建管道中运行 manage.py check 命令,并在非零返回值时使构建失败。这种方法的一个缺点是它会在提交代码之前引入另一个开发人员步骤(例如,除了运行单元测试套件之外,请记住运行 manage.py check)。

另一个想法是添加一个运行系统检查的单元测试。这在技术上看似可行,但是否符合Django系统检查框架的目的和设计?

我注意到 the documentation有一个关于为自定义检查编写测试的部分,这并不完全符合我的要求。我没有在 Django 文档中看到有关将系统检查合并到测试中的其他文档。

错误信息:

SystemCheckError: System check identified some issues:

ERRORS:
myapp.MyCoolModel.linked_groups: (fields.E304) Reverse accessor for 'MyCoolModel.linked_groups' clashes with reverse accessor for 'MyCoolModel.primary_group'.
    HINT: Add or change a related_name argument to the definition for 'MyCoolModel.linked_groups' or 'MyCoolModel.primary_group'.
myapp.MyCoolModel.primary_group: (fields.E304) Reverse accessor for 'MyCoolModel.primary_group' clashes with reverse accessor for 'MyCoolModel.linked_groups'.
    HINT: Add or change a related_name argument to the definition for 'MyCoolModel.primary_group' or 'MyCoolModel.linked_groups'.

最佳答案

根据 this ticket ,不与测试一起运行检查是版本 1.8 中引入的回归,最近已修复。

如那里所述,一个简单的解决方案似乎是创建您自己的 test runnerrun_suite() 的开头插入一个 call_command('check')。查看actual fix举个例子。

关于python - 将 Django 的系统检查合并到单元测试套件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42446505/

相关文章:

python - Python 'build_py' 的步骤 'setup.py install' 中的神秘错误

python - 从 Windows + Apache 上运行的 Django 应用程序调用时,Popen 不执行

java - 验证方法体中是否使用了方法参数

objective-c - STAssertEqualStrings 中的转义序列

python - 填充洗牌缓冲区时,Google Colaboratory session 突然结束

python - 从文本文件计算

python - 如何用 Python 编写自己的 OrderedDict 类?

django - 使用 Django session 存储登录用户

unit-testing - 如何在 Swift 中模拟 NSDate?

python - 在谷歌应用程序引擎灵活环境中使用stream_with_context在 flask 中加载栏