Testing Types

Codecademy Team
In this article, you will be introduced to the different types of testing that may be used throughout the various stages of a project from local development to shipping to real users.

What are the Types of Testing?

At different stages of production for a particular project, you may encounter the opportunity for different types of tests, which can vary in scale and resource intensity, as well as serve different purposes. The types of testing we will discuss in this article are:

  • Unit tests
  • Integration tests
  • End to end tests

Testing levels art, demonstrating types of tests in a pyramid shape, smaller unit tests at the bottom, larger ent to end tests at the top

Throughout this article, we will discuss how each of these types of tests can be applied to a basic weather application.

Unit Tests

A unit test covers the smallest possible unit of testable code, such as a single function. In order to keep the scope of a unit test focused on the unit being tested, any data or behavior from other units or external sources that the unit relies on should be replaced with fake (mock) data or behavior.

For example, in a weather application, we might have a number of functions that each handle a small piece of computation, such as converting fahrenheit to celsius or formatting incoming weather data from an API. Unit tests would be written first to ensure that these functions can perform independently before we move on to testing how they work together. Any data that might come in from an external database or API would be mocked.

Integration Tests

An integration test covers how the units of a particular program work with one another. When testing integrations with external services, only the handling of incoming data is tested while the data itself remains mocked.

For example, in a weather application, integration tests would be written to ensure that weather data fetched from an API will be properly formatted to be displayed to the user. It would also ensure that delays, errors, or invalid data from the external service would be handled properly once they are introduced. The data itself would be mocked.

End to End Tests

An end to end test (sometimes referred to as a UI layer test or e2e) automates user flow to test the application in the way that a real user would experience it. To closely match the end user’s experience, this type of testing would also test interactions with external services such as databases and APIs.

For example, in a weather application, end to end tests might be written to simulate a user searching for a particular location, selecting that location, choosing celsius or fahrenheit, and clicking through various aspects of the UI. In this test, the actual database and external API is used.

How do these tests relate?

The order of tests in this article (unit -> integration -> end to end) represent an increase in resource intensity (time, computation, money) and the scope of the tests (amount of code being tested). Due to the increase in complexity, tests of these types may initially be written in this order. However, as an application is developed, new features may be tested using all three types simultaneously, with each providing different feedback to the developer or testing team.

A typical developer’s feedback loop using these various test types might be:

  1. Make code changes
  2. Make a pull request
  3. The code change has tests run against it (unit, integration, sometimes e2e)
  4. If there are any failures then the dev will work on fixes in their local development environment.
  5. Repeat steps 1-4 until all tests pass.
  6. The pull request is allowed to be merged.

Summary

In this article, we learned about the different types of tests that are written and performed throughout the production process. These include:

  • Unit tests, which test the smallest unit of code possible.
  • Integration tests, which test the interactions between internal services in the application.
  • End to end tests, which test the whole application including the external services it depends on.

These types of tests increase respectively in terms of resource intensity and amount of code being tested and each provide developers and/or testing teams with distinct feedback during the development cycle.