Kotlin Spek - How to generate XML with tests report?

旧城冷巷雨未停 提交于 2019-12-25 05:50:27

问题


I am using Spek to test my Kotlin app. I would like to publish my tests report after Jenkins build. JUnit or TestNG would generate XML report which Jenkins can use to generate tests stats.

Does Spek generate such report? If so, how to configure my Gradle project to get it? If not, what are the other reporting options available?


回答1:


I haven't checked thoroughly my build directory. Since Spek is using JUnit 5 Platform Engine it will produce reports the same way as JUnit 5 would have done.

Indeed, after running ./gradlew clean build you can see the file here: ./build/reports/junit/TEST-spek.xml. I used Jenkins to publish JUnit XML report after build and it works fine.

If you'd like to change reports directory, you should configure it in Gradle build script as follows.

junitPlatform {
    reportsDir file("$buildDir/your/path")
    filters {
        engines {
            include 'spek'
        }
    }
}

Source, JUnit 5 User guide: http://junit.org/junit5/docs/current/user-guide/#running-tests-build




回答2:


I am currently using JaCoCo with Coveralls to integrate my (multi-module) project after the CI build so I might have slightly mistakes for a mono-module (I have adapted it) build but this is part of my research.

The first thing that you need to do is configure your build.gradle in order to have your tests working is applying the Jacoco plugin to your gradle:

apply plugin: "jacoco"

And then you have to enable the output:

jacocoTestReport {
    group = "Report"
    reports {
        xml.enabled = true
        csv.enabled = false
        html.destination "${buildDir}/reports/coverage"
    }
}

To generate the reports you can use: gradle test jacocoTestReport (Feel free to add just a jacocoTestReport to your already working command to build)

After the reports are generated now you have to send them to coveralls, this is done in a step after the compilation/testing has finished.

To send it to coveralls you need to add the gradle plugin for coveralls:

plugins {
    id 'com.github.kt3k.coveralls' version '2.7.1'
}

Create a rootReport task

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
    dependsOn = subprojects.test
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories =  files(subprojects.sourceSets.main.output)
    executionData = files(subprojects.jacocoTestReport.executionData)
    reports {
        html.enabled = true
        xml.enabled = true
        csv.enabled = false
    }
}

and add the kotlin sources for the coveralls task (only java support by default, Coveralls gradle plugin issue):

coveralls {
    sourceDirs += ['src/main/kotlin']
}

I stumbled upon a bug when doing it that needed this three lines when generating the jacocoRootReport but this is mostly for a multi module project (workaround source):

onlyIf = {
    true
}

The last step is configuring your CI tool to know where to find your coveralls token/properties (source). I personally have done it by adding environment variables and not coveralls.yml (it didn't work well).

And now you can add two steps to your after build:

gradlew jacocoRootReport coveralls

And you should see your reports in your coveralls page!

Jacoco and coveralls: https://nofluffjuststuff.com/blog/andres_almiray/2014/07/gradle_glam_jacoco__coveralls

Working example: https://github.com/jdiazcano/cfg4k/blob/master/build.gradle#L24



来源:https://stackoverflow.com/questions/42428850/kotlin-spek-how-to-generate-xml-with-tests-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!