How to write a good unit test message

I recently started writing unit tests in my projects, and I noticed that all assert statements contain a message argument.

What makes a good message for unit test?

$cache->set('foo', 3); $this->assertEquals($cache->get('foo'), 3, 'what should i say?'); 

Thanks!

+4
source share
2 answers

Indicate verifiable fact.

Consider:

 $this->assertEquals($person->age, 21, "Age is 21") 

vs:.

 $this->assertEquals($person->age, 21, "Age of person born on 1990-12-20"); 

A good unit test message should help you quickly figure out where the error is. A bad person makes you spend more time hunting.

+1
source

I usually keep them pretty and short - just what is being tested. The message should not explain the history of all things that could go wrong, just need to point the reader in the right starting direction. The test should be clear enough so that he can understand it.

Usually I write my posts in such a way that someone who is not familiar with the test will have at least a little context, and someone who knows the test better probably knows exactly what went wrong (or at least least where something went wrong in the test).

In the above example, I would say something like "cache for" foo ", so the message will return as" cache for "foo", expected 3, but it was 67. "For someone familiar with the test, this will be much more useful than "expected 3", but there were 67 on line 123 of the abc test. "

(I am primarily a Java developer, so I say as if it were JUnit, I assume that phpunit or whatever it is called works approximately the same).

+1
source

Source: https://habr.com/ru/post/1387134/


All Articles