Building Better Apps with Automated Tests

Building Better Apps with Automated Tests


6 min read

Automated testing is an essential software development practice. Agile practitioners rely on automated tests to ensure that their apps are always in a working state. Often, developers who are new to automated testing raise concerns about the practice. However, once you become acclimated to testing, you will find that it is a valuable tool that speeds up development.

What is Automated Testing?

Automated testing is the practice of writing code that tests your code. Typically, automated tests are run as part of a continuous integration (CI) pipeline. That means that every time a developer makes a code change, the automated tests ensure that all previously written code still works as expected.

There are several types of automated tests. The most common type of test is a unit test. Unit tests are written to test a single “unit” of your code: it tests a small, self-contained piece of functionality. These tests are quick to write and run, and generally make up the majority of automated tests for an application.

Integration tests test the “integration” (interaction) between two or more units of code. For example, an integration test might test that a user can log in to your application. Such a test would require that the user interface, the authentication system, and the database all work together as expected.

End-to-end (E2E) tests test the entire application. Ideally, E2E tests fully test functionality from the user’s perspective. As an example, an E2E test might test that a user can log in to your application, navigate to a specific page, and see the expected content.

Integration tests are more complex to write and slower to run than unit tests. E2E tests are even more complex and slower to run. This is why unit tests make up the majority of automated tests for an application. Integration and E2E tests are incredibly valuable. However, due to the trade-offs involved, they are often used to test only the most critical functionality that can not be adequately covered by unit tests.

An Example of an Automated Test

Here’s a simple example of an automated unit test in a Ruby on Rails social media app. This test ensures that a Post model is associated with a user and has a valid body attribute.

RSpec.describe Post, type: :model do
  it { belong_to(:user) }
  it { validate_presence_of(:body) }
  it { validate_length_of(:body).is_at_most(255) }

Notice how simple this test is. You can see that it’s a unit test because it only tests a single “unit” of code: a Post model.

The it statements are assertions. Assertions are statements that test the behavior of your code. They’re a central part of automated testing: the assertions are what ensure that your application’s code is working as expected.

The above test is written using the RSpec testing framework. While RSpec is a popular choice for Ruby on Rails apps, there are plenty of testing frameworks to choose from for most programming languages and frameworks.

Benefits of Automated Testing

When your team adopts automated testing, you will quickly realize several benefits. A tested code base is unlikely to have regressions. Regressions are bugs that are introduced in old code when new code is added, typically due to unintended side effects.

Automated tests also help you when you need to change existing code to support new features. This is known as “refactoring.” If your app is adequately covered by a well-written test suite, refactoring carries little risk. When you make a breaking code change, your tests catch the problem.

You will write better code with automated tests as well. When you write a test, you are forced to think about how your code will be used. That means writing code in a modular way that is easy to understand. Modular code has benefits beyond testability. A modular code base is easier to understand, maintain, and extend.

Automated tests also speed up development. While there is an initial learning curve when you first start writing tests, the time you spend writing tests will quickly pay off. As you write more tests, you will find that you can make changes to your code with confidence. You will have clearer ideas about how to structure your code, and you will be able to test your changes more quickly. Automated tests aren’t just reserved for catching problems. You can also use them to experiment with new ideas and to try out different approaches to solving a problem. Instead of clicking around in an app, you can write a test case and run it over and over as you program until you get the intended result.

Reservations About Automated Testing

The time investment for automated testing is a common concern. New developers can not easily see the benefits of automated testing when it initially slows down their work. However, within a few weeks, most developers begin seeing the benefits of automated testing. They become faster and more confident in their work.

Another common concern is that automated tests prevent developers from making changes to the code. This can be true in a sense: tests prevent developers from breaking existing functionality when they make new changes. That can be frustrating when you’re new to automated testing. Instead of thinking of automated tests as a barrier to change, think of them as a safety net. They ensure that you can make changes to your code without breaking existing functionality.

Persuading Your Team to Invest in Automated Testing

Management is often hesitant to invest in automated testing. “There’s no time right now” is a common refrain. Managers and developers alike will say that the team can adopt better practices like automated testing after the next big release. However, in practice, what you don’t start today will rarely happen later.

If you face resistance in adopting automated testing, you can try to convince your team by showing them the benefits. Start by testing your own code. When other developers make code contributions, run your tests against their changes to ensure that the tests still pass. If you find a test failure, inform the team member and let them know how you discovered the problem.

As you gain experience with automated testing, you will be able to build complex features more quickly than before. Testers will find fewer bugs in your code, and you will be able to fix the bugs they do find more quickly. The quality and speed of your work will increase, making a clear case for the rest of the team to adopt automated testing.

How to Get Started with Automated Testing

To get started with testing, search for a popular testing framework for your programming language. PHP has PHPUnit, for example. Java has JUnit. Flutter apps use Flutter Driver. No matter your language or framework, there is a testing framework that will work for your app.