paint-brush
Basics of Unit Testing in Software Developmentby@prplcode
328 reads
328 reads

Basics of Unit Testing in Software Development

by Simon Egersand 🎈May 15th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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. Unit testing is the most important way of testing your code. Unit tests allow you to test individual units of code in isolation. It also allows you to find and fix bugs early on before they have a chance to cause problems in production. There are many different ways to test your code, and we'll go over why testing is important in this article.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Basics of Unit Testing in Software Development
Simon Egersand 🎈 HackerNoon profile picture


Testing your code is an important part of the software development process. This is to confirm that your code behaves the way 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? This is to ensure 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? It'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 would 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 are we 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;
};


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. 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. However, 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.



Connect with me on Twitter @prplcode

Also published at https://prplcode.dev