Unit Testing
Also known as: Unit tests
Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.
Unit testing is a software testing practice in which the smallest testable parts of a program, typically individual functions, methods or classes, are checked automatically against their expected behavior. A unit test supplies known inputs, runs the code and asserts that the outputs or effects match what the programmer intended. A test for a function that formats dates, for example, might confirm that a given timestamp produces the expected text and that an invalid input raises an error. Tests are usually written in the same language as the code and run with a test framework.
To keep tests isolated and fast, dependencies such as databases, networks or clocks are often replaced with simplified stand-ins called mocks, stubs or fakes. Kent Beck's SUnit framework for Smalltalk and JUnit for Java, which he wrote with Erich Gamma, popularized a common structure that spread to many languages as the xUnit family. Unit tests sit at the base of the so-called testing pyramid, beneath integration tests that check how components work together and end-to-end tests that exercise a whole system. They are typically run on every change as part of continuous integration.
Well-maintained unit tests make it safer to change code, because a failing test points to the unit that has broken. They support refactoring and are the central tool of Test-Driven development, in which tests are written before the code they check. Unit tests have limits. Passing tests show only that the cases someone thought to write behave correctly, and heavy use of mocks can let tests pass while real integrations fail. Coverage figures measure how much code the tests execute, not whether they check the right things.
Key points
- A unit test runs a small piece of code with known inputs and checks the result against an expected outcome.
- Test doubles such as mocks and stubs isolate the unit under test from slow or unpredictable dependencies.
- The xUnit family of frameworks, descended from Kent Beck's SUnit and JUnit, set a widely copied structure for tests.
- High test coverage does not guarantee correctness, because tests only check the scenarios that were written.
From the source
...if a test fails, you can quickly isolate the area of the code that has the bug or error. Unit testing enforces modular thinking paradigms and improves test coverage and quality.
Related entries
Test-Driven Development
Software development practice in which a failing automated test is written before the code that makes it pass, after which the code is refined.
Continuous Integration
Development practice in which programmers merge changes into a shared repository frequently, with each change verified by an automated build and tests.
Refactoring
Process of restructuring existing code to improve its design, readability or maintainability without changing its external behavior.
Debugging
Process of finding, diagnosing and fixing defects in software or hardware that cause it to behave incorrectly or unexpectedly.
Kent Beck
American software engineer who created Extreme Programming, popularized test-driven development and co-wrote the Manifesto for Agile Software Development.
Code Review
Practice in which developers examine each other's source code changes to find defects, share knowledge and keep the codebase consistent.
CI/CD Pipeline
Automated sequence of stages that builds, tests and releases software changes, combining continuous integration with continuous delivery or deployment.
Featured in guides
Referenced by
Other entries that discuss or point to Unit Testing.