How to run specific profile ID / TestNG suites (to run specific TestNG.xml) in Gradle?

I am new to Gradle, but I know in Maven we can run specific profile.
In my case, I have 2 TestNG.xml files and in Maven POM.xml I can write like this

<profiles>
		<profile>
			<id>FirstRegression</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-surefire-plugin</artifactId>
						<version>2.18.1</version>
						<configuration>
							<suiteXmlFiles>
								<suiteXmlFile>testng1.xml</suiteXmlFile>
							</suiteXmlFiles>
						</configuration>
					</plugin>
				</plugins>
			</build>
		</profile>
		<profile>
			<id>SecondRegression</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-surefire-plugin</artifactId>
						<version>2.18.1</version>
						<configuration>
							<suiteXmlFiles>
								<suiteXmlFile>testng2.xml</suiteXmlFile>
							</suiteXmlFiles>
						</configuration>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

In command line , we can choose which profile (which TestNG.xml file) we want to run.
mvn test -PFirstRegression will execute our testng1.xml file only, meanwhile
mvn test -pSecondRegresion will execute our testng2.xml file only.

How can we do this in build.gradle file ? so we can choose which profile to run in gradle.

I can put like this in build.gradle

plugins {
	 id 'java'
 }

test {
	 useTestNG() {
         suites 'testng1.xml'
		 suites 'testng2.xml'
	 }
 }

But when I run gradle clean build , it will run both of them.
Is there anyway we can say gradle clean build --"please run testng2.xml only" ? Thank You.

Since Gradle build scripts are code, you can easily add any logic around properties that you want. For example, something like:

test {
    useTestNG() {
        if (project.hasProperty('FirstRegression')) { suites 'testng1.xml' }
        if (project.hasProperty('SecondRegression')) { suites 'testng2.xml' }
    }
}
1 Like

Thanks James for the answer.

Just wondering, how can I call it in the command prompt ?
gradle clean build -FirstRegression to execute testng1.xml ?

Secondly, project.hasProperty , where does the project come from? Does project point to the whole build process?
A hint may be useful for me to further study this topic.
Thanks.

The command line options would match what you showed for your maven build. Those project properties can be specified on the command line with -PFirstRegression or -PSecondRegression.

The Project is a very specific API in Gradle. You may have one or more depending on your build. The build script normally delegates any method calls or property access to a Project instance if not defined in your build script. In this case, I called out project because the test task has a hasProperty() method as well. See User Guide: Writing Build Scripts (Project API) for more.

1 Like

Hi Kendo,

I am new to Gradle.I have TestNg.xml file and i am trying to execute the testng suite through Gradle but I can’t.
I tried gradle clean build but test not executing.

Please help me on this.

Please find my Build.Gradle file

/*

  • This build file was generated by the Gradle ‘init’ task.
  • This generated file contains a sample Java Library project to get you started.
  • For more details take a look at the Java Libraries chapter in the Gradle
  • user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
    */
    apply plugin: 'java’
    apply plugin: ‘maven’

sourceCompatibility = 1.5
targetCompatibility = 1.5
tasks.withType(JavaCompile) {
options.encoding = ‘UTF-8’
}

repositories {

 maven { url "http://repo.maven.apache.org/maven2" }

}

dependencies {
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version:'2.52.0’
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-server’, version:'2.52.0’
compile group: ‘info.cukes’, name: ‘cucumber-java’, version:'1.2.2’
compile group: ‘info.cukes’, name: ‘cucumber-testng’, version:'1.2.4’
compile group: ‘info.cukes’, name: ‘cucumber-core’, version:'1.2.4’
compile group: ‘org.hamcrest’, name: ‘hamcrest-all’, version:'1.3’
compile group: ‘info.cukes’, name: ‘gherkin’, version:'2.12.2’
compile group: ‘com.google.guava’, name: ‘guava’, version:'18.0’
testCompile group: ‘org.testng’, name: ‘testng’, version:‘6.8’
}

test {
useTestNG() {
suites ‘./src/test/java/AllTest.xml’
}
}

Hi jjustinicJames Justinic

I am new to Gradle.I have TestNg.xml file and i am trying to execute the testng suite through Gradle but I can’t.
I tried gradle clean build but test not executing.

Please help me on this.

Please find my Build.Gradle file

/*

  • This build file was generated by the Gradle ‘init’ task.
  • This generated file contains a sample Java Library project to get you started.
  • For more details take a look at the Java Libraries chapter in the Gradle
  • user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
    */
    apply plugin: 'java’
    apply plugin: ‘maven’

sourceCompatibility = 1.5
targetCompatibility = 1.5
tasks.withType(JavaCompile) {
options.encoding = ‘UTF-8’
}

repositories {

maven { url “http://repo.maven.apache.org/maven2” }

}

dependencies {
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version:'2.52.0’
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-server’, version:'2.52.0’
compile group: ‘info.cukes’, name: ‘cucumber-java’, version:'1.2.2’
compile group: ‘info.cukes’, name: ‘cucumber-testng’, version:'1.2.4’
compile group: ‘info.cukes’, name: ‘cucumber-core’, version:'1.2.4’
compile group: ‘org.hamcrest’, name: ‘hamcrest-all’, version:'1.3’
compile group: ‘info.cukes’, name: ‘gherkin’, version:'2.12.2’
compile group: ‘com.google.guava’, name: ‘guava’, version:'18.0’
testCompile group: ‘org.testng’, name: ‘testng’, version:‘6.8’
}

test {
useTestNG() {
suites ‘./src/test/java/AllTest.xml’
}
}
[/quote]

There isn’t enough information here to help you and this is likely not a Gradle problem. Your build.gradle file works correctly with a sample TestNG project.

Where does your testNG.xml files are located in your project?