DEV Community

Cover image for Code Smell 76 - Generic Assertions
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3 2

Code Smell 76 - Generic Assertions

Don't make weak tests to create a false sensation of coverage.

TL;DR: Test Assertions should be precise. Not too Vague and not too specific. There is no silver bullet.

Problems

  • False Negatives

  • Lack of Trust

Solutions

  1. Check the right case

  2. Assert for a functional case.

  3. Don't test implementation.

Sample Code

Wrong

square = Square(5)

assert square.area() != 0

# This will lead to false negatives since it is too vague
Enter fullscreen mode Exit fullscreen mode

Right

square = Square(5)

assert square.area() = 25

# Assertion should be precise
Enter fullscreen mode Exit fullscreen mode

Detection

With Mutation Testing techniques we can find these errors on our tests.

Tags

  • Testing

Conclusion

We should use development techniques like TDD that request concrete business cases and make concrete assertions based on our domain.

Relations

More info

Credits

This smell was inspired by Mario Cervera and used with his permission.

Photo by Fleur on Unsplash


A program that produces incorrect results twice as fast is infinitely slower.

John Osterhout


This article is part of the CodeSmell Series.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay