Why Testing Your Code Is Important

Why Testing Your Code Is Important

Testing your code is a key part of the software development process

Testing your code is an important part of the software development process. It's how you confirm that your code behaves as you want it to. It also allows you to find and fix bugs early on before they have a chance to cause problems in production.

In this article, we'll go over why testing your code is important. There are many different ways of testing your code, and we'll cover the most important way -- unit testing.

Always Test Your Code

Before we talk about the why's, let's talk about the what. What is code testing? It's making sure your code works the way you want it to work. Easy as that.

Now, why should you bother testing your code? Imagine you have a program with a single module. It's responsible for one thing and one thing only. Testing it might seem like overkill (but I recommend always testing your code). Now imagine your program has five modules that all do different things. Additionally, the modules communicate with each other. Your program now has many possible states and testing all states manually will take up a lot of your precious time.

Instead, you should write tests to confirm the behavior! By testing your code, you also protect yourself against breaking features when you add new code. You want to write quality code, right? That's not possible without testing it.

Unit Testing Your Code

There are various types of tests that you can run on your code, but unit tests are the most important. They allow you to test individual units of code in isolation.

Let's look at this JavaScript function.

const incrementByOne = input => {
  if (input < 0) {
    return 0;
  }

  return input + 1;
};

Now let's write some tests. I like to start by testing the "happy path".

test('should increment by 1', () => {

  const input = 9;

  const actual = incrementByOne(input);
  const expected = 10;

  expect(actual).toBe(expected);
});

And then for the fun part; the edge cases!

test('should return 0 when negative input', () => {

  const input = -1;

  const actual = incrementByOne(input);
  const expected = 0;

  expect(actual).toBe(expected);
});

We've now tested everything, and have 100% test coverage. And we're happy, or are we?

I chose JavaScript because it's dynamically typed, and we're allowed to pass any type into our function (psst! This is a good reason to instead use TypeScript).

What happens if we pass a string?

test('should increment by 1', () => {

  const input = 'my-string';

  const actual = incrementByOne(input);
  const expected = ???;

  expect(actual).toBe(expected);
});

The function will return my-string1 because the + operator works between types in JavaScript. This is not what we want, so let's fix it.

Let's update the function to handle this case.

const incrementByOne = input => {
  if (typeof input !== 'number') {
    throw new TypeError('Only number type allowed');
  }

  if (input < 0) {
    return 0;
  }

  return input + 1;
};

And now let's add another test to our test suite.

test('should throw when non-number input', () => {

  const input = 'my-string';

  const expected = new TypeError('Only number type allowed');
  expect(() => incrementbyOne(input)).toThrowError(expected);
});

This was a simple example. Websites and programs are never trivial though, and I hope you can see the value of testing your code now. Testing your code is a crucial part of delivering high-quality software. And it should be a fundamental part of your software development process!

Unit testing is not a silver bullet, and it is not a substitute for other forms of testing, such as system testing and user acceptance testing. But, unit testing is an important part of the software testing process and should not be ignored.

Conclusion

When it comes to code, testing is key to avoiding bugs and ensuring that your code runs as expected. By taking the time to test your code, you can avoid costly mistakes and ensure that your code is ready for prime time.

Enjoyed this post? Then check out Stop using meaningless test values!


Connect with me on Twitter @prplcode

Originally published at https://prplcode.dev

Did you find this article valuable?

Support Simon Egersand 🎈 by becoming a sponsor. Any amount is appreciated!