Unit Test Overview
This guide gives an overview of what a unit test is, and when and how it should be used.
Intended for
Software engineers, scrum masters, and test engineers.
What is a unit test?
A unit test is a code that validates that any other code (under test) works as expected.
Validation should ensure that the tested code:
- Executes successfully when the correct data is passed.
- Throws proper error messages when incorrect data is passed.
A unit test:
- Runs in memory.
- Is fully automated.
- Has complete control of everything that it does.
- Verifies individual software components or methods, also known as a "unit of work" or "unit of code".
- Is usually a white box test of a separate class or function to verify its behavior.
- Should only test the functionality of a single method, also called "method under test" or "system under test" (SUT).
Related definitions
- A unit of code is the smallest piece of code that can be tested.
- Unit testing is a practice in software development that validates that a unit of code will behave as intended and that the tested code will produce the same result every time the test is executed.
- The system under test refers to the code you're writing unit tests against. This could be an object, service, or anything else that exposes testable functionality.
- Code coverage measures the amount of code run by unit tests - lines, branches, or methods.
The goal
In unit testing, each part of a program (even a very complex one) is isolated to demonstrate that the individual parts are correct and that the requirements are satisfied.
The ultimate goal is to minimize bugs and allow developers to focus on the functionalities and logic of the product, relying on the fact that individual pieces of code are "correct".
When and how
Unit test cases are described (including steps and expected results) with the test code or in a test case work item linked to the test code.
Unit tests should only test code within the developer's control. They do not test infrastructure concerns (infrastructure concerns include interacting with databases, file systems, and network resources).
How to include the documentation depends on the framework and is included in the framework descriptions.
Test cases shall be independent of the order of test case execution. Code coverage is measured during execution and compared with the defined minimum acceptable level, which should be 80%.
All unit tests should be executed on all pull requests. It is also recommended to run unit tests on a regular basis.
The test result will be analyzed in the data of the pull request execution. The test must succeed for the pull request to pass.
All class interfaces will also be tested by exploring the boundary values of each parameter.
General characteristics
- First-class citizens (unit test code should be treated as good as your production code, like first-class citizens).
- Unit test code should be clean, readable, and maintainable, see Unit Test Writing Guideline.
- No code logic in unit tests (no
if
,else
, orfor
,foreach
,while
loops). - Isolated - one unit test should not call another unit test method.
- Not too specific or general - try to cover all scenarios like passing null, empty collection, and filled collection according to the characteristics of the system under test.