cover-img

Code Smell 76 - Generic Assertions

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

11 June, 2023

1

1

0

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

Right

square = Square(5)

assert square.area() = 25

# Assertion should be precise

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

Code Smell 30 - Mocking Business

Code Smell 52 - Fragile Tests

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

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

1

1

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.