简体   繁体   English

Django的单元测试是否向异常发出警告?

[英]Does Django's Unit Testing Raise Warnings to Exceptions?

I am using Django's unit testing apparatus (manage.py test), which is throwing an error and halting when the code generates a warning. 我正在使用Django的单元测试设备(manage.py测试),当代码生成警告时,该设备会引发错误并暂停。 This same code when tested with the standard Python unittest module, generates warnings but continues code execution through them. 当使用标准Python unittest模块进行测试时,相同的代码会生成警告,但会继续通过警告执行代码。

A little research shows that Python can be set to raise warnings to exceptions, which I suppose would cause the testing framework to think an error had occurred. 一项小小的研究表明,可以将Python设置为向异常发出警告,我认为这会使测试框架认为发生了错误。 Unfortunately, the Django documentation on testing is a little light on the definition of an "error", or how to modify the handling of warnings. 不幸的是,有关测试的Django文档对“错误”的定义或如何修改警告的处理有些许了解。

So: Is the Django unit testing framework setup to raise warnings to errors by default? 因此:默认情况下,Django单元测试框架是否设置为对错误提出警告? Is there some facility in Django for changing this behavior? Django中是否有一些工具可以更改此行为? If not, does anyone have any suggestions for how I can Django to print out the errors but continue code execution? 如果没有,是否有人对Django如何打印出错误但继续执行代码有任何建议? Or have I completely misdiagnosed the problem? 还是我完全误诊了问题?

UPDATE: The test code is halting on warnings thrown by calls on MySQLdb. 更新:测试代码因MySQLdb调用引发的警告而停止。 Those calls are made by a module which throws the same warnings when tested under the Python unittest framework, but does not halt. 这些调用是由一个模块发出的,该模块在Python unittest框架下进行测试时会发出相同的警告,但不会停止。 I'll think about an efficient way of trying to replicate the situation in code terse enough to post. 我会考虑一种有效的方法,尝试用足够简短的代码来复制情况以进行发布。

ANSWER: 回答:

A little more research reveals this behavior is related to Django's MySQL backend: 更多研究表明此行为与Django的MySQL后端有关:

/usr/...django/.../mysql/base.py: /usr/...django/.../mysql/base.py:

if settings.DEBUG:
    ...
    filterwarnings("error", category=Database.Warning)

When I change settings.py so DEBUG = False, the code throws the warning but does not halt. 当我更改settings.py以便DEBUG = False时,代码将引发警告,但不会停止。

I hadn't previously encountered this behavior in Django because my database calls are generated by backend of my own. 我以前在Django中没有遇到过这种情况,因为我的数据库调用是由我自己的后端生成的。 Since I didn't call the Django backend, I didn't reset the handling of the warnings, and the code continued despite the warnings. 由于我没有调用Django后端,所以我没有重置警告的处理方式,尽管有警告,代码仍然继续。 The Django test framework surely calls the Django backend -- it does all sorts of things with the database -- and that call would reset the warning handling before my code is called. Django测试框架肯定会调用Django后端-它会对数据库进行各种操作-并且该调用将在调用我的代码之前重置警告处理。

Given the updated info, I'm inclined to say that this is the right thing for Django to be doing; 鉴于更新的信息,我倾向于说这是Django要做的正确的事情。 MySQL's warnings can indicate any number of things up to and including loss of data (eg, MySQL will warn and silently truncate if you try to insert a value larger than a column can hold), and that's the sort of thing you'd want to find out about when testing. MySQL的警告可以指示直到数据丢失(包括数据丢失)的任何数量(例如,如果您尝试插入的值大于列可以容纳的值,则MySQL会警告并以静默方式截断),这就是您想要的事情了解有关何时进行测试的信息。 So probably your best bet is to look at the warnings it's generating and change your code so that it no longer causes those warnings to happen. 因此,最好的选择是查看它所生成的警告并更改代码,以使其不再导致那些警告发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试时Django的send_email()不会引发BadHeaderError - Django's send_email() does not raise BadHeaderError when testing Django / Python单元测试:让异常上升 - Django/Python Unit Testing: Let exceptions rise 在 django 单元测试中出现 BadHeaderError 时引发异常的最佳方法是什么? - What is the best way to raise an exception in case of BadHeaderError in django unit testing? 出于单元测试的目的,如何修补Python的raise(或return)关键字? - How to patch Python's raise (or return) keyword for the purpose of unit testing? 单元测试如何在 django 中工作? (有覆盖) - how does unit testing works in django? (with coverage) django 单元测试异常 - django unit test for exceptions 对Python构造函数中的异常进行单元测试 - Unit testing for exceptions in Python constructor raise FieldError("Cannot resolve keyword '%s' into field." django.core.exceptions.FieldError: 无法解析关键字 - raise FieldError("Cannot resolve keyword '%s' into field. " django.core.exceptions.FieldError: Cannot resolve keyword 如何在基于 ORM 异常的 Django 视图中引发异常 - How to raise exceptions in Django view based in ORM exceptions 在 API 中更新 function 不引发异常 - does an update function in the API not raise exceptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM