Skip to main content

Unit Test

In Turkish
Birim Testi
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/unit-test

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

A unit test in three steps: arrange a cart with one item, act by asking it for its total, and assert that the total is 20. The cart is tested on its own, with a fake price list instead of the real database.ARRANGEconst cart = new Cart(fakePrices)cart.add("book")ACTconst total = cart.total()ASSERTexpect(total).toBe(20)Cart.total()the unitfake price listinstead of the real database✓ passed · 3 ms
With nothing real to wait for, a test like this runs in milliseconds, so hundreds of them can run on every change.

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

A unit test using the Node.js built-in test runnerjavascript
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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings