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
Related
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 :)
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"
I have integration tests named with "IT" at the end and the tests are excluded by default. My build.gradle contains:
test {
if (!project.hasProperty('runITs')) {
exclude '**/**IT.class'
}
}
The check task, which is part of build, no longer runs integration tests. All tests (unit + integration) are executed with defined runITs (e.g. ./gradlew -PrunITs=1 check) if it is necessary.
It works very well. The only drawback is than I can't run single integration test with --test (used by IDE) without runITs defined. The command fails with message: No tests found for given includes: [**/**IT.class](exclude rules).
Is there any way (e.g. build variable) how to recognize the run of single test with --test and skip the exclusion?
I have found a solution. My build.gradle now contains:
test {
if (!project.hasProperty('runITs') && filter.commandLineIncludePatterns.empty) {
exclude '**/**IT.class'
}
}
When tests are run with --tests then the list commandLineIncludePatterns in filter is not empty and exclude is not applied.
Details are obvious in test task source: https://github.com/gradle/gradle/blob/dc8545eb1caf7ea99b48604dcd7b4693e79b6254/subprojects/testing-base/src/main/java/org/gradle/api/tasks/testing/AbstractTestTask.java
try invoking the test task to the specific sub-project. if the test is root try
gradle -PrunITs=1 :test --tests 'MServiceTest'
else
gradle -PrunITs=1 :sub-prj:test --tests 'MServiceTest'
I'm new to gradle and learning as I go. I've tests that I can kick off using gradle command in my terminal. The issue I'm running into is that I have to keep two copies of the same test, one for QA and one for Staging environment. They are similar tests the reason for this is that I have no idea how to set up gradle in a way that I can just tell it to run testA on QA or Staging
My Current gradle.build file looks like this
dependencies {
testCompile 'org.testng:testng:6.14.3'
compile 'org.seleniumhq.selenium:selenium-java:3.141.59'
compile 'io.github.bonigarcia:webdrivermanager:3.3.0'
compile 'org.slf4j:slf4j-nop:1.7.25'
}
test {
useTestNG()
}
My current gradle command looks like this for QA
gradle test --tests testlandingpageQA
or for Staging
gradle test --tests testlandingpageDev
I need a solution where end user can pick which environment to run this test on.
I got this working in build.gradle file I had to define system property
test {
systemProperty 'env', System.properties['env'] ?: 'staging'
useTestNG()
}
In java I had to do
String environment = System.getProperty("env");
driver.get("https://" + environment + ".somewebsite.com");
Finally in the console/terminal I can run something like
gradle -Denv=Dev test --tests testlandingpage
Hopefully it helps others who are trying to figure this out
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.