Unit test
A unit test is an automated check of one small piece of code (a function, a class or a module) separately from the rest of the system: the test feeds in pre-chosen input and compares the result with the expected one. Unit tests most often cover pure functions, that is, functions with no dependencies: they touch no database, network or files and always return the same result for the same input, so the test runs in milliseconds. The border is blurry: because of accumulated technical debt, real code has few pure functions, and the part under test often calls a database or a network. Then its dependencies are replaced with mocks and the unit test stays fast. Unit tests work at the application level: they check code inside one program, while its interaction with other systems is checked by E2E tests. Example: a discount function is checked on several amounts without starting the whole shop. A project usually has the most unit tests: they are fast and show exactly which part of the code is faulty.
Frequently asked questions
What is a unit test?
An automated check of one small piece of code separately from the rest of the system: the input is given in advance and the result is compared with the expected one.How is a unit test different from an E2E test?
A unit test checks one part of the code and runs in milliseconds. An E2E test checks a chain of scenarios across several systems, is slow and runs on a stand.What is a pure function and how does it relate to unit tests?
A pure function has no dependencies: it touches no database, network or files and answers the same way for the same input. It is the easiest to cover with a unit test, and no mocks are needed.Why do unit tests use mocks?
Real code often has few pure functions because of technical debt, and the part under test calls a database, a network or a third-party service. A mock replaces those dependencies so that the result depends only on the part itself and the test stays fast.Who writes unit tests?
Usually the developer who writes the code under test, alongside it or before it.Source: Wikipedia: Unit testing
Where it appears · 1
- MCP server and AI agent for 1C · Harness