Testing Your Code

code tests
Photo by Ferenc Almasi on Unsplash

You may have heard about software testing, but what is it actually and should we use it when writing programs?

In this quick article I will explain what testing is, why it’s important, and how you can get started learning it.

Since most of my audience are writing code with JavaScript, we will talk about testing in the context of programs and apps written with JavaScript.

What is testing?

In its simplest form, testing refers to writing code (tests) to ensure that our program works in the way we expect it to.

Think of it like this, you want to build a paper airplane, first you write out what you expect the airplane to be able to do. For example, it should have two wings. It should be able to fly. It should be able to fly a minimum of 5 feet, and so on.

Now, it’s approximately 23 seconds later and your paper airplane is completely built. Time to test!

Test 1. Does it have two wings? You observe the plane, it does, check☑️.

Test 2. Does it fly? You throw throw the plane, it nose dives and falls to your feet. Test 2 has failed, you are terrible at making paper airplanes, did you even have a childhood?! Just kidding, that was mean, your plane flies fine ✈️, your test passes with flying colors 🏳️‍🌈. Next!

Test 3. Does it fly a minimum of 5 feet 📏? Heck yeah it does, in fact it’s still flying right now 🦅, you’re a genius and you’ve made the first paper airplane to fly over a mile. Pat yourself on the back.

Alrighty then, what we’ve just described is pretty much exactly how code testing works. Not nearly as exciting, perhaps, but you get the point.

Why is it important?

Despite being time intensive and therefore costly, testing is important because, if done properly, it facilitates the writing of clean, secure, and modular code that is easy for developers to read, maintain, and extend. There may be some higher costs up front, but over time the costs of maintaining the program will go down significantly and can ultimately cost much less than a codebase written without any tests.

Imagine for a moment your paper airplane is a real airplane and you’ve written extensive tests that are performed on the plane throughout its creation. One day on a test flight one of the tests fails. Rather than guessing why the plane is no longer working, the engineering and mechanical teams will know exactly what is wrong and therefore be able to remedy the issue with ease.

The same goes for your code. Maybe you introduced some new code into your application and it causes one or more of the tests to fail. You now know exactly what’s wrong and have saved yourself a lot of wasted time trying to debug the issue.

How can you get started with testing?

Now that you have a general idea of what testing is and why it’s useful, you may be asking, “How can I get started?”.

Firstly, you will want to do a little more reading on the different types of testing. For example, one common form of testing is referred to as unit testing. This is where a singular, modular, and isolated piece of your codebase can be tested on its own, without the use of separate systems like a database or microservice. Another kind of testing is called integration testing. This is where you test multiple units working together and may be reliant on other entities like a database or server. There are other forms of testing too, like end-to-end testing, which is what it sounds like and tests software in its entirety to ensure that it performs as expected in a live/production environment while communicating with any outside systems, interfaces, and so on.

If you want to get started right away with writing unit tests for your JavaScript code then I would suggest learning Jasmine. The syntax is easy to learn and it will cover pretty much everything you need for writing unit tests, and more. There are plenty of other frameworks and tools out there like Cypress or Mocha and Chai, but Jasmine should suffice for getting your feet wet. Once you learn it, you can easily switch to another testing tool and in some cases will even find that the syntax is similar.

This article is meant to be purely conceptual, in a future post I will demonstrate how to write some tests for a simple JavaScript program, but in the meantime I would highly recommend reading the Jasmine documentation or searching online for existing Jasmine tutorials as there are already plenty in existence.

Thanks for reading!

Leave a comment

Your email address will not be published. Required fields are marked *