Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can pytest be made to fail if nothing is asserted?

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.

like image 600
Thomas Avatar asked Jan 24 '18 10:01

Thomas


1 Answers

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.

like image 119
Joris Timmermans Avatar answered Oct 15 '22 11:10

Joris Timmermans