By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright ยฉ All Rights Reserved. World of Software.
Reading: Code Smell 312 – You Put Multiple Assertions in One Test, Making Failures Hard to Analyze | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright ยฉ All Rights Reserved. World of Software.
World of Software > Computing > Code Smell 312 – You Put Multiple Assertions in One Test, Making Failures Hard to Analyze | HackerNoon
Computing

Code Smell 312 – You Put Multiple Assertions in One Test, Making Failures Hard to Analyze | HackerNoon

News Room
Last updated: 2025/10/27 at 9:12 AM
News Room Published 27 October 2025
Share
SHARE

Cluttered tests hide real problems

TL;DR: You put multiple assertions in one test, making failures hard to analyze.

Problems ๐Ÿ˜”

  • Readability
  • Fragile tests
  • Slow Tests
  • Debugging pain
  • Coupled logic
  • Maintenance nightmare
  • Ambiguous failures
  • Generic assertions

Solutions ๐Ÿ˜ƒ

  1. Follow the One-assert-per-test rule
  2. Extract assert methods
  3. Use descriptive test names
  4. Group related checks
  5. Refactor test logic in smaller pieces

Refactorings โš™๏ธ

https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true

https://hackernoon.com/refactoring-013-eliminating-repeated-code-with-dry-principles?embedable=true

https://maximilianocontieri.com/refactoring-010-extract-method-object?embedable=true

https://maximilianocontieri.com/refactoring-011-replace-comments-with-tests?embedable=true

Context ๐Ÿ’ฌ

When you cram multiple assertions in a single test, failures become ambiguous.

You donโ€™t know which part of the code caused the failure.

Imagine a test with five assertions fails at the second one – you never see whether assertions 3, 4, and 5 would have passed or failed, hiding additional defects.

Tests should be precise, easy to understand, and focused.

Each test should validate a single behavior and clearly communicate its purpose.

A single test function should test a single real world thing/concept.

You should not write long functions testing unrelated behaviors sequentially.

This usually hides the problem of heavy and coupled setups.

Sample Code ๐Ÿ“–

Wrong โŒ

def test_car_performance():
    car = Formula1Car("Red Bull")
    car.set_speed(320)
    assert car.speed == 320
    car.accelerate(10)
    assert car.speed == 330
    car.brake(50)
    assert car.speed == 280
    car.change_tire("soft")
    assert car.tire == "soft"

Right ๐Ÿ‘‰

def test_set_speed():
    car = Formula1Car("Red Bull")
    car.set_speed(320)
    assert car.speed == 320, (
        f"Expected speed to be 320 km/h, "
        f"but got {car.speed} km/h"
    )

def test_accelerate():
    car = Formula1Car("Red Bull")
    car.set_speed(320)
    car.accelerate(10)
    assert car.speed == 330, (
        f"Expected speed to be 330 km/h "
        f"after accelerating by 10, "
        f"but got {car.speed} km/h"
    )

def test_brake():
    car = Formula1Car("Red Bull")
    car.set_speed(330)
    car.brake(50)
    assert car.speed == 280, (
        f"Expected speed to be 280 km/h "
        f"after braking by 50, "
        f"but got {car.speed} km/h"
    )

def test_change_tire():
    car = Formula1Car("Red Bull")
    car.change_tire("soft")
    assert car.tire == "soft", (
        f"Expected tire type to be 'soft', "
        f"but got '{car.tire}'"
    )

Detection ๐Ÿ”

  • [x] Automatic

Check for tests with more than one assert.

Look for tests that fail for multiple reasons or cover multiple unrelated actions.

Most linters and test frameworks can flag multiple assertions.

Set up a rule to warn when tests exceed one or two assertions.

Exceptions ๐Ÿ›‘

You can group multiple asserts only when they validate the same logical behavior or output of a pure function.

Tags ๐Ÿท๏ธ

  • Testing

Level ๐Ÿ”‹

  • [x] Intermediate

Why the Bijection Is Important ๐Ÿ—บ๏ธ

You need a bijection between MAPPER entities and your tests.

If one test checks multiple behaviors, failures break this mapping.

When a test fails, you should immediately know exactly which behavior is broken without reading the test code.

AI Generation ๐Ÿค–

AI generators often produce tests with multiple asserts, trying to cover everything in one shot.

This happens because AI tools optimize for code coverage rather than test clarity, treating tests as checklists rather than behavior specifications.

AI Detection ๐Ÿงฒ

AI can refactor tests to keep one assert per test.

Try Them! ๐Ÿ› 

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Refactor this test file to contain one assert per test method. Keep each test focused and descriptive.

| Without Proper Instructions | With Specific Instructions |
|—-|—-|
| ChatGPT | ChatGPT |
| Claude | Claude |
| Perplexity | Perplexity |
| Copilot | Copilot |
| You | You |
| Gemini | Gemini |
| DeepSeek | DeepSeek |
| Meta AI | Meta AI |
| Grok | Grok |
| Qwen | Qwen |

Conclusion ๐Ÿ

Tests should be focused and precise.

You need to understand quickly which contract is broken.

Avoid multiple asserts per test to make failures clear, debugging faster, and your test suite maintainable.

Relations ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘จ

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xi-sit35t1

https://hackernoon.com/code-smell-03-functions-are-too-long-heres-how-to-fix-that

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-x-i7r34uj

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxi

More Information ๐Ÿ“•

https://hackernoon.com/coupling-the-one-and-only-software-designing-problem-9z5a321h

Disclaimer ๐Ÿ“˜

Code Smells are my opinion.

Credits ๐Ÿ™

Photo by Abhinand Venugopal on Unsplash


Testing is not about finding bugs, it’s about understanding them

Brian Marick

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


This article is part of the CodeSmell Series.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Is Nothing bringing adverts to the phone lock screen?
Next Article Israeli intelligence vets raise $20M to track developer buying signals | News
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

If you hate Google Maps ads, Apple Maps might not be better for much longer
News
Beyond the Hype: Why 87% of AI Projects Fail and What the 13% Do Differently | HackerNoon
Computing
Sennheiserโ€™s Awesome Wireless Earbuds Are Almost Half Off
Gadget
Sky reveals spooktacular FREE Halloween treats for kids this half-term
News

You Might also Like

Computing

Beyond the Hype: Why 87% of AI Projects Fail and What the 13% Do Differently | HackerNoon

12 Min Read
Computing

The AI Co-Pilot for a Star: Google DeepMind and the Quest for Fusion Energy | HackerNoon

8 Min Read
Computing

How to Extract and Embed Text and Images from PDFs for Unified Semantic Search | HackerNoon

9 Min Read
Computing

Correction: Just 3% of Nigeriaโ€™s 170 million mobile users are on 5G

5 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright ยฉ All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?