Testing

What is Unit Testing?

Unit testing validates individual functions and components in isolation to verify they behave correctly, providing fast feedback and a safety net for refactoring.

By the Hyrax team·4 min read·May 1, 2026
TL;DR
  1. 1.What is a Unit?
  2. 2.Anatomy of a Unit Test
  3. 3.Test Doubles
  4. 4.The Testing Pyramid
  5. 5.Unit Testing Frameworks

Unit testing is the practice of writing automated tests that verify the behavior of individual, isolated units of code — typically a single function, method, or class. Unit tests run fast, provide immediate feedback, and form the base of the testing pyramid. They are the most numerous and frequently run tests in a healthy test suite.

What is a Unit?

A unit is the smallest testable piece of software. In object-oriented code, a unit is typically a class or method. In functional code, a unit is a function. The key property is isolation: a unit test verifies one thing and one thing only, with all dependencies replaced by controlled substitutes.

Anatomy of a Unit Test

Most unit tests follow the Arrange-Act-Assert (AAA) pattern:

  1. Arrange — set up the object under test and its dependencies
  2. Act — invoke the method or function being tested
  3. Assert — verify the output or behavior matches expectations

Test Doubles

Unit tests replace external dependencies with test doubles to maintain isolation:

  • Stub — returns predefined data when called
  • Mock — records calls and verifies they were made as expected
  • Spy — like a mock but wraps the real implementation
  • Fake — a working lightweight implementation like an in-memory database

The Testing Pyramid

The testing pyramid describes the recommended ratio of test types. Unit tests form the broad base because they are fast, cheap to write, and provide specific feedback. Integration tests sit above, and end-to-end tests form the narrow peak. Most of a test suite should be unit tests.

LevelSpeedCostCoverage Scope
UnitMillisecondsLowSingle function or class
IntegrationSecondsMediumMultiple components together
End-to-endMinutesHighFull user workflow

Unit Testing Frameworks

  • Jest — JavaScript/TypeScript; widely used for frontend and Node.js
  • pytest — Python; clean syntax and powerful fixtures
  • JUnit — Java standard library for unit testing
  • RSpec — Ruby; BDD-style test DSL
  • Go testing package — built into Go standard library
  • Rust built-in tests — native test framework in the Rust compiler

Unit Testing and Autonomous Code Governance

Hydra uses unit test results as a primary signal when evaluating automated code changes. A remediation PR generated by Hydra must pass the full unit test suite before it is opened for review. High unit test coverage also enables Hydra to make more confident automated fixes because the test suite provides comprehensive verification of expected behavior.

Frequently Asked Questions

How many unit tests should a codebase have?

There is no fixed number. Tests should cover all meaningful behaviors, edge cases, and error paths. Measuring branch or statement coverage gives a proxy; 80% branch coverage is a common threshold. Focus on meaningful behavioral coverage, not hitting a metric.

What should unit tests not test?

Unit tests should not test framework behavior, external services, or implementation details like private method names. Testing implementation details makes tests brittle and provides no value when implementation changes without behavior changing.

What is mutation testing?

Mutation testing evaluates the quality of a test suite by introducing deliberate bugs into the production code and checking whether tests catch them. A test suite that does not catch mutations has low confidence even with high line coverage.

What is test coverage?

Test coverage measures what percentage of the production code is executed by the test suite. Line coverage, branch coverage, and mutation score are common metrics. Coverage is a necessary but not sufficient indicator of test suite quality.

Frequently Asked Questions

What is the AAA pattern in unit testing?

Arrange (set up), Act (invoke), Assert (verify) — the three-phase structure of a well-written unit test.

What is a test double?

A substitute for a real dependency in a unit test. Types include stubs, mocks, spies, and fakes.

What is mutation testing?

Introducing deliberate bugs and checking if tests catch them, measuring how much the test suite can be trusted.

What percentage of tests should be unit tests?

Most of them — unit tests form the base of the testing pyramid because they are fast, cheap, and specific.

Stop flagging. Start fixing.

Hyrax reviews your pull requests, remediates issues autonomously, and closes the ticket.

Join the waitlist