Sonarqube task in Gradle does not produce test coverage - unit-testing

I have multi-project Gradle script. I added to the root project:
buildscript {
repositories{
...
}
dependencies { classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2.1") }
}
apply plugin: org.sonarqube.gradle.SonarQubePlugin
sonarqube {
properties {
property "sonar.junit.reportsPath", "$projectDir/build/test-results/test"
property "sonar.host.url", "http://localhost:9000"
property "sonar.verbose", "true"
}
}
Sonarqube shows the correct number of tests, but coverage is 0.
I use Gradle 3.0, Java 1.8.0_45, Sonarqube 6.1.
Gradle console shows many "Class not found" messages.
Gradle console also shows message:
"Reports path contains no files matching TEST-.*.xml :
myPath\build\test-results\test"
, which is correct, since that particular project does not have any tests.

In my child project, I have:
apply plugin: 'java'
apply plugin: "jacoco"
jacocoTestReport {
reports {
xml.enabled = true
}
}
check.dependsOn jacocoTestReport
Run Gradle task "sonarqube" of the root project.

No SonarQube Analyzer or plugin executes your tests or the coverage calculation for you. You must do those things before analysis and feed the resulting report(s) into your analysis.

Related

How to create separate tests with Gradle and Kotlin?

I am working on a Kotlin project with Gradle that includes unit tests. I want to add some integration tests (or functional tests, never understood the difference between the two), but I want to be able to run them independently. Ideally, the source of the tests are in different folders.
I am using Gradle 4.5 and my build.gradle file looks something like this :
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = 'xyz.bobdudan.myproject.AppKt'
repositories {
maven { url "http://maven.stardog.com" }
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile 'io.kotlintest:kotlintest:2.0.7'
}
I have tried the method described here for java, but it doesn't work : the task also runs the unit tests, but they can't be found, and the integration tests are not executed at all.
What can i do ?
Edit :
Here is the result of gradle clean integTest with the solution of #lance-java:
:clean
:compileIntegTestKotlin
:compileIntegTestJava NO-SOURCE
:processIntegTestResources NO-SOURCE
:integTestClasses UP-TO-DATE
:integTest NO-SOURCE
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
So nothing is executed (I make sure that the tests are supposed to fail)
Note: I'm not a kotlin dev so will write in groovy.
You can add another SourceSet which will automatically add a JavaCompile task for the SourceSet and also a few Configuration instances to the model (eg integTestCompile, integTestCompileOnly, integTestRuntime etc)
sourceSets {
integTest {
java.srcDir 'src/integTest/java'
resources.srcDir 'src/integTest/resources'
}
}
configurations {
integTestCompile.extendsFrom compile
}
Then you can add another Test task
task integTest(type: Test) {
testClassesDir = sourceSets.integTest.output.classesDir
classpath = sourceSets.integTest.runtimeClasspath
}
You may wish to wire the integTest task into the normal DAG, or perhaps you'll leave this off and call it explicitly
check.dependsOn integTest
To tell long story short you have to:
create separate source sets,
create testTask for each test source,
set it up,
define order these tasks should be executed during build/check
and you are good to go.
Here is a guide in an answer to another question about how to do this for project using JUnit5 and Kotlin Spek test framework.

Running test task on gradle root project does not run test task on subprojects

In my multiproject I am running test task on root project and expecting that it will run test task on subprojects and produce a single test report. What I observe is that it never runs test task on subprojects. Is my expectation incorrect" DO I need to do any special configuration in my gradle scripts?
Note that I have no tests in my root project.
I think, this snippet from Gradle User Guide should help you out:
subprojects {
apply plugin: 'java'
// Disable the test report for the individual test task
test {
reports.html.enabled = false
}
}
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}

Gradle Task failing because JSHint detect improvement on code

I have a task that will run the Javascript test, and I'm trying to put JSHint to verify the code after run the tests, right?
So, this is a part of my build.gradle file:
apply plugin: 'com.eriwen.gradle.js'
def jsSrcDir = "src/main/resources/static/js/"
def jsSrcTestDir = "src/test/resources/"
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
}
}
test {
js {
srcDir jsSrcTestDir
include "*.js"
}
}
}
task jasmineTest(dependsOn: ['npmTest'],type: Exec)
jasmineTest.doFirst {
println "Running JSHint ----> SOURCE CODE"
commandLine "jshint", jsSrcDir
//println "Running JSHint ----> TEST CODE"
//commandLine "jshint", "src/test/resources/"
}
So, ok, it's running well, but is always failing, because JSHint are finding some improvements on my code, it's cool, but it's not my intention, I want to run the jasmine Test task, and does not matter if JSHint found or not an improvement, my task only need to fail if the test fail.
So, what I need to configure on my build gradle to put this working well?
Also, I have .jshintrc and .jshintignore configured.
Set ignoreExitValue = true in jasmineTest task.This will tell Gradle to ignore exit value else an exception thrown.
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html#org.gradle.api.tasks.Exec:ignoreExitValue

How to run integration tests from IntelliJ context menu for gradle project?

Using IntelliJ IDEA 14.0.2, I have imported a gradle java project. We have set up a sourceSet and configuration to separate integration tests from unit tests. (our integration tests are in the test source tree, but in their own package). Relevant bits from the build.gradle are:
sourceSets {
test {
java {
exclude '**/it/**'
}
}
integTest {
java {
srcDir 'src/test/java'
include '**/it/**'
}
resources {
srcDir 'src/test/resources'
}
compileClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
runtimeClasspath += sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
}
}
configurations {
integTestCompile.extendsFrom testCompile
integTestRuntime.extendsFrom testRuntime
}
idea {
module {
scopes.TEST.plus += [ configurations.integTestCompile ]
}
}
task integTest(type: Test) {
testClassesDir = sourceSets.integTest.output.classesDir
classpath = sourceSets.integTest.runtimeClasspath
}
This works fine from the command line. But when I open up the source of an integration test in IntelliJ and right-click to run it, IntelliJ launches the "test" task rather than the "integTest" task. How do I get IntelliJ to launch the correct task?
Alternatively, how can I make the test task delegate to another task based on the contents of the "--tests " arg?
Follow this: gradle settings > Gradle > Runner and check Delegate IDE build/run actions to gradle. Then apply and Ok.
Good luck!
Right-click on the test on file and you should see a menu option for Create Run Configuration >. Select that an in the dialog, modify the Tasks option. Change that to integTest and click OK. From that point, you might have to run the test using the menu system rather than the context system. i.e. Run > Run...
My solution is to configure IntelliJ to not use Gradle as a test runner, but to execute the tests with the built-in JUnit runner. This is just a configuration option in IntelliJ IDEA at the Gradle settings.
See https://www.jetbrains.com/help/idea/gradle-settings.html#gradle_tests for details.

Gradle test Unit tests - Run all except one or few - Command line

I have bunch of JUnit unit tests in my projects. Build system is Gradle. OS: Windows/Linux.
Test (Unit tests) come free in Gradle i.e. if you run "gradle clean build" Gradle will also run "test" task (to run your Unit tests). I know how can I run all tests in a test folder, of a specific test at command line. Ex: See 23.13.3 and 23.13.4 sections in Gradle user guide: http://www.gradle.org/docs/current/userguide/java_plugin.html#sec:java_test
My question:
I want to run all tests except one or some. How can I do this in Gradle at command line (rather than using exclude(s) within test { ... } section in build.gradle or higher level .gradle file).
You could conditionally add an exclusion based on a property value.
test {
if (project.hasProperty('excludeTests')) {
exclude project.property('excludeTests')
}
}
You could then do something like this from the command line.
$ gradle -PexcludeTests=org.foo.MyTest build
If you want to exclude more than one test per command line, here an enhanced version of Mark's answer (testet in gradle 6.8.2):
if (project.hasProperty('excludeTests')) {
project.properties['excludeTests']?.replaceAll('\\s', '')?.split('[,;]').each {
exclude "${it}"
}
}
On the command line you can now specify more than one test for exclusion:
$ gradlew test -PexcludeTests="**/bar*, **/foo.SomeTest*"
The separator may be ";" or ",", with or without following space.