We are big fans of xUnit. But we don't care much for the programmers.
TL;DR: Use asserts with declarative descriptions.
-
Readability
-
Hard debugging
-
Time waste
-
Put a nice descriptive assertion
-
Share guides for problem-solving
<?
public function testNoNewStarsAppeared(): void
{
$expectedStars = $this->historicStarsOnFrame();
$observedStars = $this->starsFromObservation();
// These sentences get a very large collection
$this->assertEquals($expectedStars, $observedStars);
// If something fails you will have a very hard time debugging
}
<?
public function testNoNewStarsAppeared(): void
{
$expectedStars = $this->historicStarsOnFrame();
$observedStars = $this->starsFromObservation();
// These sentences get a very large collection
$newStars = array_diff($expectedStars, $observedStars);
$this->assertEquals($expectedStars, $observedStars,
'There are new stars ' . print_r($newStars,true));
// Now you can see EXACTLY why the assertion failed with a clear and
// declarative Message
}
Since assert and assertDescription are different functions, we can adjust our policies to favor the latter.
- Test Smells
Be respectful to the reader of your assertions.
It might even be yourself!
Photo by Startaê Team on Unsplash
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John Woods
Software Engineering Great Quotes
This article is part of the CodeSmell Series.