4

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 ?

matt b
  • 138,234
  • 66
  • 282
  • 345
ravinikam
  • 3,666
  • 6
  • 28
  • 25
  • possible duplicate of [How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?](http://stackoverflow.com/questions/1232853/how-to-execute-junit-and-testng-tests-in-same-project-using-maven-surefire-plugi) – MariuszS May 20 '14 at 20:19

3 Answers3

4

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>
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • Thanks much Rich, I really appreciate your help. You saved my hours of time man!! I was looking around this since morning. Even tried creating different profiles for both and all.I wasn't aware of the fact that maven uses the default configuration and we need to override this. Thanks lot. – ravinikam Aug 06 '09 at 13:21
  • You're welcome. It's worth noting that in general you can run **mvn help:effective-pom** (and add any profiles with -P as normal) you can see the resultant POM, that would have shown the merged compiler configuration. – Rich Seller Aug 06 '09 at 14:47
0

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

MariuszS
  • 30,646
  • 12
  • 114
  • 155
0

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>
Jim Tough
  • 14,843
  • 23
  • 75
  • 96