Codementor Events

The Most Important Skill When Writing Unit Tests Is Making Sure They Fail

Published Jun 02, 2022Last updated Nov 28, 2022
The Most Important Skill When Writing Unit Tests Is Making Sure They Fail

Yes, seriously…

Around one year ago, I came across this LinkedIn post by Craig Livings who posted this controversial statement:

Any test that has never failed adds no value #tdd #agile

What it means is any test that has never failed has no value, and therefore, can be deleted. In order for any test to add value, it must have failed at least once.

Let’s see why failing our tests is such an important skill…

Why must our tests fail?

Let us say we have written some code and some supporting unit tests. We’ve done good right? The tests have never failed, but we now have unit tests that support our code! Or at least we think we have…

Let’s look at some examples. One of a test that has not failed and one that has.

The test that has never failed

This example is a common scenario where we are just focused on writing a test that passes. We have no intention to make it fail.

We will use the following JavaScript function:

function sum(a, b) {
  return a + b;
}

module.exports = sum;

We have written the following Jest test to verify our sum function adds two numbers together:

const sum = require('./sum');

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

We run the test and it passes ✅

That’s it. We’re happy the test has passed so we move on.

The test that has failed

In this example, we use exactly the same code. However, this time we make sure the test fails to prove its value.

Using the same function:

function sum(a, b) {
  return a + b;
}

module.exports = sum;

And using the same test:

const sum = require('./sum');

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

We run the test and it passes ✅

But this time we want to make sure the test has failed. So we decide to modify the code:

function sum(a, b) {
  return 0;
}

module.exports = sum;

We re-run the test and we get another pass ✅

But wait… this should have failed. We removed the code that adds the numbers together.

We can see the reason the test still passed is that the test itself is incorrect. The test was not calling sum.

So we update the test:

const sum = require('./sum');

test('two plus two is four', () => {
  expect(sum(2, 2)).toBe(4);
});

We re-run the test and it fails ❌

Ok, great! Now let’s put our code back:

function sum(a, b) {
  return a + b;
}

module.exports = sum;

And we re-run the test and it passes ✅

Excellent, now our test has value because we have ensured the test is validating the correct thing.

The example is a simple one. However, when testing more complex code, it is difficult to spot these mistakes. This is why it’s so important we make sure our tests fail before we make them pass — so we have confidence in what we are validating.

The takeaway

When writing tests, we must know they have failed, so we know they add value.

Do you agree?

Discover and read more posts from Christopher Northfield
get started
post commentsBe the first to share your opinion
i you
6 months ago

Dealing with the stress of term papers and tight deadlines can be challenging. But I’ve found a solution at https://mypaperdone.com/write-my-term-paper . My brother suggested this website, and it has been a game-changer for me. If you want to improve your academic performance, don’t hesitate to explore their services!

Catrin Brooks
2 years ago

When I was a developer, I was interested in essay writing. I hope it will be interesting for you too. When selecting a research paper topics , you should consider a variety of factors. For example, a good research paper topic can be about global warming and its effects on animal populations. Another good research paper topic could be about the impact of the Seven Years’ War on colonial economies and British debt.

Ben Davidson
2 years ago

If you’re struggling to write an essay on a complicated topic, you can take help from one of the best essay writing services online https://www.topreviewstars.com/essay-writing-services/ . These companies have been in the business for over a decade and have a long list of happy customers. Their writers are trained to tackle even the most complex of subjects. Whether you’re an undergraduate who needs help before your next lecture, or a Master’s student who needs a specialist for their final research semester, EssayPro can help you. You can select a writer from the list of experts on their website based on his or her educational background, experience, and customer reviews. They also have a dedicated customer support department that will answer your questions at any time.

Show more replies