Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion messages: assume failure, or assume success

When testing in any language, how does everybody phrase their assertion messages?

I see three obvious ways:

# assume failure
assert (4-2) == 2, "Subtracting 2 from 4 doesn't equal 2"

# describe success
assert (4-2) == 2, "Subtracting 2 from 4 should equal 2"

# be vauge with failure
assert (4-2) == 2, "Subtracting 2 from 4 is broken"

This is obviously a simple example, but you get the idea. What is the standard practice? What do you do? Why?

like image 868
Daniel Beardsley Avatar asked Dec 20 '08 08:12

Daniel Beardsley


People also ask

What are assertion messages?

From a test readability perspective, assertion messages are code comments. Instead of relying on them, refactor tests to be self-documenting. In terms of ease of diagnostics, a better alternative to assertion messages is: Making tests verify a single unit of behavior.

What is assertion failure?

An assertion failure occurs when the database server cannot continue normal processing and must shut down. You can correct some of the problems that cause assertion failures, such as disk issues. For other problems that cause assertion failures, you must contact IBM® Software Support.

What is true about assertions?

An assertion is a true-false statement, a boolean expression, about the values of the variables in a program. In the sequence to the left below, an assertion has been placed in a comment on the second line. The assertion is true at that point.


2 Answers

I don't know what's the standard practice, but I combine your first two approaches with the addition of the actual result obtained.

"Substracting 2 from 4 should equal 2, but equals " + value

This leaves no room for doubt and eases debugging.

like image 64
Vinko Vrsalovic Avatar answered Oct 06 '22 08:10

Vinko Vrsalovic


The important thing with an assert is the actual condition being tested. In C you can use the preprocessor "stringization" to output the actual condition being tested. My code just outputs

Assert Failed: (4-2)==2 : Line 123, File foo.c

If you're lucky, you can get a stack dump as well...

like image 41
Roddy Avatar answered Oct 06 '22 09:10

Roddy