I am using a gradle file to build my project. In this file I am using jacoco to produce a test report. What I want to do is modify the build file so that it displays a message if my test coverage isn't 100%. Or at least just display the test coverage. Is this possible?
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
sourceSets.main.java.srcDirs = ['src']
sourceSets.test.java.srcDirs = ['test']
dependencies {
testCompile group: 'junit', name: 'junit', version: "4.+"
}
repositories {
mavenCentral()
}
jacocoTestReport {
doFirst {
classDirectories = files('build/classes/main/draw')
}
reports {
xml.enabled false
csv.enabled false
html.destination "build/reports/coverageHtml"
}
}
task(runui, dependsOn: jar, type: JavaExec) {
main = 'gui.Main'
classpath = sourceSets.main.runtimeClasspath
}
defaultTasks 'clean', 'test', 'jacocoTestReport', 'runui'
There is a very simple Gradle plugin called gradle-jacoco-log that simply logs the Jacoco coverage data:
plugins {
id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}
Then after ./gradlew jacocoTestReport, it shows:
Test Coverage:
- Class Coverage: 100%
- Method Coverage: 100%
- Branch Coverage: 81.2%
- Line Coverage: 97.8%
- Instruction Coverage: 95.3%
- Complexity Coverage: 90.2%
There are also some options to customize what is logged.
The other topic of enforcing a certain test coverage is covered by Gradle these days.
Full disclosure: I am the author of this little plugin.
At the moment this is not supported by the gradle jacoco plugin. You can vote for this issue at https://issues.gradle.org/browse/GRADLE-2783 and https://issues.gradle.org/browse/GRADLE-2854. As a workaround you could possibly parse the output file in a custom task.
You can use Gradle plugin gradle-console-reporter to report various kinds of summaries to console. JUnit, JaCoCo and Cobertura reports are supported.
Related
Having the following task registered in a gradle build script:
tasks.register<Test>("unitTest") {
useJUnitPlatform {
excludeTags("integrationTest")
}
}
And the tests tagged with
import org.junit.jupiter.api.Tag
#Tag("integrationTest")
class MyIntegrationTest {
...
}
./gradlew unitTest still does run the tagged integration-test as well, I tried several other options provided by the gradle documentation to exclude the integration tests, though nothing seemed to work by now.
So I am working with SonarQube and I keep getting stuck. My coverage is 0% while it should not be 0% (I made sure with an simple test that always should run correctly and covers at least one method).
I am reading a lot of different gradle.build files on the internet, but I can't find one that works for me.
Also I think the problem is that sonarqube can't find certain files, but I can't find a working directory of someone which I can compare with my own.
Gradle.build
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "org.sonarqube" version "3.0"
id 'jacoco'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
implementation('io.jsonwebtoken:jjwt:0.2')
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile 'junit:junit:4.12'
implementation 'org.modelmapper:modelmapper:2.4.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r'
/**
* JUnit jupiter with mockito.
*/
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.19.0'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
}
sonarqube{
properties{
property 'sonarjava.source', '1.8'
property 'sonar.java.coveragePlugin', 'jacoco'
}
}
test {
useJUnitPlatform()
}
This is the folder where my test reports are in (I think you will need this to help me):
The build directory:
The jacoco test directory
Can anyone help me with my problem? If you want to see directories or other files, just ask and add them to the post.
Thanks in advance!
you need to make sure jacoco runs before sonarqube, so add something like:
tasks["sonarqube"].dependsOn("jacocoTestReport")
Also, side note, compile is deprecated, so use implementation and testImplementation instead.
The answer was in a directory that I had not shown. My project was missing the Jacocoreportxml file. Once I added this with the gradle build jacocoreportxml command in my terminal it worked!
I thought it would be a good idea to set up MarkLogic unit testing, so I've been following the unit-test-project as best as I can. Everything seems to work, but no unit tests are being performed. I've copied the four unit tests to src/test/ml-modules/root/suites/My Tests and the one module to src/main/ml-modules/root/. Everything deploys correctly. But running the unit tests gives me:
$ gradle mlUnitTest
> Task :mlUnitTest
Constructing DatabaseClient that will connect to port: 8021
Run teardown scripts: true
Run suite teardown scripts: true
Run code coverage: false
Running all suites...
Done running all suites; time: 1791ms
0 tests completed, 0 failed
The unit testing UI loads in a browser, but it also displays no tests.
This is my build.gradle file:
buildscript {
repositories {
jcenter()
mavenLocal()
}
dependencies {
classpath "com.marklogic:marklogic-unit-test-client:1.0.0"
classpath "com.marklogic:ml-gradle:4.1.0"
}
}
plugins { id "com.marklogic.ml-gradle" version "4.1.0" }
repositories {
jcenter()
}
dependencies {
mlBundle "com.marklogic:marklogic-unit-test-modules:1.0.0"
}
And gradle.properties:
mlAppName=unitTestApp
mlHost=localhost
mlUsername=admin
mlPassword=*****
mlRestPort=8020
mlTestRestPort=8021
mlModulePaths=src/main/ml-modules,src/test/ml-modules
I'm using MarkLogic 10, on 64 bit Linux, ml-gradle 4.0
Ah. It looks like the test suites are expected under the project directory in
src/test/ml-modules/root/test/suites/SuiteName
I had put suites under root and missed the second test directory.
I have a .NET Framework project that is being built on a self hosted Windows build agent.
There is a step to run tests, and that step needs to provide code coverage reports and stats.
When i try using "dotnet test" the step runs and the tests complete, the .coverage files are also generated. When i check the build summary after it's complete i see the standard test results and report, and also the code coverage tab. The code coverage tab has a download link to get the file. There is no code coverage report. There is also a link to "Setup Code Coverage" on the initial build summary screen.
Why is there no code coverage report? and why is the "Setup Code Coverage" link still visible?
This is incredibly frustrating! I must be missing something incredibly obvious, but the docs suggest what i have done is correct.
Using VSTest task rather that dotnet tests results in the same outcome, but runs far slower.
displayName: dotnet test
inputs:
command: test
arguments: '--configuration $(BuildConfiguration) --collect:"Code Coverage"'
workingDirectory: '$(Build.SourcesDirectory)\src'```
I eventually achieved this by using Hugh Lin's answer for help and modifying for my own purposes.
We have Coverlet as a reference in the project, and ReportGenerator installed into Azure DevOps, so that made this a little easier.
I found that we had an issue with a SOAP API reference that was causing the huge performance issues with generating a report. Once I filtered that out with a "classfilter"the process became more manageable. I also found that without the "disable.coverage.autogenerate" variable the "PublishCodeCoverageResults" task will take forever and likely fail as it tries to do the "ReportGenerator" step itself by without the "classfilters". It does this because the "ReportGenerator" is built into the "PublishCodeCoverageResults" step now, but due to having no filters it doesn't work for this scenario.
This is running against a .NET Framework project so there were a few adjustments to the projects needed to ensure "dotnet test" works successfully.
variables:
disable.coverage.autogenerate: 'true'
- task: DotNetCoreCLI#2
displayName: dotnet test
inputs:
command: test
publishTestResults: true
arguments: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-restore'
workingDirectory: '$(Build.SourcesDirectory)\src'
configuration: "$(buildConfiguration)"
- task: reportgenerator#4
inputs:
reports: '$(Build.SourcesDirectory)\src\*.UnitTests\coverage.cobertura.xml'
targetdir: '$(Common.TestResultsDirectory)/CoverageReport/'
classfilters: '-NAMESPACE*'
- task: PublishCodeCoverageResults#1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)\src\*.UnitTests\coverage.cobertura.xml'
reportDirectory: '$(Common.TestResultsDirectory)/CoverageReport/'
By default, the code coverage for the dotnet test task is output to a .codecoverage file, which Azure DevOps does not know how to interpret and only provides as a downloadable file. Code coverage Tab only supports code coverage data in Jacoco or Cobertura formats. So the result of the *.coverage file can not be shown by tables and graphs.
If you want more detailed code coverage report, you need to use coverlet in .Net framework by install the tool during the pipeline and then generate the report. For example in PowerShell script:
dotnet tool install dotnet-reportgenerator --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .\reports
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin\*test*.dll" }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }
Then add Publish code coverage task:
For details, you can refer to the case mentioned in the comment and this ticket.
We are not able to run test cases written with Junit 5 and Spock framework together in one gardle project?
We tried add dependencies given in https://www.baeldung.com/junit-5-gradle to our gradle file. Gradle version is 4.10.3 and Junit 5. Below is my build.gradle file
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven {
url "http://repo.fusesource.com/nexus/content/groups/public/"
}
maven {
url "https://repository.jboss.org/nexus/content/groups/public"
}
jcenter()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile(
'org.codehaus.groovy:groovy-all:2.4.8',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.jmockit:jmockit:1.8',
'junit:junit:4.12'
)
testRuntime(
'cglib:cglib:2.2.2',
'com.athaydes:spock-reports:1.2.7'
)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
}
test {
useJUnitPlatform()
testLogging { showStandardStreams = true }
}
I have created two test cases, one is using spock framework and other is using junit 5. But when I do gradlew -test, it runs only test cases written with Junit 5. Below is build path.
You need the Vintage test engine to execute Spock tests since they are based on JUnit 4, and you need the Jupiter test engine to execute the JUnit Jupiter tests.
So you need dependencies on both engines.
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
I'd also recommend that you upgrade to JUnit 5.5.1 (i.e., the latest and greatest).