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

Groupe SNCF Modernizes Infrastructure with Talos OS and Kubernetes
News
X is getting closer to removing the last reminders of Twitter
News
When the doctor won’t listen to you, MyDébboApp bets it will  |
Computing
AT&T unveils Open RAN call milestone | Computer Weekly
News

You Might also Like

Computing

When the doctor won’t listen to you, MyDébboApp bets it will  |

10 Min Read
Computing

AI Agents to Discover Drugs | HackerNoon

8 Min Read
Computing

New ChatGPT Atlas Browser Exploit Lets Attackers Plant Persistent Hidden Commands

5 Min Read
Computing

AMD EPYC 9965 “Turin” 2P Performance Seeing Some Gains On Linux 6.18

2 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?