Why do we test our software?
Product specification: an agreement among the software development team that defines the product they are creating, detailing what it will be, how it will act, what it will do, and what it won't do.
Bug:
For the purposes of this course, a software bug occurs when one of the following is the case.
Let us exemplify bugs using a calculator.
Specification:
Bugs:
Here is the result of a an assessment of bug causes.
Most bugs can be traced to the product specification because the specification
Another relevant portion can be attributed to design because it may be
The third main category is only traceable to the mistakes in the code that maybe due to
The costs of fixing bugs grow logarithmically with time:
Definition: The activity to verify if a software or a portion of a software behaves according to its specification.
Types of testing:
A unit test is:
A small piece of code usually refers to a method or a class.
Fast is subjective, but you should aim for a test suite that runs in a few seconds.
Unit tests themselves should be run in isolation from each other, so that you run tests in parallel, sequentially, or in any order.
Unit tests are not suitable for testing complex user interface or component interaction.
From: Vladimir Khorikov, Unit Testing Principles, Practices, and Patterns (2020). Manning Publications. Unit Testing Principles, Practices, and Patterns
Consolidates the specification
Before we start with coding a component we must try to determine what the component must do.
The act of building test cases at the start helps to clarify the expected behavior of the component. We build the test case for the possible inputs and the possible outputs. If we are unable to come up with a test, it means that the specifications are not clear enough and require revision.
Early error detection
Unit tests are proof of working code. They are executed in every build and can detect failures at the first instance.
Unit tests can detect not only coding bugs but flaws in product specifications as well. A unit test demonstrates progress; thus, as soon as a component is complete, it can be demo-ed to the stakeholders to find gaps, if any. The sooner a bug is uncovered, the cheaper it is to fix.
Supports maintenance
Unit tests help in understanding the intended behavior in development cycles without being distracted by the actual code.
Product specifications evolve over time with changes that lead to development cycles. In each of these cycles, the team has to understand how the existing code works before team members can make any changes. A well-written unit test suite helps to keep the development in focus for the team.
Improves design
Unit tests make us think in terms of the expected input and the expected output.
Unit tests are the first client of the code being tested. They uncover various issues that a client can face while interfacing with the code being tested. Unit tests can help in classifying responsibility boundaries for the components. That can improve product specifications by exposing gaps in the interface design.
Product documentation
Unit tests describe how a piece of code works—that is, the expected output for a given input.
They always describe the latest state of a specification, as they are kept in sync with the code changes.
The percentage of code which is tested by unit tests.
Criteria:
It helps you to identify which portions of your code have been tested.
There are several testing frameworks available for Java, the most popular ones being:
In this course, we use JUnit 5.