Skip to content
BENIBIX Learn

Unit Test

Development🧪 Testing & QualityTest Types

Also known as Unit Testing, Unit Tests

A test that checks one small piece of code, like a function, by itself.

Think of it like

Think of it like a box of bricks. You check each brick before you build.

Example

Emma's function adds VAT to a price. Her unit test checks that 100 becomes 120 at a 20% rate, in 2 milliseconds.

Why it matters

They are fast and point at the exact function that broke. That makes them cheap to run on every save.

Definition

A unit test checks one small piece of code, like a single function, on its own. It gives the function an input and checks the output. It does not touch the network, a database or the disk, so it runs in a few milliseconds. A project can have thousands of them and still run them all in under a minute. Unit tests are the base of most test suites.

Example code

javascript
test('adds 2 + 3', () => {
  expect(2 + 3).toBe(5);
});
A unit test in Jest: one small check, no network, no database.

Common mistake

Thinking passing unit tests mean the app works. The pieces can each be right and still not fit together.

Key takeaways

  1. 1
    One small piece. Usually one function.
  2. 2
    On its own. No network or database.
  3. 3
    Very fast. Milliseconds each.

Compare