This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate report file based on TestResult
- 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
Showing
6 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/groovy/com/ullink/gradle/extentreport/ExtentReportDotnetPlugin.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/groovy/com/ullink/gradle/extentreport/ReportGenerator.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/groovy/com/ullink/gradle/extentreport/ReportGeneratorDownloader.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
src/test/groovy/com/ullink/ExtentReportDotnetPluginTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |