From 5e9f51cb1437b4c51bdb95960e36715d9c9f9112 Mon Sep 17 00:00:00 2001 From: Rishav Sharan Date: Mon, 26 Nov 2018 20:00:36 +0530 Subject: [PATCH] Adding code coverage for Maven and Gradle projects (#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 --- .gitignore | 5 +++ azure-pipelines.Gradle.yml | 8 +++++ azure-pipelines.yml | 8 +++++ build.gradle | 12 +++++++ pom.xml | 41 ++++++++++++++++++++++ src/main/java/com/microsoft/demo/Demo.java | 12 +++++++ src/test/java/MyTest.java | 5 ++- 7 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/microsoft/demo/Demo.java diff --git a/.gitignore b/.gitignore index 0adb93e6a..cdfca4bd3 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/azure-pipelines.Gradle.yml b/azure-pipelines.Gradle.yml index ee7ed4ad8..beaaf09de 100644 --- a/azure-pipelines.Gradle.yml +++ b/azure-pipelines.Gradle.yml @@ -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' diff --git a/azure-pipelines.yml b/azure-pipelines.yml index f56977e54..8cf79b32b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -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' diff --git a/build.gradle b/build.gradle index b34729bfd..8c0476fe2 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'java' apply plugin: 'maven' +apply plugin: 'jacoco' group = 'helloworld' version = '1.0-SNAPSHOT' @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0aa66b65c..310949ae8 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,47 @@ helloworld + + + + org.jacoco + jacoco-maven-plugin + 0.8.2 + + + prepare-agent + + prepare-agent + + + + report + prepare-package + + report + + + + post-unit-test + test + + report + + + + target/jacoco.exec + + target/jacoco-ut + + + + + + target/jacoco.exec + + + + diff --git a/src/main/java/com/microsoft/demo/Demo.java b/src/main/java/com/microsoft/demo/Demo.java new file mode 100644 index 000000000..3ab319fe3 --- /dev/null +++ b/src/main/java/com/microsoft/demo/Demo.java @@ -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"); + } +} \ No newline at end of file diff --git a/src/test/java/MyTest.java b/src/test/java/MyTest.java index 62ee40e00..9e1d2abed 100644 --- a/src/test/java/MyTest.java +++ b/src/test/java/MyTest.java @@ -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