unit test
/ˈjuː.nɪt test/noun
A unit test is an automated test of a small, defined software component, usually isolated from external systems.
The unit test checks whether the price calculation applies the correct discount.
A unit test specifies the expected result for a particular input to a small piece of software. This might be a VAT calculation or a rule determining whether a user gets a discount. Developers often run these tests when changing code so unexpected behaviour becomes visible quickly.
How does a unit test work?
A unit test usually has three steps. First, it puts the component in a known starting state. It then performs an action and compares the actual result with the expected one. If they differ, the test fails.
The test focuses on behaviour that can be checked independently. Dependencies such as a Glossary · In briefdatabaseA database is a structured collection of data that software can store, retrieve and modify. A database management system controls access to that data.Read more, external Glossary · In briefAPIAn API is a defined way for software to exchange data or call functions in other software without needing to know how that software works internally.Read more or clock are therefore often replaced with controlled test versions. This makes it clear which component causes a failure and keeps the test fast. Unit tests are well suited to frequent execution in Glossary · In briefcontinuous integrationContinuous integration is a practice in which developers regularly merge small code changes and check them automatically, making integration problems visible early.Read more.
How it differs from other automated tests
An Glossary · In briefintegration testAn integration test checks whether several parts of a software system work together correctly, focusing on their communication and the data they exchange.Read more checks whether several components work together, such as application logic storing data correctly in a database. An Glossary · In briefend-to-end testAn end-to-end test checks whether a complete user journey through an application works, including the components and systems that must work together.Read more follows a broader process as a user would, from input to visible result.
Unit tests therefore have a narrower scope. They provide fast, focused feedback but do not show whether all components work together. Teams usually combine them with broader tests. The amount of code executed by tests can be expressed as Glossary · In brieftest coverageTest coverage measures which parts of the code execute during automated tests. It can count lines, functions and branches, among other things.Read more, but a high percentage alone does not show whether the most important situations were tested properly.