Modules
Intermediate⏱ 7 min read

Unit Testing Guide

sign in to track

Unit testing is the process of testing individual functions, methods, classes, or small components of a software application in isolation from the rest of the system.

The main goal of unit testing is to verify that a small piece of code behaves correctly for different inputs and conditions. Unit tests are usually automated, which allows them to run quickly and frequently during development.

Because unit tests focus on small parts of the application, they can help developers identify problems close to where they were introduced. They are also generally faster and less expensive to run than larger tests that involve multiple components or the complete application.

Example

Suppose an application has a function that calculates the total price of an order:

calculateTotal(price, quantity)

A unit test could verify:

  • calculateTotal(100, 2) returns 200

  • calculateTotal(50, 3) returns 150

  • calculateTotal(0, 5) returns 0

  • Invalid input is handled according to the expected behavior

The test focuses only on the calculateTotal() function rather than testing the entire application, database, payment system, or user interface.

Key Characteristics of Unit Testing

  • Small scope: Tests a specific function, method, class, or component.

  • Isolation: The unit is tested independently from unrelated parts of the system, often using mocks or stubs for dependencies when needed.

  • Fast execution: Unit tests can usually run very quickly.

  • Repeatable: The same tests can be executed repeatedly with consistent results.

  • Automated: Unit tests are commonly written as automated tests and can run as part of development or CI/CD pipelines.

  • Specific feedback: When a test fails, it can help identify the specific piece of code that may have a problem.

Why Unit Testing Is Important

Unit testing helps teams:

  • Find defects early in development.

  • Verify that individual pieces of code work as expected.

  • Reduce the risk of breaking existing functionality when code changes.

  • Make refactoring safer by providing automated checks.

  • Provide fast feedback to developers.

  • Build confidence in the reliability of individual components.

Unit Testing vs Larger-Level Testing

Unit testing focuses on small individual pieces of code, while other testing levels examine interactions between components or the complete application.

For example:

Unit Testing:
Does the calculateTotal() function return the correct result?

Integration Testing:
Does the order service correctly communicate with the payment and database services?

System Testing:
Does the complete application allow a user to successfully place an order?

◈Key Points

  • A unit test should test one thing and have a single reason to fail.
  • Use the AAA pattern: Arrange (setup), Act (execute), Assert (verify).
  • Mock external dependencies (databases, APIs, services) to keep tests fast and isolated.
  • Aim for high unit test coverage of business logic, but don't worship the coverage number.
  • Unit tests double as living documentation of intended behaviour.
⚡ Expert Pro Tip

Write unit tests before fixing bugs — first write a failing test that reproduces the bug, then fix it. This prevents regression and proves your fix works.