TotT: The Stroop Effect
February 7th, 2008 | Published in Google Testing
How quickly can you...
- ...read all 25 words out loud: RED, GREEN, BLUE, ... (Try it now!)
- ...say all 25 colors out loud: GREEN, YELLOW, WHITE... (Try it now!)
Did the second task require more time and effort? If so, you're experiencing the Stroop Effect, which roughly says that when a label (in this case, the word) is in the same domain as its content (the color) with a conflicting meaning, the label interferes with your ability to comprehend the content.
What does this have to do with testing? Consider the following code:
public void testProtanopiaColorMatcherIsDistinguishable() {
ColorMatcher colorMatcher = new ColorMatcher(PROTANOPIA);
assertFalse(“BLUE and VIOLET are indistinguishable”,
colorMatcher.isDistinguishable(Color.BLUE, Color.VIOLET));
}
When this test fails, it produces a message like this:
Failure: testProtanopiaColorMatcherIsDistinguishable:
Message: BLUE and VIOLET are indistinguishable
Quick: what caused this error? Were BLUE and VIOLET indistinguishable, or not? If you're hesitating, that's the Stroop Effect at work! The label (the message) expresses a truth condition, but the content (in assertFalse) expresses a false condition. Is the ColorMatcher doing the wrong thing, or is the test condition bogus? This message is wasting your valuable time! Now consider this slight alteration to the test name and test message:
Failure: testProtanopiaColorMatcherCannotDistinguishBetweenCertainPairsOfColors
Message: BLUE and VIOLET should be indistinguishable
Do you find this clearer? Protanopia (reduced sensitivity to the red spectrum) causes certain pairs of colors to be indistinguishable. BLUE and VIOLET should have been indistinguishable, but weren't.
Here are some things to keep in mind when writing your tests:
- When someone breaks your test – will your test name and message be useful to them?
- Opinionated test names like testMethodDoesSomething can be more helpful than testMethod.
- Great test messages not only identify the actual behavior,but also the expected behavior.
- Should is a handy word to use in messages – it clarifies what expected behavior didn't actually happen.
Remember to download this episode of Testing on the Toilet and post it in your office.