Cucumber Test execution for gradle project via command line using tags for cucumber feature files - gradle

I am looking to execute a cucumber test for a gradle project via command line using specific feature tags.
The command I am using: gradle test -DCucumber.options="-tags #tagname".
Command does execute the tags mentioned.
I have tried using gradle test -DCucumber.options="-tags #tagname" and also gradle test. I didn't find any difference in both the command.
gradle test -DCucumber.options="-tags #tagname" executes the Runtest.java and tags mentioned in this file, irrespective of what feature tags I pass via command line for example: tagname.
Runtest.java
#RunWith(Cucumber.class)
#CucumberOptions(features = "src\\test\\resources\\featurefiles", monochrome = true, plugin = {
"com.eis.listeners.ExtentCucumberFormatter:" }, glue = {
"com.adminconsole.stepdefs" },tags= {"#adminconsolelogin,#devicemanager,#certificatemanagement"} ,format = { "json:JsonReports/AdminConsole.json" })
So here I have mentioned three tags in the Runtest.java.
Now, instead of running all the tags, I wanna run a specific tag via command line.
Command: gradle test -DCucumber.options="-tags #adminconsolelogin", but -DCucumber.options="-tags #adminconsolelogin" part ain't working.
I am looking for a solution where we can run specific tags irrespective of what tag is mentioned in Runtest.java. More precisely pass tags dynamically via command line.
But -DCucumber.options="-tags #tagname" ain't working via command line.
Would appreciate if anyone can provide me with a correct command or strategy or code on how to do it. If the below command is wrong: gradle test -DCucumber.options="-tags #tagname" please correct me.

Update: For Cucumber 6 you need to provide the following statement:
test {
systemProperty "cucumber.filter.tags", System.getProperty("cucumber.filter.tags")
}
You have to bridge the system properties between the gradle JVM and the forked JVM for the tests for this to work. From issue #1346:
test {
systemProperty "cucumber.options", System.getProperty("cucumber.options")
}
Add that to your build.gradle and then you can do it on the command-line:
gradle test -Dcucumber.options="-tags #tagname"

You need to add this
test {
systemProperty "cucumber.options", System.getProperty("cucumber.options")
}
And CLI command will be
gradle test -Dcucumber.options="--tags #tagName"

Related

Gradle how to exclude 1 test file in a multi module project setup?

So we have a multi module project setup, with test all scattered in multiple modules, now we want to execute all of them but exclude 1 test file.
How could we achieve this?
I tried the following:
gradle test -PexcludeTests=*SpecificTests
but the tests get still executed.
for running a singular test I managed to fix it this way:
gradle :multi-module:test --tests '*SpecificTests'
but unfortunately the equivalent for executing all tests but 1 cannot be made with this.
Condition: we need a command we cannot use the testing filter
You can use a project property and define an optional exclude filter, which you can then use from the command line -PexcludeTests='*SpecificTests'.
test {
if (project.hasProperty('excludeTests')) {
exclude(project.property('excludeTests'))
}
}

Gradle: get task command line arguments inside the task

I'm running my tests with gradle using the command line:
./gradlew testManager:uiTest -PignoreTestFailures=true" +
"-DCHROMEDRIVER_VERSION=${env.CHROMEDRIVER_VERSION}" +
"-DBASE_URL=${params.BASE_URL}"
I need to propagate passed properties (e.g. BASE_URL) to the JMV with tests from the gradle task.
I know I could do the following inside the task:
systemProperties System.properties
But I'd like to avoid passing the whole set, as it overrides some other required values in tests.
So the question is: is there a way to get the only properties passed via -D command line parameter, inside the gradle task?
Found this pretty easy way:
task uiTest(type: Test) {
doFirst {
/* Propagate only command line start properties (-D) to the tests */
project.gradle.startParameter.systemPropertiesArgs.entrySet().collect() {
systemProperty it.key, it.value
}
}
}
So in a case of
./gradlew testManager:uiTest -PincludeTests=saveReleaseState/** -DBASE_URL=foobar
The tests receive only BASE_URL=foobar

How to pass system properties to the tests in gradle in the smart way?

build.gradle
tasks.withType(Test){
systemProperties=System.properties
println systemProperties['param']
}
Now I can either pass parameters in the command line:
gradle test -Dparam=10
or put them in gradle.properties:
systemProp.param=15
Ideally I would like to put the defaults in the gradle.properties, and be able to overwrite them from the command line. Unfortunately if I do that, the gradle.properties has precedence, and -Dparam=10 is ignored.
Could you offer any solutions on that?
https://issues.gradle.org/browse/GRADLE-2122
It works since 2.12 or 2.13 "the smart way" already!
The example above is working, the command line -D option overdrives the defaults in gradle.properties
I am using gradle 2.12 and sharing how I used it:
test {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:
//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Doing this with gradle required that the system property provided at the time of running gradle build, shown here,
gradle build -Ddeep.test.run=true
was indeed passed through to the tests.
Hope this helps others trying out this approach for running tests conditionally.

I want to run many SOAPUI project xmls using Gradle script, in Linux

I want to run the SOAPUI project xmls using Gradle script. The GRADLE script should read the project xmls from soapuiInputs.properties file and run automatically all. Please guide me step by step how to create Gradle script to run the SOAPUI projects in Linux server.
Note: We use SOAPUI version 5.1.2.
Probably the simple way is to call the SOAPUI testrunner directly from gradle as Exec task, like you can do from cli.
In gradle you can define the follow tasks (Note that I try it on windows but to do the same on linux as you ask simply you've to change the paths):
// define exec path
class SoapUITask extends Exec {
String soapUIExecutable = 'C:/some_path/SoapUI-5.2.1/bin/testrunner.bat'
String soapUIArgs = ''
public SoapUITask(){
super()
this.setExecutable(soapUIExecutable)
}
public void setSoapUIArgs(String soapUIArgs) {
this.args = "$soapUIArgs".trim().split(" ") as List
}
}
// execute SOAPUI
task executeSOAPUI(type: SoapUITask){
// simply pass the project path as argument,
// note that the extra " are needed
soapUIArgs = '"C:/location/of/project.xml"'
}
To run this task use gradle executeSOAPUI.
This task simply runs a SOAPUI project, however testrunner supports more parameters which you can pass to soapUIArgs string in executeSOAPUI task, take a look here.
Instead of this if you want to deal with more complex testing there is a gradle plugin to launch SOAPUI project, take a look on it here
Hope this helps,

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.

Resources