How to run specific profile ID / suites (to run specific TestNG.xml) in Gradle? - 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.

Related

Why "mvn site" is running tests?

I have the following build configuration for a Spring Boot application.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
When I run mvn site, I expect the site goal of the site phase to execute. But my tests are running, which means the test goal of the default lifecycle is also getting executed.
I know I can skip running the test with -DskipTets, but I want to understand why this is happening?

Maven overrides configuration in profiles plugin

I have a 3 maven profiles with plugins:
<profile>
<id>first</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>1</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>second</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>2</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>third</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>3</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
When i start my build with mvn clean install -P first,second,third -X, I discovered that all this plugins was executed with configuration from from third profile. Is there any way to preserve my configuration for each of my plugins and not to be overriden by third configuration?
As discussed in comments section, You would have to invoke 3 build activating each profile differently
for example
mvn clean install -Pfirst
mvn clean install -Psecond
mvn clean install -Pthird
and to disable compilation in second and third, you could configure maven-compiler-plugin for these profiles and use skipMain property to disable main's source compilation, also for tests

How to set default parameter in maven project?

To disable cache in my test phase, I need to enter command :
mvn test -Dnet.sf.ehcache.disabled=true
Is there any way to set this property "net.sf.ehcache.disabled" as default? (Which menas: whenever I specify "mvn test" I want to set this parameter "net.sf.ehcache.disabled" as true automatically)
You could try to configure the surefire plugin in your pom.xml like that:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<net.sf.ehcache.disabled>true</net.sf.ehcache.disabled>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
...

Is testng.xml file updated automatically

When running testng with mvn, I configured my workspace as required:
My pom.xml file is configured with all the required dependencies and testng.xml contains all the required classes.
Yet, when I add a new test class, the testng.xml isn't updated automatically-
Shouldn't it scan from the root for the corresponding tests? Or will I have to update the testng.xml file manually?
(BTW, my workspace is configured according to the following post: How to call testng.xml file from pom.xml in Maven)
The xml method gives you more granular control of your test sets. It will not be altered automatically, although eclipse will generate xml configs on the fly if you select say a folder and right click run as testng. If you would just like to run all testng annotated test and you have maven configured properly with surefire or failsafe, you don't even need an xml file. Just run "mvn verify" and all test should be run based on annotation. If this doesn't work, please post your surefire/failsafe pom sections.
If you want to configure a particular xml in maven, use something like (surefire or failsafe work the same.):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<configuration>
<argLine>-Xmx1024m</argLine>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</execution>
</executions>
</plugin>
If you do need more granular control, and would like to use an xml file specified by maven, you launch it via "verify -P MyProfile"
<profiles>
<profile>
<id>MyProfile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>MyProfile.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>MyOtherProfile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>MyOtherProfile.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>

Exclude maven dependency for tests

I have a dependency that is needed for a compilation and runtime but I want to exclude it when running tests. Is this possible? Maybe, by setting up a profile? But how do I deactivate it only for test lifecycle phase?
You could (re)configure the classpath during the test phase thanks to the maven surefire plugin. You can add classpath elements or exclude dependencies.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
<additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
</additionalClasspathElements>
<classpathDependencyExcludes>
<classpathDependencyExclude>org.apache.commons:commons-email</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
As noted by #jFrenetic you could do the same with maven-failsafe-plugin.

Resources