如果没有断言,可以使pytest失败吗? [英] Can pytest be made to fail if nothing is asserted?

查看:262
本文介绍了如果没有断言,可以使pytest失败吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我的一项失败的测试成功了,因为我忘了最后一个很重要的一行:

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

assert actual == expected

我希望将来机器能够捕获此错误.有没有办法让pytest检测到测试函数是否没有断言任何东西,并认为这是测试失败?

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?

当然,这需要是全局"配置设置.用@fail_if_nothing_is_asserted注释每个测试功能会达到目的.

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

推荐答案

这是在编写通过测试的代码之前真正帮助编写失败的测试的原因之一.就是对您的代码进行一点额外的检查.

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.

这篇关于如果没有断言,可以使pytest失败吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆