Stop lying to yourself when testing
Tags: coding, testing, javascript, webdev
Published: 2018-07-24 17:27:34

---
title: Stop lying to yourself when testing
published: true
description: When writing unit tests, are you testing all scenarios, or just those you think should pass?
tags: coding, testing, javascript, webdev
cover_image: https://images2.imgbox.com/1a/1c/2eTesGOQ_o.jpg
---

[The New York Times had a quiz a while back](http://www.nytimes.com/interactive/2015/07/03/upshot/a-quick-puzzle-to-test-your-problem-solving.html) with a simple task: figure out the mathematical rule behind a sequence of numbers.

As a reader, you would enter your own numbers and see if they pass or fail your guess at the rule.

In my mind, this is very similar to the way we unit test. Provide an input, validate the output. Here, you provide the input (the numbers), and the output is whether the number sequence matches the rule.

You can imagine the unit test for it:

```js
function checkRule (numbers) {
	// some magical formula we’re supposed to figure out
};

describe('my mathematical equation', function () {
	it('should pass when doubling each number', function () {
		expect(checkRule([1,2,4])).toBeTrue;
		expect(checkRule([2,4,8])).toBeTrue;
		expect(checkRule([3,6,12])).toBeTrue;
		expect(checkRule([5,10,20])).toBeTrue;
	});
})
```

Looking at this code, it’s easy to assume that the rule is “Each number should double the previous one”. After all, our four assertions pass, so we’ve got green tests!

The trick with the quiz is that the mathematical equation is very simple: each number must be larger than the previous one.

This broad rule means that it’s easy for people to assume their complex solution is the correct one. Every input they give to validate their rule returns true, so it must be right.

Yet there’s a flaw to this testing methodology, as the article points out:

> Remarkably, 78 percent of people who have played this game so far have guessed the answer without first hearing a single no. A mere 9 percent heard at least three nos — even though there is no penalty or cost for being told no, save the small disappointment that every human being feels when hearing “no.”

The article attributes this as “[confirmation bias](http://rationalwiki.org/wiki/Confirmation_bias)” which partially applies. But a better description is [a lesser known bias](http://rationalwiki.org/wiki/List_of_cognitive_biases) called Congruence Bias. (one I was unaware of before hearing about this article on [The Skeptic’s Guide to the Universe](http://www.theskepticsguide.org/)).

This bias is “the tendency to test hypotheses exclusively through direct testing, in contrast to tests of possible alternative hypotheses.”

In our tests above, we’re only checking for positive results. We never ask “does this fail if I provide data which contradicts the rule?” 

Every suite of unit tests should have negation checks. A simple `expect(passesRule([2,4,6])).toNotBeTrue;` would have broken our tests, showing us that the rule passes despite 6 not being twice that of 4. 

Again, from the article:

> When you want to test a theory, don’t just look for examples that prove it. When you’re considering a plan, think in detail about how it might go wrong.

That second part rings especially true for testing.

It’s easy to assume that because your tests pass, the code and the tests are working as expected. But we must remember [what Edsger Dijkstra said long ago](https://en.wikiquote.org/wiki/Edsger_W._Dijkstra#1960s):

> Testing shows the presence, not the absence of bugs.

Think about confirmation and congruence bias next time you’re testing your code.

Keep in mind the phrase “fail fast”. Prove that your code really is what it says it is and always keep a skeptical mind when coding.

Don’t wait until it’s too late to learn the harsh truth.

In [the words of Richard Feynman](http://neurotheory.columbia.edu/~ken/cargo_cult.html):

> The first principle is that you must not fool yourself — and you are the easiest person to fool.

For more analysis of the quiz itself, and thoughts on congruence bias, check out these discussions:
- [SGU Transcript](http://www.sgutranscripts.org/wiki/SGU_Episode_361#Congruence_Bias_.2853:19.29) 
- [Neurological blog post about it](http://theness.com/neurologicablog/index.php/a-quick-logic-lesson/)

<p>Header Photo by <a href="https://unsplash.com/photos/3y1zF4hIPCg">Hans-Peter Gauster</a> on <a href="https://unsplash.com/">Unsplash</a></p>