Skip to content

Commit

Permalink
Adding code coverage for Maven and Gradle projects (MicrosoftDocs#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rishavsharan authored and davidstaheli committed Nov 26, 2018
1 parent a3dd26b commit 5e9f51c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.idea/

*.iml
target/
8 changes: 8 additions & 0 deletions azure-pipelines.Gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ steps:
testResultsFiles: '**/TEST-*.xml'
tasks: 'build'

# Publish Cobertura or JaCoCo code coverage results from a build
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'JaCoCo'
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/reports/code-cov-report.xml'
reportDirectory: '$(System.DefaultWorkingDirectory)/**/reports/code-cov-report.html'
failIfCoverageEmpty: true

- task: CopyFiles@2
inputs:
contents: '**/helloworld*.jar'
Expand Down
8 changes: 8 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ steps:
testResultsFiles: '**/TEST-*.xml'
goals: 'package'

# Publish Cobertura or JaCoCo code coverage results from a build
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'JaCoCo'
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/site/jacoco/jacoco.xml'
reportDirectory: '$(System.DefaultWorkingDirectory)/**/site/jacoco'
failIfCoverageEmpty: true

- task: CopyFiles@2
inputs:
contents: '**/*.war'
Expand Down
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'jacoco'

group = 'helloworld'
version = '1.0-SNAPSHOT'
Expand All @@ -16,3 +17,14 @@ repositories {
dependencies {
testCompile group: 'junit', name: 'junit', version:'4.11'
}

jacocoTestReport {
reports {
xml.enabled true
xml.destination "${buildDir}/reports/code-cov-report.xml"
html.enabled true
html.destination "${buildDir}/reports/code-cov-report.html"
}
}

check.dependsOn jacocoTestReport
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,47 @@

<build>
<finalName>helloworld</finalName>

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</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>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<properties>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/microsoft/demo/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.microsoft.demo;

public class Demo {
public void DoSomething(boolean flag){
if(flag){
System.out.println("I am covered");
return;
}

System.out.println("I am not covered");
}
}
5 changes: 4 additions & 1 deletion src/test/java/MyTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import org.junit.*;
import com.microsoft.demo.Demo;
import org.junit.Test;

public class MyTest {
@Test
public void test_method_1() {
Demo d = new Demo();
d.DoSomething(true);
}

@Test
Expand Down

0 comments on commit 5e9f51c

Please sign in to comment.