The Reliable Power of the FIRST Principle: A Comprehensive Guide
Tags: programming, cleancode, testing, unittest
Published: 2023-09-05 23:32:38

---
title: The Reliable Power of the FIRST Principle: A Comprehensive Guide
published: true
date: 2023-07-31 13:02:15 UTC
tags: Programming,CleanCode,Testing,UnitTest
canonical_url: https://samurai-developer.com/unit-tests-first-principle/
cover_image: https://i0.wp.com/samurai-developer.com/wp-content/uploads/2023/08/pexels-photo-577585.webp?w=800&ssl=1
---

Writing unit tests correctly is as important as coding features. To achieve this, certain principles help ensure that unit tests are easy to use and modify when adding new test cases. The FIRST principle, which stands for Fast, Independent, Repeatable, Self-validating, and Timely, is a guide for us in this endeavor

## (F)ast

Unit tests should execute quickly to verify that the system behaves as expected during fixes, refactoring, or when new features are added. If running tests for a project takes a substantial amount of time, developers will tend to use them less frequently. Waiting 30 minutes to check if adding a few lines of code breaks something is not ideal.

Therefore, it’s crucial to focus on creating fast-running tests. This can be achieved by configuring tests to run in parallel or ensuring that the context setup process is not overly time-consuming.

## (I)ndependent

Independence among tests is crucial because modifying one test should not break others. If a test’s modification causes failures, it becomes less reliable and harder to maintain. To ensure independence, it is essential to verify that tests are executed randomly, which is generally the case. Additionally, leveraging mock tools to isolate the code under test provides the determinism needed for thorough testing.

## (R)epeatable

A unit test is repeatable if executing it multiple times produces the same result consistently. Using mock tools should make the untested code behave consistently, regardless of when or in what order it is executed. A typical example of failure to achieve repeatability is observing a project that relies on system objects to represent dates, causing test failures on January 1st each year.

## (S)elf-validating

A self-validating test is one that doesn’t rely on manual checks to determine success or failure, reducing the risk of human errors. Additionally, a self-validating test prepares its own context before execution, avoiding the need for manual tasks that could impact the test results.

## (T)imely or Thorough

“Timely” means writing tests for code in the same timeframe as the code itself. Tests help quickly identify issues and ensure that everything functions correctly, allowing dynamic changes and improvements throughout the feature development process.

On the other hand, “Thorough” reminds us not only to test the happy path but also to cover potential error scenarios when executing the code. For example, if a function takes a parameter, we should test not only valid values but also null values (if the language allows) and other boundary cases.

Both “Timely” and “Thorough” principles are not mutually exclusive; they should be applied together.

## Summary

Apart from the first principles there are other aspects to remember include.

- Simplicity: Simple code is easier to read and modify. 
- The “Single Responsibility” principle of [S.O.L.I.D](https://samurai-developer.com/follow-principles-solid/) also applies to tests. When designing tests, consider that each test should focus on verifying one specific thing, making it easier to identify what is functioning correctly or not in the source code.

Lastly, regarding unit test coverage, while it is often necessary for development processes to reach a certain percentage of test coverage before deploying a feature, developers must understand that coverage does not guarantee the correct functioning of the entire system. It merely indicates that tests have executed in specific parts of the code. Tests may not necessarily aim to validate those specific functionalities. Consequently, test coverage should be accompanied by a comprehensive understanding and complemented by other quality metrics for tests.

For more information, see [Mutation Testing](https://en.wikipedia.org/wiki/Mutation_testing).


---
![Samurai Developer Icon](https://i0.wp.com/samurai-developer.com/wp-content/uploads/2023/05/cropped-katana-1.png?fit=32%2C32&ssl=1
)

The post [The Reliable Power of the FIRST Principle: A Comprehensive Guide](https://samurai-developer.com/unit-tests-first-principle/) first appeared on [Samurai Developer](https://samurai-developer.com).