Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Generate report file based on TestResult
Browse files Browse the repository at this point in the history
- use the extent-report framework
- generate report once NUnit or OpenCover
finished generating the report
- added unit tests for report generation
  • Loading branch information
Eniko Kando committed Jul 10, 2019
1 parent c702b6d commit e1cffb5
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 1 deletion.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ description 'Uses extentreports-dotnet-cli to generate an HTML report based on a

dependencies {
compile(
[group: 'de.undercouch', name: 'gradle-download-task', version: '3.4.3']
[group: 'de.undercouch', name: 'gradle-download-task', version: '3.4.3'],
[group: 'com.ullink.gradle', name: 'gradle-nunit-plugin', version: '1.14'],
[group: 'com.ullink.gradle', name: 'gradle-opencover-plugin', version: '1.13']
)
testCompile 'junit:junit:4.11'
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
exclude module: 'groovy-all'
}
}

bintray {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package com.ullink.gradle.extentreport

import com.ullink.gradle.nunit.NUnit
import com.ullink.gradle.opencover.OpenCover
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

class ExtentReportDotnetPlugin implements Plugin<Project> {

@Override
void apply(Project project) {

Task extentReportDownloaderTask = project.task('reportDownload', type: ReportGeneratorDownloader)
Task reportingTask = project.task('nunitReport', type: ReportGenerator)

reportingTask.dependsOn extentReportDownloaderTask

project.afterEvaluate {
project.tasks.withType(OpenCover).each { task ->
task.finalizedBy(reportingTask)
}
project.tasks.withType(NUnit).each { task ->
task.finalizedBy(reportingTask)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ullink.gradle.extentreport

import com.ullink.gradle.nunit.NUnit
import org.gradle.api.tasks.Exec

class ReportGenerator extends Exec {

def EXTENT_REPORT_TYPE = 'v3html'
def EXTENT_REPORT_EXECUTABLE_PATH = 'tools/extent.exe'

@Override
protected void exec() {
if (isTestResultFileAvailable()) {
project.logger.info("Generating the report for the NUnit Test Results..")

generateReports()
} else {
project.logger.info("There is no available Test Result file based on which the report should be generated located at ${project.tasks.nunit.getTestReportPath()}")
}
}

boolean isTestResultFileAvailable() {
return project.tasks.nunit.getTestReportPath().exists()
}

File getCacheDirForExtentReport() {
new File(new File(project.gradle.gradleUserHomeDir, 'caches'), 'extent-report')
}

private def generateReports() {
NUnit nunit = project.tasks.nunit

project.exec {
commandLine = buildCommandForExtentReport(nunit.getTestReportPath(), nunit.getReportFolder())
}

def resultFile = new File(nunit.getReportFolder(), "index.html")
resultFile.renameTo new File(nunit.getReportFolder(), 'TestResult.html')
}

def buildCommandForExtentReport(def testResultPath, def outputFolder) {
return [getExtentReportExeFile().absolutePath, "-i", testResultPath, "-o", outputFolder, "-r", EXTENT_REPORT_TYPE]
}

File getExtentReportExeFile() {
def extentReportFolder = getCacheDirForExtentReport()
return new File(extentReportFolder, EXTENT_REPORT_EXECUTABLE_PATH)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ullink.gradle.extentreport

import de.undercouch.gradle.tasks.download.Download

class ReportGeneratorDownloader extends Download {

def EXTENT_REPORT_DOWNLOAD_URL = 'https://www.nuget.org/api/v2/package/extent/0.0.3'
def EXTENT_REPORT_DOWNLOADED_FILE_NAME = 'extentReport.zip'
def EXTENT_REPORT_EXECUTABLE_PATH = 'tools/extent.exe'


@Override
void download() {
ensureExtentReportIsAvailable()
}

void ensureExtentReportIsAvailable() {
if (!isExtentReportAvailable()) {
project.logger.info "Downloading extent report..."

getCacheDirForExtentReport().mkdirs()
downloadExtentReport()
} else {
project.logger.info "Extent Report already available in the local cache : ${getCacheDirForExtentReport()}"
}
}

boolean isExtentReportAvailable() {
def extentReportCacheDir = getCacheDirForExtentReport()
if (!extentReportCacheDir.exists()) {
return false
}

def extentReportExecutable = new File(extentReportCacheDir, EXTENT_REPORT_EXECUTABLE_PATH)
return extentReportExecutable.exists()
}

File getCacheDirForExtentReport() {
new File(new File(project.gradle.gradleUserHomeDir, 'caches'), 'extent-report')
}

void downloadExtentReport() {
def downloadedFile = new File(getTemporaryDir(), EXTENT_REPORT_DOWNLOADED_FILE_NAME)
def zipOutputDir = getCacheDirForExtentReport()

project.logger.info "Downloading & Unpacking Extent Report from ${EXTENT_REPORT_DOWNLOAD_URL}"

src "$EXTENT_REPORT_DOWNLOAD_URL"
dest downloadedFile
super.download()

project.copy {
from project.zipTree(downloadedFile)
into zipOutputDir
}
}
}
55 changes: 55 additions & 0 deletions src/test/groovy/com/ullink/ExtentReportDotnetPluginTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ullink

import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class ExtentReportDotnetPluginTest extends Specification{
@Rule TemporaryFolder testProjectDir = new TemporaryFolder()
File buildFile

def setup() {
buildFile = testProjectDir.newFile('build.gradle')
buildFile << """
plugins {
id 'base'
id 'nunit'
id 'com.ullink.opencover-nunit'
id 'com.ullink.extentreports-dotnet'
}
"""
}

def "nunitReport task is invoked once nunit task is finished"() {
given:
buildFile << """
nunit {
testAssemblies = ['-help']
nunitVersion = '3.9.0'
}
"""
when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments( 'nunitReport')
.withPluginClasspath()
.withDebug(true)
.build()
then:
def output = result.output
result.output.contains('nunitReport')
}

def "nunitReport task is invoked once opencover task is finished"() {
when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments( 'opencover')
.withPluginClasspath()
.withDebug(true)
.build()
then:
result.output.contains('nunitReport')
}
}
46 changes: 46 additions & 0 deletions src/test/groovy/com/ullink/ReportGeneratorTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.ullink

import com.ullink.gradle.extentreport.ReportGenerator
import java.nio.file.Paths
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class ReportGeneratorTest extends Specification {

private static File GradleUserHomeDir = new File("path", "GradleHomeDirForTests")

def "generating reports with Extent Report has the correct build arguments"() {
given:
def nunitReportGenerationTask = getReportGenerationTask()
def testResultFile = new File("TestResult.xml")
def reportOutputFolder = new File("reports/results")
when:
def extentReportCommandArgument = nunitReportGenerationTask.buildCommandForExtentReport(testResultFile, reportOutputFolder)
then:
extentReportCommandArgument == [nunitReportGenerationTask.getExtentReportExeFile().absolutePath, '-i', testResultFile, '-o', reportOutputFolder, '-r', 'v3html']
}

def "extent report executable has the correct path"() {
given:
def nunitReportGenerationTask = getReportGenerationTask()
when:
def extentReportExecutable = nunitReportGenerationTask.getExtentReportExeFile()
then:
def pathToTheExecutable = Paths.get("path", "GradleHomeDirForTests", "caches", "extent-report", "tools", "extent.exe")
extentReportExecutable.absolutePath
.contains(pathToTheExecutable.normalize().toString())
}

def "nunitReport task has the expected type"() {
given:
def nunitReportGenerationTask = getReportGenerationTask()
expect:
nunitReportGenerationTask instanceof ReportGenerator
}

def getReportGenerationTask() {
def project = ProjectBuilder.builder().withGradleUserHomeDir(GradleUserHomeDir).build()
project.apply plugin: 'com.ullink.extentreports-dotnet'
return project.tasks.nunitReport
}
}

0 comments on commit e1cffb5

Please sign in to comment.