Start trefwoorden te typen om de site te doorzoeken. Druk enter om te verzenden.
Generative AI
Cloud
Testing
Artificial intelligence
Security
Unit testing is a very important quality measure that supports the IT delivery objective to deliver quality at speed. (note: unit testing is also called component testing) The main goal of unit testing is to verify that the implementation does what it intends to do. It is about individually testing the smallest units of code, referred to as modules or components. This allows developers to isolate each component, and whenever they are not according to expectation, fix problems at an early stage of the development lifecycle. Developers aim to test each part of the software in individual components as early as possible in the development process. For example, you have a password field and the requirements are that it needs to be at least 8 characters long, must contain alphanumerical characters and at least one symbol. Good practice is to create test cases that meet these criteria and, more important, also test cases that do not meet these criteria. So, a password like “secret01” should fail and “Secret123!” should pass the test.
Writing unit tests basically is just like writing production code. Well-written tests are assets while badly written tests are a burden. Following unit testing principles helps in creating good unit tests that pay off more than they cost.
The aim of DevOps teams is to build quality in. Testing the code on desired functionality and quality while writing the code is therefore an effective and efficient way to verify that the implementation actually does what it is intended to do. Some benefits of unit testing are:
Unit tests are an effective way to find faults or detect regressions, but unit tests should not be the only testing that is done. Unit tests, by definition, examine each unit of code separately. Unit testing (as part of Test Driven Development) supports designing software components robustly. But when an application is run for real, all those units also have to work together, and the whole is more complex and subtle than the sum of its independently tested parts. So other varieties of testing must also be organized by the team. For example, refer to the testing pyramid for more information.
That sounds great, but still some people may wonder: Why do you actually want a secondary system to help design or verify your code? Doesn’t your source code itself express the design and behavior of your solution?
The benefit of unit testing is correlated with the non-obviousness of the code under test
If you have code of any normal length, already it is not obvious at a single glance – so working out its exact behavior would take time and careful thought. Next additional design – and verification assistance (e.g., through unit testing) is essential to be sure that all situations are handled correctly. For example, if you’re coding a system of business rules or parsing a complex hierarchical string format, there will be too many possible code paths to check at a glance. In scenarios like these, unit tests are extremely helpful and valuable.
Unit testing takes time, apply good practices to be efficient
Designing and executing unit tests of course will require effort and time. On the other hand, it also brings benefits which makes this investment worthwhile.Here we give some examples of the efforts needed for designing and executing unit tests.
Some people avoid improving and refactoring application code out of fear that it may break a lot of unit tests and hence incur extra work. This of course is reversing the idea of quality engineering, keeping unit tests in sync with the application code is part of the work and when applying test driven development, creating and improving unit tests is just part of the development process.
To write good unit tests apply the “FIRST-U” rules: Fast, Isolated/Independent, Repeatable, Self-validating, Timely and Understandable.
These rules are described below:
The typical anatomy of every unit test is arrange-act-assert or given-when-then. This pattern is a standard across the industry. In the arrange (given) section the unit which is tested is initialized in a specific state. Mocks are created, and the expected result is set. The act section is the actual execution of the unit test case. The assert section compares the actual output of the act section with the expected output set in the arrange section. Below is a snippet of Java unit test code which give an example of the anatomy of a unit test.
The cost of unit testing a certain code unit is very closely correlated with its number of dependencies on other code units.
Below the costs and benefits of unit testing are put in a simple diagram.
Sources:
https://blog.stevensanderson.com/2009/11/04/selective-unit-testing-costs-and-benefits.
https://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises
https://howtodoinjava.com/best-practices/first-principles-for-good-tests
Related building blocks:
Building Blocks
Related wiki’sRisk PokerPlanning PokerRoot Cause Analysis (RCA) Specification and Example (SaE)Test-Driven Development (TDD)Clean Code-architectureCode MaintenancePair programmingPairingTest design techniquesCode reviewUnit Testing PrinciplesCode coverageFeature togglesMonitoring of product quality Parallel testingMutation testingPath Testing (algorithm test)