To run them together there are few options available but I have chosen using different profiles for Junit and TestNG. But now problem is with excluding and including test cases.
Since if we add testNG dependency to main project in maven it will skip all Junit,I have decided to put it in separate profile. So I am excluding TestNG tests in default(main) profile from compiling using following entry in pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
and same for surefire plugin. So it works fine with main profile and executes only Junit4 tests. But when I use testNG profile it wont execute testNG test even it wont compile them. I am using following profile to execute them.
<profile>
<id>testNG</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<includes>
<include>**/**.class</include>
<include>**/tests/utils/**.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</profile>
Anybody have any idea why it is not including them and compiling again ?
The configuration for the compiler plugin excludes the TestNG types. The configuration from the profile is merged with the default configuration, so your effective compiler configuration is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
This means that your TestNG types aren't ever compiled and therefore aren't run.
If you specify the <excludes> section in the testNG profile it will override the default excludes and your TestNG types will be compiled and run. I can't remember if it will work with an empty excludes tag (i.e. <excludes/>), you may have to specify something like this to ensure the default configuration is overridden.
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
Simplest solution is like this
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
More info about this: Mixing TestNG and JUnit tests in one Maven module – 2013 edition
I didn't find any combined solution online for both Surefire and Failsafe. The POM file changes below worked for me.
- The Surefire trick is done by explicitly specifying the child plugin for both JUnit and TestNG.
- The Failsafe trick is done by defining two executions, each with an empty configuration, one for JUnit and the other for TestNG.
Both solutions are hacks that I found online. I think the link to the Surefire trick is in another answer to this question.
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<executions>
<execution>
<id>test-testng</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-junit</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>