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.
-
Determinism
-
Confidence loss
-
Wasted time
-
Test should be in full control. There should be no space for erratic behavior and degrees of freedom.
-
Remove all tests coupling.
Coupling - The one and only software design problem
- Fragile, Intermittent, Sporadic or Erratic tests are common in many organizations.
Nevertheless, they mine the developers trust.
We must avoid them.
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 depends on set sort (which is not defined)
}
}
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);
}
}
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.
Code Smell 76 - Generic Assertions
Code Smell 204 - Tests Depending on Dates
-
Coupling
-
Determinism
Fragile tests show system coupling and not deterministic or erratic behavior.
Developers spend lots of time and effort fighting against these false positives.
Photo by Jilbert Ebrahimi on Unsplash
The amateur software engineer is always in search of magic.
Grady Booch
Software Engineering Great Quotes
This article is part of the CodeSmell Series.