Output test coverage with clover + gradle

About this document

Repository

I left the contents created when I tried it in the following repository mitsuya-saida/clover-sample

Dependence

Dependent License
clover Apache 2.0 license
bmuschko/gradle-clover-plugin Apache 2.0 license

Try to output the coverage for the time being

Move for the time being

Write that it depends on build.gradle

Add gradle-clover-plugin to dependencies in buildscript as below

buildscript {
	dependencies {
		classpath 'com.bmuschko:gradle-clover-plugin:2.1.3'
	}
}

Addition of settings to apply plugin

apply plugin: 'com.bmuschko.clover'

Added clover to dependencies

dependencies {
	clover 'org.openclover:clover:4.2.0'
}

Describe clover settings

clover {
	report {
		html = true
	}
}

The build.gradle finally completed by the project made with Spring boot is as follows

buildscript {
	ext {
		springBootVersion = '1.5.8.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath 'com.bmuschko:gradle-clover-plugin:2.1.3'
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'com.bmuschko.clover'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	mavenCentral()
}

clover {
	report {
		html = true
		filter = 'main'
	}
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compileOnly('org.projectlombok:lombok')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	clover 'org.openclover:clover:4.2.0'
}

Write a suitable test

Please

Output coverage

The test is executed by the following command and the coverage file is output.

$ gradle clean cloverGenerateReport

View the output result

Since the html of the report is generated in the build result, look at it with a browser In the case of this project, it is located in the following path

clover-sample/build/reports/clover/index.html

Looking at the browser, it looks like this

Commentary

Around the clover settings

Commands provided by clover

The available commands are as follows

Command Usage
cloverGenerateReport Run test and output coverage
cloverAggregateReports Aggregate test execution results for projects with multiple modules (I'm not sure because I've never used it)

Filter the test target

Try excluding the main function, getters, and setters from coverage

Move for the time being

Output coverage by writing the following in the clover settings

clover {
	contexts {
		method {
			name = 'main'
			regexp = 'public static void main\\(String\\[\\] args\\).*'
		}
	}
	report {
		html = true
		filter = 'main,property'
	}
}
Output result

Commentary

Miscellaneous feelings

Recommended Posts

Output test coverage with clover + gradle
Integration Test with Gradle
Remote debugging with Gradle test
JUnit 5 gradle test fails with lombok annotation
Java automated test implementation with JUnit 5 + Gradle
Build and test Java + Gradle applications with Wercker
Use ProGuard with Gradle
Install Gradle with ubuntu16.04
Combine Gradle multi-project test results and coverage into one
DataNucleus starting with Gradle
[Test code learning / output]
[Rails] Test with RSpec
Test Nokogiri with Rspec.
Automatically test with Gauge
Load test with JMeter
Unit test with Junit.
Use WebJars with Gradle
Check coverage with Codecov in Java + Gradle + Wercker configuration
Get started with Gradle
Use jlink with gradle
Output FizzBuzz with stream
[Java] I want to test standard input & standard output with JUnit
csv file output with opencsv
Java multi-project creation with Gradle
[Spring] Controller exception output test
Test Active Strage with RSpec
CSV output with Apache Commons CSV
Lombok not working with Gradle5
Output multiplication table with Stream
Control log output with Doma2
Test GraphQL resolver with rspec
Output "Izumi Oishi" with dokojava
Output JaCoCo coverage to console
Use log4j2 with YAML + Gradle
Hello World with SpringBoot / Gradle
Output Excel with formulas with XlsMapper
Understand code coverage with Rspec, the Ruby on Rails test framework