Skip to content

Commit 5e9f51c

Browse files
rishavsharanDavid Staheli
authored andcommitted
Adding code coverage for Maven and Gradle projects (MicrosoftDocs#3)
* Added Code Coverage * Adding CC to Yaml * Added Jacoco plugin to build file * Added Jacoco plugin to build file 2 * Added Jacoco plugin to build file 3 * Added Jacoco plugin to build file 4 * Added Jacoco plugin to build file 5 * Added Jacoco plugin to build file 7 * Added Jacoco based code cov for Maven and Gradle * Removing the JacocoRpeort bit from yaml
1 parent a3dd26b commit 5e9f51c

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@
2424

2525
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2626
hs_err_pid*
27+
28+
.idea/
29+
30+
*.iml
31+
target/

azure-pipelines.Gradle.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ steps:
1919
testResultsFiles: '**/TEST-*.xml'
2020
tasks: 'build'
2121

22+
# Publish Cobertura or JaCoCo code coverage results from a build
23+
- task: PublishCodeCoverageResults@1
24+
inputs:
25+
codeCoverageTool: 'JaCoCo'
26+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/reports/code-cov-report.xml'
27+
reportDirectory: '$(System.DefaultWorkingDirectory)/**/reports/code-cov-report.html'
28+
failIfCoverageEmpty: true
29+
2230
- task: CopyFiles@2
2331
inputs:
2432
contents: '**/helloworld*.jar'

azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ steps:
1818
testResultsFiles: '**/TEST-*.xml'
1919
goals: 'package'
2020

21+
# Publish Cobertura or JaCoCo code coverage results from a build
22+
- task: PublishCodeCoverageResults@1
23+
inputs:
24+
codeCoverageTool: 'JaCoCo'
25+
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/site/jacoco/jacoco.xml'
26+
reportDirectory: '$(System.DefaultWorkingDirectory)/**/site/jacoco'
27+
failIfCoverageEmpty: true
28+
2129
- task: CopyFiles@2
2230
inputs:
2331
contents: '**/*.war'

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
apply plugin: 'java'
22
apply plugin: 'maven'
3+
apply plugin: 'jacoco'
34

45
group = 'helloworld'
56
version = '1.0-SNAPSHOT'
@@ -16,3 +17,14 @@ repositories {
1617
dependencies {
1718
testCompile group: 'junit', name: 'junit', version:'4.11'
1819
}
20+
21+
jacocoTestReport {
22+
reports {
23+
xml.enabled true
24+
xml.destination "${buildDir}/reports/code-cov-report.xml"
25+
html.enabled true
26+
html.destination "${buildDir}/reports/code-cov-report.html"
27+
}
28+
}
29+
30+
check.dependsOn jacocoTestReport

pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@
1919

2020
<build>
2121
<finalName>helloworld</finalName>
22+
23+
<plugins>
24+
<plugin>
25+
<groupId>org.jacoco</groupId>
26+
<artifactId>jacoco-maven-plugin</artifactId>
27+
<version>0.8.2</version>
28+
<executions>
29+
<execution>
30+
<id>prepare-agent</id>
31+
<goals>
32+
<goal>prepare-agent</goal>
33+
</goals>
34+
</execution>
35+
<execution>
36+
<id>report</id>
37+
<phase>prepare-package</phase>
38+
<goals>
39+
<goal>report</goal>
40+
</goals>
41+
</execution>
42+
<execution>
43+
<id>post-unit-test</id>
44+
<phase>test</phase>
45+
<goals>
46+
<goal>report</goal>
47+
</goals>
48+
<configuration>
49+
<!-- Sets the path to the file which contains the execution data. -->
50+
<dataFile>target/jacoco.exec</dataFile>
51+
<!-- Sets the output directory for the code coverage report. -->
52+
<outputDirectory>target/jacoco-ut</outputDirectory>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
<configuration>
57+
<systemPropertyVariables>
58+
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
59+
</systemPropertyVariables>
60+
</configuration>
61+
</plugin>
62+
</plugins>
2263
</build>
2364

2465
<properties>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.microsoft.demo;
2+
3+
public class Demo {
4+
public void DoSomething(boolean flag){
5+
if(flag){
6+
System.out.println("I am covered");
7+
return;
8+
}
9+
10+
System.out.println("I am not covered");
11+
}
12+
}

src/test/java/MyTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import org.junit.*;
1+
import com.microsoft.demo.Demo;
2+
import org.junit.Test;
23

34
public class MyTest {
45
@Test
56
public void test_method_1() {
7+
Demo d = new Demo();
8+
d.DoSomething(true);
69
}
710

811
@Test

0 commit comments

Comments
 (0)