Is testng.xml file updated automatically - maven

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>

Related

Maven install is running failsafe

I am attempting to have my integration tests separated from the usual lifecycle, meaning that I do not want them to be executed during a mvn install. I have the following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<goals>
<goal>integration-test</goal>
</goals>
</plugin>
And my integration tests are all suffixed with IT. I was able to confirm that surefire does exclude the *IT.java tests, however it seems that failsafe is being triggered during mvn install anyway. What am I missing?
Thank you for your help
A typical configuration to handle the issue with running integration tests only if you like is a profile like the following:
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
By using the above configuration you can run
mvn clean verify
which will execute the unit tests. Using the following you can activate the integration tests:
mvn -Prun-its clean verify
Based on the default naming conventions the integration tests which will be executed by maven-failsafe-plugin should be named like *IT.java where as the unit tests can be name like *Test.java (will be executed via maven-surefire-plugin).

Can not append system properties from profile config

I have build configuration for maven failsafe plugin which includes systemPropertyVariables:
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Also I have a profile which can append few properties to same plugin via pluginManagement:
<profile>
<id>test</id>
<activation>
<property>
<name>test.host</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<TEST_HOST>test.host</TEST_HOST>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
But I can't read properties from this profile. It's working fine if I delete systemPropertyVariables in build plugin configuration section and keep in pluginManagement configuration of profile. It seems that I need to merge these properties when used together, so I tried to add combine.children="append" and combine.self="append" to configuration of plugin, but it didn't help.
UPDATE:
I've found the cause of this issue: I didn't mention that I have parent pom including from this one, and parent pom.xml has these lines:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.0-M3</version>
<configuration combine.children="append">
<systemProperties>
<!-- some properties that I don't use -->
</systemProperties>
</configuration>
</plugin>
And these parent pom failsafe configuration is breaking all configs in my pom.xml - if I remove parent pom reference, failsafe properties will be included correctly. I can't just remove parent pom reference and can't modify it (actually I can fork it and edit, but it's not an easy task), is it possible to override these properties, since I don't use them?
Attached minimal reproducible project: https://send.firefox.com/download/88dfb9f933c9567f/#71ij1jbuEuAYgdwIiZQZRg
I managed to do it by adding an extra pom in between the untouchable (or pariah) parent and current child, I call this new middle pom midboy =)
So midboy has only the following difference from pariah;
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.0-M3</version>
<inherited>false</inherited>
</plugin>
And this pom becomes the the new parent pom of the child, while it uses the original pariah pom as its parent.
child ---[inherits]---> midboy ---[inherits]---> pariah
With inherited:false I essentially cut-off the failsafe plugin being passed into the child and breaking its profile-based configuration addition to its own local failsafe plugin & config. & this works perfectly!
No skipping of the test this time! 😁
Running com.test.so51905256.PropITCase
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 s - in com.test.so51905256.PropITCase
p.s. don't forget to add some sort of <activation> for the test profile

How to configure testng testsuite in jenkins

I am using Maven project and I have different TestNG suite file. Now I ve to configure Jenkins and POM.xml file where I've to call my individual testsuite from Jenkins. Please help me out how to configure my project.
I've configured my pom.xml file as below
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<includes>
<include>**/*.*</include>
</includes>
<suiteXmlFiles>
<suiteXmlFile>${project.basedir}/src/test/java/com/cisco/citeis/suites/${x‌​mlfile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
In Jenkins I add command clean test -Dsuite= AppFlow.xml
In the POM file add
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<suiteFile></suiteFile>
</properties>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
and in jenkins add the following command
clean test -DsuiteFile=\AppFlow.xml

Is isolation of failsafe and surefire runs from each other using a skip approach possible?

The value of property skipITs when overridden on the command line appears to be ignored by maven if used in conjunction with -Dit.test=full.Classname. The specified failsafe test will not be run (case one).
As opposed to this specifiing skipITs without the it.test switch leads to running of existing failsafe tests (case two).
Background: Am trying to isolate surefire tests from failsafe tests runs by namely either running both types of them or one of each only. The maven calls are:
mvn -Pintegration -DskipTests=true -DskipITs=false -Dit.test=full.Classname verify
in the first case and:
mvn -Pintegration -DskipTests=true -DskipITs=false verify
in the second.
The relevant configuration (pom.xml, snippets only) being:
<properties>
<skipTests>false</skipTests>
<skipITs>false</skipITs>
</properties>
(those are defaults) and
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>custom<id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skipITs}</skipTests>
<skip>${skipITs}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Did you observe that too or have you eventually found a better working approach?
By default, the Surefire plugins runs during the test phase and usually you configure the Failsafe plugin to run during the integration-test and verify phase like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
To run the Surefire tests only, use
mvn clean test
To run both the Surefire and the Failsafe tests, use
mvn clean verify
You can completely skip any plugin by using the <skip> configuration option. If you configure the Surefire plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skip>${skip.surefire}</skip>
</configuration>
</plugin>
you can run only the Failsafe tests by calling
mvn clean verify -Dskip.surefire=true
If the integration test (it.test) has a non-default name I need to add an include pattern accordingly, like in (pom.xml snippet):
<profiles>
<profile>
<id>dbUnit</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>custom</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skipITs}</skipTests>
<skip>${skipITs}</skip>
<includes>
<include>**/*DbTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
But that appears valid only for the original approach. Also observe that the default inclusion patterns might need to be added too in case for your integration tests you followed different naming schemes.

Is it possible to get the list of active profiles in Maven?

In Maven, is there a way to get a list of the active profiles, say, as a property or as a text file?
More specifically, when I run:
mvn resources:resources -P MyProfile
I want to get the string MyProfile somewhere I can read it into my Java program.
Maven 3.2.1
Thanks
Edit
I attempted to configure the Maven Help plugin to run the active-profiles goal whenever the goal resources:resources is run by configuring an execution to participate in the process-resources phase as shown below. That did not work either ...:
<packaging>jar</packaging>
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You could try this maven plugin. The configuration below will create a text file that will contain the profiles that were active during the build.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
</plugin>

Resources