Unit Testing – The Basics

April 8, 2019 —

What is a Unit?

In order to understand what unit testing is, it is important to first know what a “unit” is. In general, a unit is going to be a function in the code that performs a certain task, but in some cases can refer to the entire class, if the class is fairly simple. Defining a unit like this is important, as a unit that is too complicated cannot be easily unit tested.

What is a Unit Test?

A unit test is a small piece of code that is used to independently verify that the unit works as expected in all scenarios. It should not rely on any other parts of the code. This is why it is important to make sure that the unit is simple, and only performs a specific task. If a unit is overly complicated, it becomes difficult to test it, as there can be many factors that can impact how the unit works.

Why are Unit Tests Important?

While creating unit tests may seem like extra overhead, they can actually provide much needed relief later on down the line. By taking the extra time in the beginning phases of writing code to create unit tests, the amount of time spent fixing errors later on down the line can be significantly cut down.

How to Effectively and Efficiently Unit Test

The idea that you can write unit tests to test all scenarios seems like an impossible task. There is simply no way that you can cover every single possible thing that could happen to your code. However, you can cover most scenarios in a way that doesn’t require hundreds of tests per unit, but still takes care of the major cases to worry about. This can be done by testing what are known as normal, error, boundary, and special scenarios.

A normal scenario is anything that you would expect your code to handle without any issues. For example, if a section of code takes two integer numbers, performs some bit of logic with them, and outputs a different integer number, a normal scenario to test would be inputting two integers to see if the expected output number is produced.

An error scenario is any situation which would cause your code to error. For most pieces of code, errors can happen in many different ways, so it is useful to try and test multiple things that could potentially break the logic in the code. Using the example from before, an error test could be used to make sure that the code handles a text input instead of an integer input properly. In this case, the code should throw some kind of error, and the test would verify that the correct error was thrown.

A boundary case is anything that is right at the edge of what your code can handle. In general, these are created in pairs, with one being just inside the boundary, and one just outside the boundary. If we modify the example above to say that the code takes two integers, but they have to be 0 – 100, a boundary case could be created to test how the code handles the user entering 100 twice, and another test could be created to test how the code handles the user entering 101 twice. In this situation, it would also be helpful to create another pair of tests that would test the lower boundary as well.

The final scenario to try and test is a special case. This is much harder to define, as special is a very broad term. The best way to think of this is to determine whether the code should behave in a certain way if a very specific situation happens. Using the example above, there could be a special case built into the logic of the code that handles if null values are passed in instead of integers.

In some cases, it is not necessary to test special cases, as the code will be straightforward and won’t have any quirks to it. In these situations, only the first three scenarios (normal, error, and boundary) are necessary.

Unit testing is a very broad term and can seem like a very daunting task to incorporate into your workflow when you aren’t used to it. This is a very simplified overview of the basics of unit testing, as well as a quick and easy method to get started with your first unit tests. There are many ways to extend what unit testing can do, and this can provide even more possibilities for what you can test, and how you can improve your code.

We Wrote the Book

The Indispensible Guide to ArcGIS Online

Download It for Free

What do you think?

Leave a comment, and share your thoughts

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.