Can pytest be made to fail if nothing is asserted?

£可爱£侵袭症+ 提交于 2019-12-01 04:18:50

问题


Today I had a failing test that happily succeeded, because I forgot a rather important line at the end:

assert actual == expected

I would like to have the machine catch this mistake in the future. Is there a way to make pytest detect if a test function does not assert anything, and consider this a test failure?

Of course, this needs to be a "global" configuration setting; annotating each test function with @fail_if_nothing_is_asserted would defeat the purpose.


回答1:


This is one of the reasons why it really helps to write a failing test before writing the code to make the test pass. It's that one little extra sanity check for your code.

Also, the first time your test passes without actually writing the code to make it pass is a nice double-take moment too.



来源:https://stackoverflow.com/questions/48419754/can-pytest-be-made-to-fail-if-nothing-is-asserted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!