What is unit testing in software development

Bo WesdorpCo-founder and CTO, Miyagami Practice3 min read

Unit testing is the practice of checking the smallest testable pieces of a program, usually single functions or classes, in isolation. A unit test calls the code with known input and compares the result with the expected output. Because it avoids databases and networks, it runs in milliseconds and points at one cause when it fails.

What counts as a unit

A unit is the smallest piece of behaviour you can check on its own. In most codebases that is a function, a method or a small class. The word matters less than the boundary: a unit test should not need a running database, a network call or another service to give a result.

How does a unit test look

A typical unit test has three parts. You set up the input, call the code, and assert on the result. A test for a price calculation might pass a basket and a discount code and assert on the total. If the total is wrong, the failing test names the function to look at.

How do unit tests differ from integration tests

Unit tests check one piece alone. Integration tests check that several pieces work together, for example your code and a real database. End-to-end tests drive the whole product the way a user would.

Unit test Integration test End-to-end test
Scope One function or class Several components Whole product
Speed Milliseconds Seconds Seconds to minutes
Dependencies Replaced or avoided Real or close to real Real
Failure tells you Exactly where Which boundary That something broke
Typical share of a suite Largest Middle Smallest

This shape is often called the test pyramid: many fast unit tests at the bottom, fewer slower tests above.

What should you unit test

Test logic that can go wrong: calculations, validation rules, parsing, state changes and edge cases such as empty input, very large values and unexpected types. Skip code that only passes data through, and skip tests that restate the implementation line by line. A test that breaks every time you refactor, without catching a real bug, costs time and protects nothing.

What about mocks

A mock or stub replaces a dependency, such as a payment provider, so the unit can be tested alone. Use them at real boundaries and keep them simple. Heavy mocking often means the code is tangled, and the test then checks the mocks instead of the behaviour. When a dependency is cheap and deterministic, use the real one.

How do unit tests help with AI-written code

Coding agents write code faster than anyone can read it closely, so a fast, trustworthy test suite becomes the specification the agent has to meet. Good unit tests tell an agent what correct means for each function, and they tell a reviewer quickly when a change broke something. They do not replace judgement: an agent can write tests that pass while checking the wrong thing, which is why test coverage and confidence are different. Pair unit tests with careful review of AI-generated code, and use regression testing to protect behaviour over time.

What this means when you hire

Engineers who write tests alongside the feature ship code you can change safely. In our vetting, candidates work in a real codebase for two sixty-minute sessions, one with AI tools off and one with their own agents on, and both scorecards show how they verify their own changes. If you need engineers who build test discipline into delivery, see backend engineers, from €5,600 a month for a full-time engineer.

Short answers

What is the difference between unit testing and integration testing?

Unit tests check one function or class in isolation. Integration tests check that several components work together, such as your code and a database.

How many unit tests are enough?

Enough to cover logic that can fail, including edge cases. Chasing a coverage percentage matters less than testing behaviour you rely on.

Should AI-generated code have unit tests?

Yes. Tests give the agent a definition of correct and give reviewers a fast signal that a change did not break anything.

Do unit tests replace manual testing?

No. They catch logic errors quickly, while exploratory and visual checks still need a person.

Let's talk

Get a shortlist within three days

You share the roles and the stack in a short form or a thirty-minute call. Within three working days you get named senior engineers to review, each with both scorecards, and you can hire within five.

Reviewed onClutch39 reviews
ISO 27001
Certified

Book thirty minutes with Dale

The calendar is provided by HubSpot, which sets its own cookies. Load it here, or book on HubSpot's page.

Open booking page