Vezi pagina în română

IT Glossary

What are unit tests?

Automated checks, written by developers, that run a single function or method in isolation and confirm it returns the expected result.

7
sections
6
questions answered

Unit tests (the ISTQB standard also calls them component tests) are automated checks, written as code, that run the smallest testable part of a program, usually one function or method, in isolation and confirm that for a given input it returns exactly the expected result. Developers write them in their own working environment with dedicated libraries, and a whole suite runs in a few seconds at every change. Example: the function that calculates VAT receives 100 lei and must return 21 lei, the standard rate since 1 August 2025; when a rate changes, as it did from 19% to 21%, unit tests show at once every calculation still using the old value. They do not check how the pieces work together, though: integration tests and end-to-end tests exist for that.

Serious car manufacturers do not test the brakes for the first time on the track; every part is checked separately on a bench before assembly. For the company buying the software, the presence of unit tests is a quality indicator you can require in writing: a project delivered without them will be expensive to modify, because every future change becomes a risk taken with your product. They also happen to be the most honest documentation a codebase carries, because a test states in plain terms how a piece of logic is supposed to behave, and unlike a written specification it cannot quietly drift out of date without the build going red and somebody noticing.

Why it matters for your business

Calculation errors caught in seconds

Business rules covering prices, taxes and discounts are verified automatically at every code change, so a VAT mistake surfaces before release rather than inside customer invoices.

Cheaper changes in future

Code covered by tests can be changed quickly and without fear, which over years separates applications that keep evolving from the ones nobody wants to touch.

Living documentation for new people

Each test states plainly how a piece of code should behave, so an incoming team learns the project by reading tests instead of doing archaeology through undocumented code.

What a unit test looks like

A unit test has three steps, known as Arrange–Act–Assert. You prepare the data (a price of 100 lei and a rate of 21%), call the function that calculates VAT, and check the result (21 lei). The test name describes the behaviour, for example “calculates VAT at the standard rate”, so a failing test tells you by itself what broke.

A good test also covers the edge cases people forget: a zero price, a negative price, rounding to the smallest currency unit. A failing test in the delivery pipeline (CI/CD) stops the release. Each language has dedicated libraries: JUnit for Java, pytest for Python, PHPUnit for PHP, Jest or Vitest for JavaScript and TypeScript, xUnit or NUnit for .NET.

Isolation: mocks and stubs

Real functions depend on other things: the database, an external service such as the e-Factura API or a courier's, the system clock. In a test those dependencies are replaced with substitutes. A stub returns a fixed answer; a mock also checks that the function called it. That way the test runs in milliseconds and gives the same result every time.

There is a limit: a fully simulated test can pass even though the real connection to the external service does not work. That is why unit tests do not replace integration tests.

Want an estimate for your project?

Message us on WhatsApp. We reply the same working day, and the first 30-minute call is free.

Book a free call

Where unit tests stop

The ISTQB syllabus (version 4.0.1) orders the test levels as: component (the unit test), component integration, system, system integration, acceptance. Unit tests are the first level and developers write them; the last one, acceptance testing (UAT), is done by the users who receive the product.

In the test pyramid (Mike Cohn, 2009), unit tests form the base: many, small and fast, with few slow end-to-end tests at the top. Unit tests are part of a regression suite, but they are not the same thing: regression testing checks after every change that what worked before still works, whatever level the test runs at.

Coverage and cost

Code coverage shows what share of lines or branches ran while the tests executed. It says what ran, not that the result was checked. Google, on its testing blog (2020), says there is no ideal percentage and gives as an internal guideline 60% acceptable, 75% commendable, 90% exemplary. Money logic (prices, taxes, discounts, validations) deserves coverage first.

Cost: a Microsoft–IBM study published in 2008, covering four teams that wrote the tests before the code (TDD), found 40–90% fewer defects before release than in similar projects without TDD. The teams subjectively estimated 15–35% more initial development time. It is a single study and it measures TDD, not unit tests in general.

What to ask the supplier

Ask for unit tests on the calculation logic and the business rules, run automatically at every delivery and visible in the pipeline. Ask for the coverage report on the modules that handle money, not only a global percentage. Ask for the tests to be handed over with the source code, not just the compiled application. And ask for the rule “changed code = new or updated test”, so the tests do not fall behind the code.

Frequently asked questions

What unit test coverage counts as good?

There is no universal percentage. Google, on its testing blog (2020), says there is no ideal number and uses internally the guideline 60% acceptable, 75% commendable, 90% exemplary; above 90% across a whole project rarely pays. More important than the global figure: the areas involving money, meaning calculations, validations and rules, covered nearly completely.

Do unit tests make a project more expensive?

At the start, yes. A 2008 Microsoft–IBM study of four teams that wrote tests before the code (TDD) found 40–90% fewer defects before release; the teams subjectively estimated 15–35% more initial development time. The cheaper part shows up at every later change, when the tests catch mistakes on the spot.

If we have unit tests, do we still need other kinds of testing?

Yes. Unit tests verify the pieces in isolation but say nothing about how they behave assembled, which is what integration tests and end-to-end tests exist for, walking complete journeys through the application. The healthy pyramid has many fast unit tests at the base and progressively fewer, slower tests towards the top.

Who writes unit tests?

The developers who write the code, in their own development environment, according to ISTQB. Testers and the quality team check the levels above (integration, system, acceptance) and can ask for a new unit test for every defect found, so that it does not come back.

What is TDD (test-driven development)?

TDD is an order of work: you first write the test that fails, then the minimum code that makes it pass, then clean up both (ISTQB, after Kent Beck, 2003). Unit tests are the tool, TDD is the order in which you write them; you can have unit tests without TDD.

Which frameworks are used for unit tests?

Each language has its own: JUnit for Java, pytest for Python, PHPUnit for PHP, Jest or Vitest for JavaScript and TypeScript, xUnit or NUnit for .NET.