Automated tests: why should I care about them and which ones should I use?

Rayane de Araújo
6 min readApr 26, 2019

Automated test is a subject that is gaining the attention of many developers and companies around the world. But why is that happening? Why do people spend time developing tests instead of using their time only to develop the features of the system? What is so special about automated tests?

On this post, I will answer those questions discussing about the benefits of automated tests. After you be convinced that they are awesome (I’m sure you will 😉), I will also help you to know some of them and to find the ones you need by providing an overview of the main types of tests you can perform.

Why should I care about tests?

Have you ever changed a piece of code and some minutes latter discovered that small change broke everything? Or maybe it broke something you thought was not related to that change at all? That is a very common problem when you do not work with automated tests.

When you change something on your code, the ideal thing to do would be testing the whole system, to assure nothing is broken. However, doing it manually is impossible in almost all real world case scenarios. That’s why the tests are so important, because they can do that for you. They can test the whole system in just a few seconds!

By using automated tests, you will have three main benefits:

  • Quality: tests are not only about avoiding headaches for developers, they are also something that makes your project manager, clients and users happy. Once you have automated tests, the changes to deploy a bug will be much smaller because for each small change that you implement, the whole system can be tested to guarantee everything is working perfectly. Through this, you will be able to have high quality products.
  • Flexibility: automated tests get your back when you change something on your code. No matter how big or small the changes are, from a button color update to the development of a whole new feature, those tests will check your entire system in order to find possible errors. This will give you flexibility to maintain and update current features and to create new ones, without causing problems on the existing ones.
  • Documentation: for some people documentation can be a boring thing to do. It can also be hard and costly to maintain, since for every significant change you make on your system, you will have to update it. Well written tests have the power to document your classes, features or even business rules. They are a working specification of your code. I’m not saying that documentation is not important, in fact I believe it is a good practice. However I think we all should avoid overdocumenting. Having good tests that document your classes can help you concentrate on documenting the right things. Besides, any other developer that looks at the tests will understand a lot of the system only by reading them.

So now that you understand that tests are your best friend, it’s time to get to know them and to decide which ones you should use.

Which tests should I use?

When starting to work with automated tests, it is easy to get lost on deciding which ones to use. There are different types of tests and each one of them focuses on a specific aspect of the system.

So, to answer this question you will have to analyze your needs as well as your projects’s deadline and the cost benefit of them. So it will depend on the project you are working on. Maybe you will have enough time and resources to build all levels of tests and it would be amazing. Or maybe for your specific needs you don’t actually need all of those tests and that is also fine. You just have to consider these variables and think about them when deciding which ones to use.

But to choose a test you have to know them right? So, let’s see some tests you can implement and how they actually work.

Unit tests

A unit is the smallest testable piece of a system. Generally, in object oriented programming, it would be a method which has specific parameters and a specific return type.

Unit tests verifies if each one of those units is working as designed. One of the keys of unit testing is the isolation, you should be able to test each function in isolation. By doing so, if you get an error you will be able to trace which one of the methods is the cause.

Now, let’s see an example of a unit test. For those examples I will use PHPUnit, however the important thing here is to understand the logic behind the tests so you will be able to reproduce them using any other language.

The following code represents a calculator class. This class is responsible for implementing a sum functionality. For demonstration purposes, the behavior of this function is simple: it will receive two variables, sum their values and return the result.

So, to build a unit test for the sum function we should call this function, pass two variables and make sure that the returned value is the sum of the numbers. Your test would look something like this:

On line 12, we instantiate a Calculator object and, on line 14, we call the sum method passing 2 and 2 as variables. Then we verify that the result is 4. The assertEquals() function will verify that both values I passed to it are equal, in this case it will verify that the returned value of the sum() function is equals to 4. Pretty simple, right?

Another thing to point out is the name I used for the test function. If you take a look at it you will see that it documents the behavior of the class. It expresses how this function should work and what it should return. That’s an example of what I meant when I talked about the documentation advantage tests bring us.

Integration tests

Its goal is to find integration failures. In unit tests, you test each function in isolation, here you will build tests that verifies if the integration between those functions and other components, such as APIs and databases, are working properly. For example, let’s say your system saves a user to the database and you are using Eloquent. You can build a test to verify that it is actually saving the record to the database.

Functional tests

Those tests will test whole features of the software. They will verify if those functionalities are working as they should do.

Imagine a software that has a functionality: to show a dashboard to the logged in users. You can write the following tests to check if that feature is working.

The first test will verify that if a user accesses the dashboard route, he will be able to access it and see the content of the page (that in this case is a message). The second one, will be responsible for verifying that unauthenticated users cannot access the dashboard, once it should only be available to logged in users. In this way, you will make sure that the whole functionality is working properly.

Of course, there are also other kinds of tests such as regression tests and acceptance tests and I plan on making other post for them, but for now those presented here are great for you to start walking towards building testable systems.

Wrapping up

I hope at this point you are already convinced that automated tests are a great ally to software development and that you understand the benefits from using them. Also, I hope now you are able to build your own unit, integration and feature tests and understand when you can apply each one of them.

Feel free to comment here and share with me your suggestions and experiences on automated tests.

--

--

Rayane de Araújo

Data engineer at tembici. Big Data and Machine Learning Lover. Always seeking for professional and personal improvement.