Photo by JESHOOTS.COM on Unsplash

Developers know that it’s important to test their code. There is a difference, though, between knowing a thing is important and actually doing that thing. Oftentimes developers prioritize extending their code rather than dedicating time to writing tests for the code that they’ve already completed.

So I wanted to take some time to explore testing in code. What is it, why does it need to be done? Are there different types of tests?

What is testing and why does it need to be done?

The solution to this is automation. We now write tests for our code that will spare us from having to make sure we perform just the right clicks to check all parts of our app. Automated testing also makes large scale refactoring much less of a headache, allowing you to determine whether you are breaking things in close to real-time.

Are there different types of tests?

General idea for the testing pyramid

The idea here is to make sure you write a large number of small unit tests that are fast. The middle layer should have a moderate amount of coarse-grained tests. There should only be a few high level tests. This idea of decreasing the number of tests the higher up you go (and increasing their breadth of scope) will help establish a healthy, fast and maintainable test suite.

Ok, so what are each of these layers in the testing pyramid?

Unit Tests

Unit tests have a number of benefits.

  1. You can be secure in the knowledge that your code is working. If everything seems to work initially and then you deploy your app without testing…you are often left with the feeling that something could break at any moment. With unit tests, you can drastically cut down on that fear. You can make sure to fix bugs early on in the development cycle and increase confidence in your code when it’s deployed.
  2. With proper unit tests, all developers involved in a project can better understand what’s going on in the code and changes can be made much quicker. It also helps structure the code better at the beginning of a project…thinking about how code can be unit-tested will help avoid messy code that is responsible for way too many actions.
  3. Understanding edge cases is also made easier by unit testing. If a parameter fall outside an expected range, contains too few or too many characters, or is null, how do you handle that? All this becomes clearer as you build your tests.
  4. If you need to re-use code, proper unit tests can help get that up and functional in a fraction of time. You can migrate over code and its associated unit tests!

Integration Tests

Functional Tests

Hopefully this article gave you a general idea of what testing is all about. In future articles, I’m going to take a deeper dive into unit testing and give some more concrete examples. Happy coding (and testing)!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store