Unit tests (also called module tests or component tests) are used in software development. Their purpose is to verify that individual software components work and function as intended.
Why are unit tests useful?
The following tasks are performed by unit tests as part of quality assurance:
- Unit testing uncovers bugs and accidental changes to functionality within software.
- In the case of feature enhancements, unit tests reveal unintended effects on the existing system.
- Thanks to unit tests, errors are already detected during development, so that error costs can be avoided.
- By finding and fixing bugs early, software development is accelerated.
- Unittests can be performed much faster than manual tests (especially if they are automated). As a result, development stages are passed through more quickly and release cycles are shortened.
- Unit tests help to document the system, as they show the intended functions and reactions of the tested module. If the objects are named sensibly (clean code), additional measures for documentation are unnecessary.
Automated Unit Testing
In software development using agile methods, unit tests are often performed as part of test-driven development and refactoring to quickly find errors and improve quality. In order that testing does not mean additional work and can be performed at low cost, unit tests run automatically thanks to so-called test automations.
Test frameworks call the individual test classes and execute their component tests. Mostly, the test results of test frameworks are output as a graphical summary.
What is important when developing unit tests?
If you and your team want to write your own unit tests, pay attention to the following points:
- The tests should work independently of each other so that the test results do not influence each other. If this is the case, the order of the tests does not matter.
- Unit tests should be short and easy to understand and should only test one property at a time.
- The tests should be fully automated so that they can be run as often as possible.
- Unit tests should only test the really relevant code (for example, no getters or setters) and are grouped around a test fixture (not around a class).
- Unit tests should be written before the code under test - especially in the context of test-driven development. This additional implementation effort should always be considered when planning new features and functions.
How do unit tests work?
Most tests follow a similar basic structure:
- An initial state is initialized.
- The operation under test is executed.
- The result is compared to a target value derived from the specification.
By whom are unit tests written?
In some companies, software development and quality assurance are still separated, so unit tests are not written by the developers themselves.
If your company works according to agile methods, the entire responsibility for the development and functioning of the software should remain in the same team, i.e. the team that writes the code for the software also develops the unit testing for it.
Conclusion on the subject of unit testing
Even if only the expected errors in applications can be found by Unit Testing (because only for these the tests are written), it developed as important measure for the quality assurance of software. Especially the automated testing of units accelerates the process of finding and fixing bugs at an early stage and supports iterative work, prototyping and continuous improvement of products, especially in agile software development.