Code Abdeckungstool: Anweisungs-, Verzweigungs- und Entscheidungstests
⚡ Intelligente Zusammenfassung
Code coverage is a white-box measurement that reports the degree to which source code has been exercised by a test suite, helping teams locate untested statements, branches, conditions, and paths that hidden defects may occupy.

Was ist Code Abdeckung?
Code Berichterstattung is a measure which describes the degree to which the source code of a program has been tested. It is one form of White-Box-Tests which finds the areas of a program not exercised by a set of test cases. It also helps create additional test cases to increase coverage and to determine a quantitative measure of code coverage.
In most cases, a code coverage system gathers information about the running program. It then combines that with source code information to generate a report about the test suite’s code coverage.
Warum verwenden Code Abdeckungstests?
Hier sind einige Hauptgründe für die Verwendung der Codeabdeckung:
- It helps you measure the efficiency of test implementation.
- It offers a quantitative measurement of testing.
- Es definiert den Grad, in dem der Quellcode getestet wurde.
Code Abdeckungsmethoden
The following are the major code coverage methods:
- Aussagedeckung
- Entscheidungsabdeckung
- Zweigstellenabdeckung
- Toggle Abdeckung
- FSM-Abdeckung
Aussagedeckung
Aussagedeckung is a white box testing technique in which all the executable statements in the source code are executed at least once. It is used to calculate the number of statements in the source code that have been executed. The main purpose of Statement Coverage is to cover all the possible paths, lines, and statements in the source code.
Statement coverage is used to derive scenarios based upon the structure of the code under test.
In white box testing, the tester concentrates on how the software works. In other words, the tester concentrates on the internal working of the source code concerning control flow graphs or flow charts.
Generally in any software, if you look at the source code, there will be a wide variety of elements like operators, functions, looping, exception handlers, and so on. Based on the input to the program, some of the code statements may not be executed. The goal of Statement coverage is to cover all the possible paths, lines, and statements in the code.
Let us understand this with an example of how to calculate statement coverage. Here we take two different scenarios to check the percentage of statement coverage for each scenario.
Quelle Code:
Prints (int a, int b) { ------------ Printsum is a function int result = a + b; If (result > 0) Print ("Positive", result) Else Print ("Negative", result) } ----------- End of the source code
Szenario 1: Wenn A = 3, B = 9
The statements marked in yellow are those which are executed as per the scenario. Number of executed statements = 5, Total number of statements = 7, so Statement Coverage = 5/7 = 71%.
Szenario 2: Wenn A = -3, B = -9
The statements marked in yellow are those which are executed as per the scenario. Number of executed statements = 6, Total number of statements = 7, so Statement Coverage = 6/7 = 85%.
But overall, if you see, all the statements are covered by both scenarios. So we can conclude that the overall statement coverage is 100%.
Was ist durch die Kontoauszugsdeckung abgedeckt?
- Unbenutzte Aussagen
- Tot Code
- Unbenutzte Zweige
- Fehlende Aussagen
Testen der Entscheidungsabdeckung
Entscheidungsabdeckung is a white box testing technique which reports the true or false outcomes of each boolean expression in the source code. The goal of decision coverage testing is to cover and validate all the accessible source code by checking and ensuring that each branch of every possible decision point is executed at least once.
In this coverage type, expressions can become complex, making it challenging to achieve 100% coverage. This is why various methods are used to report this metric. These methods prioritize the most critical combinations. While it is similar to branch coverage, it provides greater sensitivity to control flow.
Example of Decision Coverage
Betrachten Sie den folgenden Code:
Demo(int a) { If (a > 5) a = a * 3 Print (a) }
Szenario 1: Value of a is 2. The “No” outcome of the decision If (a>5) is checked, so Decision Coverage = 50%.
Szenario 2: Value of a is 6. The “Yes” outcome of the decision If (a>5) is checked, so Decision Coverage = 50%.
| Testfall | Wert von A | Ausgang | Entscheidungsabdeckung |
|---|---|---|---|
| 1 | 2 | 2 | 50% |
| 2 | 6 | 18 | 50% |
Testen der Filialabdeckung
Zweigstellenabdeckung is a white box testing method in which every outcome from a code module (statement or loop) is tested. The purpose of branch coverage is to ensure that each decision condition from every branch is executed at least once. It helps measure fractions of independent code segments and find out sections that have no branches.
Wenn die Ergebnisse beispielsweise binär sind, müssen Sie sowohl die Ergebnisse „Wahr“ als auch „Falsch“ testen.
Die Formel zur Berechnung der Filialabdeckung:
Beispiel für Filialabdeckung
To learn branch coverage, consider the same example used earlier. Branch Coverage will consider the unconditional branch as well.
| Testfall | Wert von A | Ausgang | Entscheidungsabdeckung | Zweigstellenabdeckung |
|---|---|---|---|---|
| 1 | 2 | 2 | 50% | 33% |
| 2 | 6 | 18 | 50% | 67% |
Vorteile der Filialabdeckung:
- Allows you to validate all the branches in the code.
- Helps you ensure that no branch leads to any abnormality in the program’s operation.
- Removes issues that happen because of statement coverage testing.
- Allows you to find areas which are not tested by other testing methods.
- Allows you to find a quantitative measure of code coverage.
- Branch coverage ignores branches inside boolean expressions.
Testen der Zustandsabdeckung
Zustandsabdeckung, or expression coverage, is a testing method used to test and evaluate the variables or sub-expressions in a conditional statement. The goal of condition coverage is to check individual outcomes for each logical condition. Condition coverage offers better sensitivity to the control flow than decision coverage. In this coverage, only expressions with logical operands are considered.
For example, if an expression has boolean operations like AND, OR, or XOR, that indicates the total possibilities. Condition coverage does not guarantee full decision coverage.
Die Formel zur Berechnung der Bedingungsabdeckung:
For an expression with two operands, there are four possible combinations: TT, FF, TF, and FT. Consider the input X=3, Y=4 (x<y) TRUE and A=3, B=4 (a>b) FALSE, which gives Condition Coverage of 1/4 = 25%.
Finite-State-Machine-Abdeckung
Finite state machine coverage is certainly the most complex type of code coverage method. This is because it works on the behavior of the design. In this coverage method, you need to look at how many times specific states are visited or transited. It also checks how many sequences are included in a finite state machine.
Welche Art von Code Auswahl des Versicherungsschutzes
This is certainly the most difficult answer to give. In order to select a coverage method, the tester needs to check whether the:
- code under test has single or multiple undiscovered defects,
- cost of the potential penalty,
- cost of lost reputation,
- cost of lost sales, and so on.
Je höher die Wahrscheinlichkeit ist, dass Defekte zu kostspieligen Produktionsausfällen führen, desto höher ist die Deckungssumme, die Sie wählen müssen.
Code Abdeckung vs. funktionale Abdeckung
| Code Abdeckung | Funktionsabdeckung |
|---|---|
| Tells you how well the source code has been exercised by your test bench. | Measures how well the functionality of the design has been covered by your test bench. |
| Never uses a design specification. | Uses a design specification. |
| Done by developers. | Done by testers. |
Code Abdeckungswerkzeuge
Here is a list of important code coverage tools:
| Werkzeugname | Beschreibung |
|---|---|
| Cobertura | An open source code coverage tool. It measures test coverage by instrumenting a code base and analyzing which lines of code are executed and which are not when the test suite runs. |
| Clover | Klee (OpenClover) also reduces testing time by only running the tests which cover the application code modified since the previous build. |
| DevPartner | DevPartner ermöglicht Entwicklern die Analyse Java code for code quality and complexity. |
| Emma | EMMA supports class, method, line, and basic block coverage, aggregated at source file, class, and method levels. |
| Kalistick | Kalistick is a third party application which analyzes the code from different perspectives. |
| CoView und CoAnt | A code coverage tool for metrics, mock object creation, code testability, path and branch coverage, and more. |
| Volltreffer für C++ | BullseyeCoverage is a code coverage tool for C++ und C. |
| Echolot | Sonar is an open code coverage tool which helps you manage code quality. |
Vor- und Nachteile der Verwendung Code Abdeckung
| Vorteile | Nachteile |
|---|---|
| Helpful to evaluate a quantitative measure of code coverage. | Even when a specific feature is not implemented in the design, code coverage still reports 100% coverage. |
| Allows you to create extra test cases to increase coverage. | It is not possible to determine whether all possible values of a feature were tested using code coverage. |
| Allows you to find the areas of a program not exercised by a set of test cases. | Code coverage does not tell how much and how well you have covered your logic. |






