DEV Community

Cover image for The Reliable Power of the FIRST Principle: A Comprehensive Guide
Nicolas Villavicencio
Nicolas Villavicencio

Posted on • Originally published at samurai-developer.com on

The Reliable Power of the FIRST Principle: A Comprehensive Guide

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 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.


Samurai Developer Icon

The post The Reliable Power of the FIRST Principle: A Comprehensive Guide first appeared on Samurai Developer.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay