Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 91 — Test Asserts without Description

Maximiliano Contieri
Dev Genius
Published in
2 min readOct 8, 2021

Photo by Startaê Team on Unsplash

TL;DR: Use asserts with declarative descriptions.

Problems

  • Readability
  • Hard debugging
  • Time waste

Solutions

  1. Put a nice descriptive assertion
  2. Share guides for problem-solving

Sample Code

Wrong

<?public function testNoNewStarsAppeared(): void
{
$expectedStars = $this->historicStarsOnFrame();
$observedStars = $this->starsFromObservation();
//These sentences get a very large collection
$this->assertEquals($expectedStars, $observedStars);
//If something fails we will have a very hard debugging time
}

Right

<?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 we can see EXACTLY why the assertion failed with a clear and
//Declarative Message
}

Detection

Since assert and assertDescription are different functions, we can adjust our policies to favour the latter.

Tags

  • Test Smells

Conclusion

Be respectful to the reader of your assertions.

It might even be yourself!

More Info

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

John Woods

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response