In CI, Run Fast and Fundamental Tests First
Tags: cicd, testing, productivity
Published: 2019-11-26 11:56:56

---
title: In CI, Run Fast and Fundamental Tests First
published: true
description: Get feedback from CI faster with pipelines that follow the test pyramid bottom-up.
tags: cicd, testing, productivity 
canonical_url: https://semaphoreci.com/blog/continuous-integration-fast-fundamental-tests-first
cover_image: https://thepracticaldev.s3.amazonaws.com/i/3tyq3xyohkni20e51oyk.jpg
---

While it’s great to keep your [CI/CD process](https://semaphoreci.com/cicd) fast, on many occasions you don’t even need to run all tests to get the feedback you need.

Unit tests run the fastest, because they test a very small piece of code in complete isolation from the rest of the system. For example, they usually don’t touch the database.

Unit tests deal with fundamental business logic, and are the most numerous, as is commonly depicted in the **Test Pyramid** diagram:

![Test pyramid](https://thepracticaldev.s3.amazonaws.com/i/uwtx6lcb8rgybqfjeien.jpg)

A failure in unit tests is a sign of a fundamental problem, which makes running the remaining high-level and long-running tests irrelevant.

For these reasons, projects with test suites that run for anything longer than a minute should prioritize unit tests in the [continuous integration (CI) process](https://semaphoreci.com/continuous-integration).

## How to Use Pipelines to Run Fast Tests First

A classic CI setup is to split all tests in parallel jobs:

![CI build with parallel jobs](https://thepracticaldev.s3.amazonaws.com/i/va61n57aip5em77y2a3m.jpg)

In this approach, the total CI build time is determined by the duration of the slowest parallel job. For example:

- If some unit tests have failed in less than a minute,
- But one of the jobs with UI tests runs for 6 minutes,
- Then you need to wait for 6 minutes to receive a report about unit test failures.

The unit tests that fail typically make some higher-level tests fail as well. However, in this case **the results of higher-level tests are irrelevant** and running them is a waste of time and resources.

A better approach is to use customizable [CI/CD pipelines](https://semaphoreci.com/blog/cicd-pipeline). This way you can configure sequential blocks of single or parallel jobs. As soon as one block fails, the pipeline stops.

![CI build as a pipeline with sequential blocks and parallel jobs](https://thepracticaldev.s3.amazonaws.com/i/7z59ypbvsor7ixykv0am.png)

This strategy allows developers to get feedback on trivial errors in seconds. It also encourages all team members to understand the performance impact of individual tests as the code base grows.

## More Tips for Fast Feedback

There are additional tactics that you can use with your CI system to get fast feedback:

- **Conditional stage execution** lets you defer running certain parts of your build for the right moment. For example, you can configure your CI to run a subset of end-to-end tests only if one of the related components was changed.
- **A fail-fast strategy** gives you instant feedback when a job fails. CI stops all currently running jobs in the pipeline as soon as one of the jobs has failed. This approach is particularly useful when running parallel jobs with variable duration.
- **Automatic cancelation of queued builds** can help in situations when you push some changes, only to realize that you have missed something small, so you push a new revision immediately, but then need to wait for twice as long for feedback. With this approach you get feedback on revisions that matter while skipping all the intermediate ones.

The same principle applies to stages in the delivery pipeline. For example, if you’re running [smoke tests after deployment](https://semaphoreci.com/community/tutorials/smoke-testing), you want to detect issues as quickly as possible.

What is your approach to getting fast feedback? Let me know in the comments. Happy building!