Unit Test
- In Turkish
- Birim Testi
In short
A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
What is a unit test?
A unit test is a short piece of code that calls one small part of a program, called a unit, with known inputs and checks that the output matches what you expect. A unit is usually a single function, method, or class. If the result is different, the test fails and points you to the exact behavior that broke.
Most unit tests follow the Arrange, Act, Assert pattern: set up the inputs, call the code under test, then assert (check) the result. A test runner finds all the tests, runs them, and reports which ones passed or failed. Because unit tests avoid slow resources like databases and networks, often by replacing them with mocks, thousands of them can run in a few seconds.
Think of unit tests like checking each brick for cracks before you build a wall. Developers run them constantly on their own machines and in CI/CD pipelines, and they form the wide base of the testing pyramid, with fewer integration tests and end-to-end tests stacked above them.
Unit tests are often confused with integration tests. A unit test checks one piece in isolation, while an integration test checks that several pieces, such as your code and a real database, work correctly together. A bug that only appears when components interact will usually slip past unit tests.
At a glance
Key takeaways
- A unit test checks one small piece of code, such as a single function, in isolation.
- Most unit tests follow the Arrange, Act, Assert pattern.
- Unit tests are fast because they avoid real databases, networks, and file systems.
- They form the base of the testing pyramid and run on every code change.
Example
import { test } from "node:test";
import assert from "node:assert/strict";
function add(a, b) {
return a + b;
}
test("add returns the sum of two numbers", () => {
// Arrange and act: call the unit with known inputs
const result = add(2, 3);
// Assert: check the output
assert.equal(result, 5);
});Readers ask
What is the difference between a unit test and an integration test?
A unit test checks one piece of code in isolation, usually with its dependencies replaced by mocks. An integration test checks that several real components, such as your code and a database, work correctly together.
What makes a good unit test?
A good unit test is fast, independent of other tests, repeatable, and checks one behavior with a clear name. When it fails, the name and message should tell you what broke without any debugging.
How many unit tests should I write?
There is no fixed number. A good rule is to test every important behavior of your public functions, including edge cases such as empty inputs, boundary values, and error paths.
Often compared
See also
- Integration TestTesting & Quality, p. 12An integration test is an automated test that checks whether several parts of a system, such as code, a database, and an API, work correctly together.
- Test-Driven DevelopmentTesting & Quality, p. 34Test-driven development is a coding practice in which you write a failing test first, then write just enough code to pass it, and then clean up the design.
- MockingTesting & Quality, p. 16Mocking is a testing technique that replaces a real dependency, such as a database or API, with a fake you control so that code can be tested in isolation.
- Test CoverageTesting & Quality, p. 30Test coverage is a metric that measures how much of a program's source code is executed when its automated tests run, usually shown as a percentage.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- CI/CDDevOps & Cloud, p. 9CI/CD is a set of automated practices that build, test, and release code changes frequently, so software can be delivered to users quickly and safely.
- JestTesting & Quality, p. 13Jest is a popular JavaScript testing framework that bundles a test runner, assertions, mocking, snapshots and code coverage in one package with little setup.
- Test CaseTesting & Quality, p. 29A test case is a check of one specific behavior: starting conditions, input or steps, and the expected result that shows if the software behaves correctly.
- White-Box TestingTesting & Quality, p. 36White-box testing designs tests from knowledge of the code's internal structure, so that its statements, branches and paths are exercised and checked directly.
Spotted a mistake or something missing on this page?Suggest an edit