In my gradle build script, I added doLast method to test task to run ant.junit task to execute tests from few jar files.
test << {
//run ant junit task with reports stored in $buildDir/test-results
//after ant junit completion and "test" task completion,
//how can I get the gradle generated html report include the above test-results?
}
How do I enhance this task to get the gradle html report benefit? I see that ant junit test xml reports are properly getting created in $buildDir/test-results along with other "gradle test" created xmls. However $buildDir/reports/tests" contains only. I was hoping that gradle will pick up the ant junit created test result xml files as well and include in its html report. But this is not happening. How can I get this behaviour?
I tried to create another task of type TestReport. But it also did not help.
task runTestsFromJar( type: TestReport ) {
destinationDir=file("$buildDir/reports/tests")
reportOn tasks.test, files("$buildDir/test-results/binary/test")
}
I am working with gradle 1.8.
I would suggest creating another task of type Test to replace your doAfter closure.
task antTests(type: Test){
//configuration here
}
I believe at that point you can use the TestReport task in a similar manner as your previous attempt
task runTestsFromJar( type: TestReport ) {
destinationDir=file("$buildDir/reports/tests")
reportOn test, antTests
}
My attempts to generate a report based on a directory of existing xml result files, and on the binary subfolder were unsuccessful as well
Based on the response from gradle forum post, it seems generate gradle styled test html report is not available out-of-the box with gradle. Gradle TestReport task seems to be dependent on the binary output files generated by "test" task. These are not generated when the tests are run using ant.JUnitTask from gradle.
I resorted to "ant JUnitReport task" finally to generate at least a meaningful consolidated report.
test << {
//run ant junit task with reports stored in $buildDir/test-results
//after ant junit completion
ant.junitReport( toDir: "$buildDir/reports") {
fileset ( dir:"$buildDir/test-results" )
report ( format:"frames", todir:"$buildDir/reports" )
}
}
This gives a basic html report. One can customize as needed using XSLT.
Related
I created Spring Boot project which uses gradle build system. I want to run one separate test class by custom gradle task to be able depend on it in other tasks. Now I can do it with this code:
import org.apache.tools.ant.taskdefs.condition.Os
def gradleWrapper = Os.isFamily(Os.FAMILY_WINDOWS) ? 'gradlew.bat' : './gradlew'
task runMyTest(type: Exec) {
workingDir "$rootDir"
commandLine gradleWrapper, ':test', '--tests', 'com.example.MyTest'
}
Obviously, this is not a very beautiful solution, because it launches an additional Gradle daemon. I tried before another solution:
task runMyTest(type: Test, dependsOn: testClasses) {
include 'com.example.MyTest'
}
But it is not working (do not execute my test class).
UPD: I tried yet another solution:
task runMyTest(type: Test) {
filter {
includeTestsMatching "com.example.MyTest"
}
}
It fails with this error message:
Execution failed for task ':runMyTest'.
> No tests found for given includes: [com.example.MyTest](filter.includeTestsMatching)
However, obviously, my test exists, since running the test through the command line produces the correct result.
UPD2: I missed useJUnitPlatform() inside my test task. It was in the default test task (written to my build.gradle by Spring Boot initializer), but not in the custom task.
You can do it using a TestFilter.
Using includeTestsMatching you can specify your class.
If you need to specify a single test method, you can use includeTest "com.example.MyTest", "someTestMethod".
task runMyTest(type: Test) {
useJUnitPlatform()
filter {
includeTestsMatching "com.example.MyTest"
}
}
I want to have a very simple clone of a gradle task like this:
project.tasks.register("runTests") {
it.group = "otherGroup"
it.dependsOn("jvmTest")
}
The problem is, that the source task (jvmTest) is a test task. And when I do the simple dependsOn like in my example, intellij does not recognize it as a test task (e.g. it only displays the output in the build terminal but does not show the fancy green, yellow and red test symbols)
I tried to add it as follows
project.tasks.register("runTests", Test::class.java){
it.group = "otherGroup"
it.dependsOn("jvmTest")
}
but this will result in a NullPointerException when running it
dependsOn only creates an execution dependency on the task graph in Gradle, it will not create a different test task nor will it clone the one depended upon.
Have a look at the sample on how to create additional tests.
Is there already a possibility to generate an HTML report when JUnit tests were started via Gradle? Any hint or comment is appreciated.
UPDATE
Gradle 4.6 provides built-in support for the JUnit Platform which allows you to run JUnit Jupiter tests using the standard Gradle test task which generates HTML reports out of the box.
Answer for Gradle versions prior to 4.6
The JUnit Platform Gradle Plugin generates JUnit 4 style XML test reports.
These XML files are output to build/test-results/junit-platform by default.
So, if your build server knows how to parse JUnit 4 style XML reports, you can just point it to the XML files in that directory and let the build server generate the HTML report for you.
However, if you are asking if Gradle can generate an HTML report for your tests run via the junitPlatformTest task, then the answer is "No, unfortunately not." The reason is that the standard Gradle test task only generates HTML reports based on its own proprietary "binary" report format. Since the junitPlatformTest task does not generate reports in Gradle's binary format, Gradle itself cannot generate HTML reports for JUnit Platform tests.
Having said that, however, there is in fact a work around: you can use Ant within your Gradle build. Ant has a task for aggregating JUnit 4 based XML reports and generating an HTML report from those aggregated reports. The output is not very modern, but it is at least human readable. The downside is that the default XSLT stylesheet does not display the test class names for tests run via the JUnit Platform.
In any case, you can configure Ant's JUnitReport task in Gradle as follows.
junitPlatform {
// configure as normal
}
configurations {
junitXmlToHtml
}
task generateHtmlTestReports << {
def reportsDir = new File(buildDir, 'test-reports')
reportsDir.mkdirs()
ant.taskdef(
name: 'junitReport',
classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator',
classpath: configurations.junitXmlToHtml.asPath
)
ant.junitReport(todir: "$buildDir/test-results/junit-platform", tofile: "aggregated-test-results.xml") {
fileset(dir: "$buildDir/test-results/junit-platform")
report(format: 'frames', todir: reportsDir)
}
}
afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
generateHtmlTestReports.dependsOn(junitPlatformTestTask)
check.dependsOn(generateHtmlTestReports)
}
dependencies {
// configure as normal ...
junitXmlToHtml 'org.apache.ant:ant-junit:1.9.7'
}
Then, executing gradle check will generate an HTML report in build/test-reports/index.html.
Regards,
Sam (Core JUnit 5 committer)
Adding below line to my java command created TEST-junit-jupiter.xml in my target/test-result folder. This xml file has all info about number of testcases run, number of tests passed/failed etc
--reports-dir target/test-result
Yes, you can using Jacoco plugin.
Here is an example:
apply plugin: 'war' or apply plugin: 'java'
apply plugin: "jacoco"
test {
reports.junitXml.destination="build/test-results"
jacoco {
destinationFile = file("build/jacoco/jacoco.exec")
append=true
}
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
Regards.
I have a junit test suite that runs 4 test classes. When I run the test suite using gradle it creates 4 html reports, 1 for each test class in the suite. I'm new to gradle, is there a way to have gradle combine the results into a single html report?
Here is my test suite.
#RunWith(Suite.class)
#Suite.SuiteClasses([
TestClass1.class,
TestClass2.class,
TestClass3.class,
TestClass4.class,
])
class MyTestSuite {
}
In my gradle.build file I the following test method.
test {
include("com/geb/tests/MyTestSuite.class")
jvmArgs '-Dsomevariabe=someValue'
}
I run my test suite with the gradle command: gradle :web-tests:test
Then when the test suite has completed running there are 4 html files created for each class. Saved in web-tests\build\reports\tests\classes
testClass1.html
testClass2.html
testClass3.html
testClass4.html
Each testClass.html file lists each test cases pass or fail status.
I'd like to have a single html file containing a combined list of all the test cases pass or fail statuses. Is this possible?
This plugin can help you. Add this to your build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.kncept.junit.reporter:junit-reporter:2.1.0'
}
}
plugins {
id 'com.kncept.junit.reporter' version '2.1.0'
}
apply plugin: 'com.kncept.junit.reporter'
junitHtmlReport {
// The maximum depth to traverse from the results dir.
// Any eligible reports will be included
maxDepth = 5
// Directory where to search (exact or relative to build path)
testResultsDir = '/path/to/root/of/project'
// Where to output
testReportsDir = 'reports/junit'
// Fail build when no XML files to process
failOnEmpty = true
}
Now build the report with:
./gradlew junitHtmlReport
Creates an HTML report of all JUnit XML files it finds to ./reports/junit. Note that this combines files like TEST-*.xml and not the html files.
I'm working on a Java project that uses Gradle as its build system.
I want to add some bdd tests using Cucumber-JVM. Following this example I was able to configure Gradle's build.gradle to have a task called cucumber, and I was able to execute that task using "gradle cucumber".
But what I am looking for is a way to have Gradle run that task automatically during its test phase (where it runs all the other regular unit tests). I also want the build to be flagged as failed if any of the cucumber tests fail (strict=true).
Is this possible? I don't know a lot about Gradle and Google so far has produced nothing really useful.
Okay so I figured out how to achieve this. Simply add this to build.gradle:
test << {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--strict', '--monochrome', '--plugin', 'pretty', '--glue', 'com.mypackage', 'src/test/resources']
}
}
All you really need are the feature file and the steps java files that implement the glue code. You do not need the xxxCukesTest.java file with a #RunWith annotation since Gradle ignores it. You may want to keep it anyway because it enables you to run tests from your IDE.
Works really neat!