About types of code coverage

Code coverage

Summary of this article

This article describes the following three types of coverage.

--Statement coverage (C0: instruction coverage) --Branch coverage (C1: branch coverage) --Simple condition coverage (C2: condition coverage)

Sample code used in the explanation

 if (Conditional statement a1||Conditional statement a2) { //Judgment condition A

Statement 1 } else{ Statement 2

} if (conditional statement b1 && conditional statement b2) {// Judgment condition B Statement 3 }


Statement coverage (C0: instruction coverage)

Conclusion

--The percentage of executable statements (instructions) that were executed in the test **
⇒ For 100% coverage, all executable statements must be executed at least once

In the case of the above sample code

--Three test cases are required to execute statements 1 to 3

Branch coverage (C1: branch coverage)

Conclusion

--Of all the judgment conditions, ** the ratio of judgment conditions executed in the test **
⇒ In order to achieve 100% coverage, it is necessary to execute the truth of each judgment condition at least once.

In the case of the above sample code

--The following two test cases are required (* It can be confirmed that the truth of each judgment condition is executed at least once)

  1. Judgment condition A: True, Judgment condition B: False
  2. Judgment condition A: False, Judgment condition B: True

Simple condition coverage (C2: condition coverage)

Conclusion

――Of all the conditional statements, ** the ratio of the conditional statements executed in the test **
⇒ In order to achieve 100% coverage, the truth of each conditional statement must be executed at least once.

In the case of the above sample code

--The following two test cases are required (it can be confirmed that the truth of each conditional statement has been executed at least once)

  1. Conditional statement a1: true, conditional statement a2: false, conditional statement b1: true, conditional statement b2: true
    → Judgment condition A: ** True **, Judgment condition B: True
  2. Conditional statement a1: False, Conditional statement a2: True, Conditional statement b1: False, Conditional statement b2: False
    → Judgment condition A: ** True **, Judgment condition B: False

――In addition, even if the coverage of C2 is 100% as in the above example, the coverage of C0 and C1 is not always 100%.

reference

-About coverage (C0 / C1 / C2 / MCC) in white-box testing -What is coverage analysis

Recommended Posts

About types of code coverage
About disconnect () of HttpURLConnection
About selection of OpenJDK
About DI of Spring ①
About DI of Spring ②
[Swift] About enumeration types
About form. ○○ of form_with
About @Accessors of Lombok
About the handling of Null
About an instance of java
[Swift] Types of types-Basic knowledge-
About simple operation of Docker
[Ruby] Various types of each
About the description of Docker-compose.yml
About Java class loader types
About size comparison of compareTo
Code Climate Code quality, coverage measurement
Implementation of unit test code
Memorandum of understanding about LOD.
About partial match of selector
About the behavior of ruby Hash # ==
About fastqc of Biocontainers and Java
About Lambda, Stream, LocalDate of Java8
About error handling of comment function
[Rails] About implementation of like function
About binding of Spring AOP Annotation
What is it? ~ 3 types of "no" ~
About the role of the initialize method
About removeAll and retainAll of ArrayList
To be aware of easy-to-read code
Think about the 7 rules of Optional
About image upload of jsp (servlet)
About Disk Cache of Glide 4 series
About Java primitive types and reference types
[Ruby] Review about nesting of each
[Rails] Types of associations (one-to-many / many-to-many)
List of types added in Java 9
Explanation about Array object of Ruby
Summary about the introduction of Device
About the log level of java.util.logging.Logger
Types of exceptions in business systems
Basic structure of Java source code
Perspectives to worry about when doing code reviews of web applications (Rails)