Question connected to Cyclomatic Complexity

41 views
Skip to first unread message

kanb...@gmail.com

unread,
Oct 3, 2019, 12:49:58 PM10/3/19
to JaCoCo and EclEmma Users
Hi,

I have a question connected to Cyclomatic Complexity. According the documentation the "Cyclomatic Complexity is the minimum number of paths that can, in (linear) combination, generate all possible paths through a method". There is also a calculation for it v(G) = B - D + 1, but was not sure in my case.

I have simple method what I want to test:

public int increase(int i) {
if (i > 1) {
i = i+ 1;
}
if (i > 10) {
i = i+ 1;
}
return i;
}

And my test is very simple, I don't do any assertions, just call it with two different numbers:

@Test
void test() {
increase(100);
increase(-10);
}

I used the following coverage limit:
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>1.00</minimum>
</limit>

So I had the assumption that the coverage report should fail, as I didn't test (call) the method with a number which is 1 < n < 10, but to my surprise the coverage failed.

Do you know why it is passed in my case?

My simplified pom.xml file just in case:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kanbagoly</groupId>
<artifactId>something</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>1.00</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>




Thank you for your answer in advance,
Csaba Kerti

kanb...@gmail.com

unread,
Oct 3, 2019, 12:52:30 PM10/3/19
to JaCoCo and EclEmma Users
* "So I had the assumption that the coverage report should fail, as I didn't test (call) the method with a number which is 1 < n < 10, but to my surprise the coverage passed."

Marc Hoffmann

unread,
Oct 4, 2019, 2:25:13 AM10/4/19
to JaCoCo and EclEmma Users
Hi,

cyclomatic complexity is calculated based on branches. In your case the cyclomativ complexity is 3 and your two test cases cover all branches.

Path coverage is a different metric which is not supported by JaCoCo.

Best regards,
-marc


-- 
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/853c87a6-4dcb-469c-8a93-3fa153cd9c7b%40googlegroups.com.

kanb...@gmail.com

unread,
Oct 4, 2019, 6:29:54 AM10/4/19
to JaCoCo and EclEmma Users
Hi Marc,

Thank you for your quick answer and for helping me to understand it. Do you have a small example when the branch coverage would pass but the cyclomatic complexity would fail?

Thank you,
Csaba
To unsubscribe from this group and stop receiving emails from it, send an email to jac...@googlegroups.com.

Marc Hoffmann

unread,
Oct 11, 2019, 7:08:35 AM10/11/19
to jac...@googlegroups.com
There is no such example. Cyclomatic complexity coverage is derived from branch coverage.

Cyclomatic complexity coverage is something I invented for JaCoCo. I don’t think there is a official definition for it. Here are the definitions for JaCoCo:


Regards,
-marc


To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/39d3be91-cec1-4b46-8f42-d043779201bc%40googlegroups.com.

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages