Code quality and technical debt

Unit testing

Also known as Unit tests

By WeavePublished 2 min read

Definition

Unit testing is the practice of checking a small, replaceable part of a program in isolation from its collaborators. A unit test usually supplies controlled inputs, observes behavior, and compares the result with an expected outcome.

Definition

A unit is a small piece of behavior that a developer can reason about independently. Depending on the language and design, it might be a function, class method, module, or another narrow boundary. The important property is not the file size. It is the test's ability to exercise the behavior without depending on a database, network service, clock, or other unstable external system.

What a unit test checks

A useful unit test makes the expected behavior visible. It might check a normal input, a boundary value, an invalid input, or an error path. Test doubles can replace collaborators when their real behavior is outside the unit's responsibility, but excessive mocking can make a test verify implementation details instead of the contract.

Unit tests are valuable because they usually run quickly and point to a narrow cause when they fail. Small, independent tests also make it practical to run feedback continuously as code changes. They do not remove the need to test how components work together.

Example

Imagine a function that calculates a shipping charge from order weight and destination. A unit test can pass a few representative weights and destinations to the function and assert the exact charge, including the boundary where a free-shipping rule begins. It can separately check that an unsupported destination produces the documented error.

The test does not need to call a carrier API. A separate integration test can verify that the application maps the carrier response into the function's inputs.

Limitations

Passing unit tests cannot prove that a database query, message contract, deployment configuration, or user journey works. A test suite with high coverage can still miss an incorrect requirement or assert the wrong result. Use unit tests for fast, focused feedback, then combine them with integration, system, and end-to-end checks that exercise important boundaries.

How this relates to Weave

Weave provides context for the delivery and review signals around a unit-testing practice, such as pull request activity, checks, rework, and change outcomes. Those signals can help a team investigate whether tests are slowing feedback, frequently failing, or associated with fewer regressions. Weave does not run unit tests or determine whether an assertion adequately covers a behavior, so test reports and code-level review remain the source of truth for that question.

Explore Engineering intelligence

Sources and further reading

  1. UnitTest, Martin Fowler