Skip to content
IT glossary
TestingUpdated December 3, 2026

Unit testing

Юнит-тестирование

A unit test is an automated check that a small piece of code, such as a function or class, behaves as expected without running the whole application.

Also known as: модульные тесты, юнит-тесты

What counts as a unit?

A unit test takes a small piece of behavior and checks it under known conditions. For example, if orders of $30 or more ship free, a test can check that a $30 order gets a zero shipping fee without starting the checkout app. The unit might be a function, a class, or a few objects that implement one rule together. The point is to pin down behavior, not to write a separate test for every method.

How a unit test works

Most unit tests follow Arrange, Act, Assert: set up the inputs, run the code, and check what happened. The assertion might compare a return value, check that an exception was raised, or verify a meaningful side effect. A good test name tells you which rule failed before you read the test itself.

Make the test repeatable. If a rule depends on the date, pass in a date or supply a clock that returns a fixed time. If it depends on a network response, you can substitute a stub that returns a known result. There is no need to replace a fast, predictable helper just because it lives in another class. Avoid shared mutable state that makes tests pass or fail depending on their run order.

Example: test the cutoff

def shipping_fee(subtotal_cents: int) -> int:
    return 0 if subtotal_cents >= 3000 else 500


def test_shipping_is_free_at_the_cutoff():
    assert shipping_fee(3000) == 0


def test_shipping_is_charged_one_cent_below_the_cutoff():
    assert shipping_fee(2999) == 500

Using integer cents keeps the example focused on the shipping rule rather than rounding. The two tests separate adjacent inputs with different expected results. If >= accidentally becomes >, the first test catches the mistake. Testing only orders far above and far below the cutoff would miss it.

Where unit tests help — and where they stop

Unit tests are useful for business rules, validation, and regressions with small reproductions. They usually run quickly, so developers can run them while editing and again in CI. A useful regression test fails on the buggy version and passes after the fix.

A passing shipping test does not prove that checkout gives the function the right subtotal. An integration test can check that the parts exchange the right data, while an end-to-end test can exercise the checkout flow from a shopper's perspective. Likewise, a high line-coverage score tells you what ran, not whether the tests checked the right outcomes. Assert what callers can observe rather than how private helpers happen to be called, unless a particular interaction is itself part of the contract.

How it comes up in interviews

You may be handed a function and asked what to test first. State the expected behavior, then choose inputs that distinguish the main outcomes: a typical case, a boundary, and invalid input if the contract specifies how to handle it. If the code calls a database or an external service, say what you would substitute in a unit test and what you would still verify against the real dependency.