DEV Community

Cover image for Code Smell 52 - Fragile Tests
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

Code Smell 52 - Fragile Tests

Tests are our safety nets. If we don't trust on their integrity, we will be in great danger.

TL;DR: Don't write non-deterministic tests.

Problems

  • Determinism

  • Confidence loss

  • Wasted time

Solutions

  1. Test should be in full control. There should be no space for erratic behavior and degrees of freedom.

  2. Remove all tests coupling.

Examples

  • Fragile, Intermittent, Sporadic or Erratic tests are common in many organizations.

Nevertheless, they mine the developers trust.

We must avoid them.

Sample Code

Wrong

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import components.set.Set;
import components.set.Set1L;

public abstract class SetTest {

    protected abstract Set<String> constructor();

    @Test
    public final void testAddEmpty() {
        Set<String> s = this.constructor();
        s.add("green");
        s.add("blue");
        assertEquals("{green. blue}", s.toString());
       //This is fragile since it dependes on set sort (which is not defined)
    }   
}
Enter fullscreen mode Exit fullscreen mode

Right

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import components.set.Set;
import components.set.Set1L;

public abstract class SetTest {

    protected abstract Set<String> constructor();

    @Test
    public final void testAddEmpty() {
        Set<String> s = this.constructor();
        s.add("green");
        assertEquals("{green}", s.toString());
    }   

    @Test
    public final void testEntryAtSingleEntry() {
        Set<String> s = this.createFromArgs("red");
        Boolean x = s.contains("red");
        assertEquals(true, x);
    } 
}
Enter fullscreen mode Exit fullscreen mode

Detection

Detection can be done with test run statistics.

It is very hard to put some test in maintenance since we are removing a safety net.

More Info

%[https://softwareengineering.stackexchange.com/questions/109703/how-to-avoid-fragile-unit-tests]

Relations

Tags

  • Coupling

  • Determinism

Conclusion

Fragile tests show system coupling and not deterministic or erratic behavior.

Developers spend lots of time and effort fighting against this false positives.

Credits

Photo by Jilbert Ebrahimi on Unsplash


The amateur software engineer is always in search of magic.

Grady Booch


This article is part of the CodeSmell Series.

Last update: 2021/06/12

Redis image

62% faster than every other vector database

Tired of slow, inaccurate vector search?
Redis delivers top recall and low latency, outperforming leading vector databases in recent benchmarks. With built-in ANN and easy scaling, it’s a fast, reliable choice for real-time AI apps.

Get started

Top comments (1)

Collapse
 
yoursunny profile image
Junxiao Shi

Should tests verify the internals (private fields) of a class?
I've seen many test cases that assert every internal variable. I don't like that.

My reason is: Every time you refactor the class you have to rewrite the tests. However, tests are supposed to ensure the external behaviour of a class doesn't change unexpectedly. If the tests are rewritten, it's hard to confirm whether the external behaviour changed.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay