Run integration and unit tests using Gradle JVMTestSuite plugin - gradle

I am trying to convert a maven project to gradle, and am struggling to get integration tests running. I am trying to use the recommended JVMTestSuite plugin, but the integration tests do not run correctly.
Using the example on the documentation, the current setup I have is this:
testing {
suites {
test {
useJUnitJupiter()
}
integrationTest(JvmTestSuite) {
testType.set(TestSuiteType.INTEGRATION_TEST)
dependencies {
implementation project()
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
tasks.named('check') {
dependsOn(testing.suites.integrationTest)
}
When I run ./gradlew integrationTest, the build succeeds but no tests are being executed. When I run ./gradlew test, all the unit tests and integration tests run, but the integration tests fail as the Spring context is not set up correctly.
End result I would like is:
test command runs only unit tests
integrationTest sets up Spring context and executes tests
Each set of tests is filtered by name (eg. filtering "*IT.java" rather than using separate folder structures)
Is someone able to help with this? The documentation does not seem to lead to a working example. Thank you :)

Related

Gradle - Chaining test tasks with `--tests` filter

I have been at this for a few days now, let's say I have a build.gradle as follows:
ext.createRunIntegrationTestsTask = { String taskName , String buildPlatform ->
return project.tasks.create(taskName, Test.class) {
dependsOn buildPlatform
[...]
}
}
def Platforms = ["A", "B"]
for (platform in Platforms) {
println "Creating task: runIntegrationTestsPlatform$platform"
createRunIntegrationTestsTask("runIntegrationTestsPlatform$platform", "buildPlatform$platform")
}
task runIntegrationTest(type: Test) {
dependsOn 'runIntegrationTestPlatformA'
dependsOn 'runIntegrationTestPlatformB'
}
I want to be able to run the following command with the tests filter:
gradlew :runIntegrationTest --tests "com.somestuff.methodTest"
For some reason when the above command runs, it does not pass the tests filter to runIntegrationTestPlatformA and runIntegrationTestPlatformB task. Both the runIntegrationTestPlatform... tasks run the whole suite of integration tests.
The current behavior I get is for runIntegrationTestPlatform... tasks to run the whole integration test suite for the specified platform and runIntegrationTest to run the integration test suite for all platforms.
But, when I pass the --tests filter to runIntegrationTest task, it does not get propagated to the chained tasks, i.e. runIntegrationTestPlatformA and runIntegrationTestPlatformB. Instead, it runs the whole integration test suite for both platforms. I want runIntegrationTestPlatformA and runIntegrationTestPlatformB to use the tests filter passed to runIntegrationTest to only run the specified integration tests.
Is there a way to get runIntegrationTestPlatformA and runIntegrationTestPlatformB to run from runIntegrationTest with the --tests filter.
Any help would be appreciated. Thank you.
Additional Sources:
Refactor duplicated code in gradle task "type: Copy"

Run gradle test from another gradle task

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"
}
}

Using the JUnitFlakyTestDataPublisher throws an AbstractMethodError "you must override contributeTestData"

I try to use the rerunFailingTestsCount option to deal with flaky tests. In order to display these in the junit results, I use the flaky test handler plugin which would theoretically deal with displaying flaky tests
In my jenkins file this looks like
pipeline {
stages {
stage('tests') {
steps{
// sh mvn verify here
}
post {
always {
junit testResults: 'target/failsafe-reports/**/*.xml', testDataPublishers: [[$class:
'JUnitFlakyTestDataPublisher']]
}
}
}
}
}
the test run fine, flaky ones are repeated but when it comes to publishing the junit results, i get an
Error when executing always post condition: java.lang.AbstractMethodError: you must override contributeTestData
Google wasn't very helpful, maybe someone here had the same problem and can help me or at least can confirm that this plugin works as pipeline script (there is a pull request regarding pipeline support, so I am not sure...)

How to make gradle not to mark build failed if no tests are found

As the title says, how can I make gradle not to fail a test task if no tests are found? I ran into this problem when I was using the --tests command line option with a multi-subproject project. For instance, this command below will run all tests in class FooTest from subproject A:
gradle test --tests com.foo.bar.FooTest
However, this command fails because of something like this:
Execution failed for task ':B:test'.
> No tests found for given includes: [com.foo.bar.FooTest]
BTW, I know something like below will succeed. But is it possible to make it succeed even with the test task? It's kind of annoying to type a test task name longer than test.
gradle :A:test --tests com.foo.bar.FooTest
The behavior you described is the current Gradle behavior, there is already a ticket on Gradle forum, see https://discuss.gradle.org/t/multi-module-build-fails-with-tests-filter/25835
Based on the solution described in this ticket, you can do something like that to disable the 'failIfNoTest' default behavior:
In your root project build (or better: in an InitScript in your Gradle USER_HOME dir, to make this behavior available for all your local projects)
gradle.projectsEvaluated {
subprojects {
// TODO: filter projects that does not have test task...
test {
filter {
setFailOnNoMatchingTests(false)
}
}
}
}
Then you can execute the following command without having errors if the given test doesn't exist in all sub-projects:
gradle test --tests com.foo.bar.FooTest
it seems that currently only a workaround like this is possible:
test {
afterSuite { desc, result ->
if (!desc.parent) {
if (result.testCount == 0) {
throw new IllegalStateException("No tests were found. Failing the build")
}
}
}
}
I have filed an issue with Gradle to introduce this as a simple config option: https://github.com/gradle/gradle/issues/7452
You can also run the tests only for the current project with
gradle :test --tests com.foo.bar.FooTest
Note the colon before the test task.

Gradle test parameter test suite

I'm new with Gradle.
My problem:
Is it possible to switch between test suites in "Gradle test" depending on parameter?
Something like:
test {
useTestNG()
{
suites 'src/test/resources/testng-'+input_parameter_as_string+'-Test.xml'
useDefaultListeners = true
}
My goal is to call: gradle test "input_parameter_as_string".
Hope you guys can help me out.
Gradle documentation lists some ways to run specific test using system property: http://www.gradle.org/docs/current/userguide/userguide_single.html#sec:java_test . To run multiple correlated tests, you can try test group (both TestNG and Gradle supports): http://testng.org/doc/documentation-main.html#test-groups .
If you insist to use your custom closure, you can always use project property. In build.gradle:
test {
useTestNG()
{
suites 'src/test/resources/testng-' + project.ext.input_parameter_as_string +'-Test.xml'
useDefaultListeners = true
}
and in command line:
gradle test -Pinput_parameter_as_string=testFoobar.
Out of the box, Gradle supports running a single test:
./gradlew test -Dtest.single=MyTestClassName

Resources