1

I have the same problem as Junit4 and TestNG in one project with Maven but I still have issues. I created simple project where I need to use junit4 and testng Here is root pom:

    <groupId>com.dimas.testmodule</groupId>
            <artifactId>TheParent</artifactId>
            <version>0.0.001</version>
            <name>TheParent</name>
            <packaging>pom</packaging>

            <properties>
                <spring.version>3.0.5.RELEASE</spring.version>
                <spring.scope>test</spring.scope>
                <maven.surefire.plugin.version>2.9</maven.surefire.plugin.version>
            </properties>

            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.7</version> 
                       <scope>test</scope>
                    </dependency>
        ...etc
                </dependencies>

            <build>
                <finalName>${project.artifactId}</finalName>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

            <profiles>
                <profile>
                    <id>default</id>
                    <activation>
                        <activeByDefault>true</activeByDefault>
                    </activation>
                    <modules>
                        <module>JUnitOnly</module>
                        <module>TestNGOnly</module>
                        <module>testmodule</module>
                    </modules>
                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <version>${maven.surefire.plugin.version}</version>
                                <configuration>
                                    <redirectTestOutputToFile>false</redirectTestOutputToFile>
<excludes>
                                <exclude>**/*NGTest.java</exclude>
                            </excludes>
                                </configuration>
                            </plugin>
                        </plugins>
                    </build>
                </profile>

                <profile>
                    <id>testNGonly</id>
                    <modules>
                        <module>JUnitOnly</module>
                        <module>TestNGOnly</module>
                        <module>testmodule</module>
                    </modules>

                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <version>${maven.surefire.plugin.version}</version>
                                <configuration>
                                    <redirectTestOutputToFile>false</redirectTestOutputToFile>
                                    <includes>
                                        <include>**/*NGTest.java</include>
                                    </includes>
                                </configuration>
                            </plugin>
                        </plugins>
                    </build>
                </profile>
            </profiles>

modules JUnitOnly and TestModule just inherits and do not overrite any properties. And here are TestNgOnly

<groupId>com.dimas.testmodule.testngonly</groupId>
    <artifactId>TestNGOnly</artifactId>
    <version>0.0.001</version>
    <name>testngonly</name>
    <parent>
        <groupId>com.dimas.testmodule</groupId>
        <artifactId>TheParent</artifactId>
        <version>0.0.001</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>testNGtest</id>  
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven.surefire.plugin.version}</version>
                        <configuration>
                            <redirectTestOutputToFile>false</redirectTestOutputToFile>
                            <suiteXmlFiles>
                                <suiteXmlFile>
                                    <!--test/resources/my-testng.xml-->
                                    src\test\resources\my-testng.xml
                                </suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

Well I need to ignore testng tests for default profile and launch only testng tests for profile testNGOnly.

Community
  • 1
  • 1
Dmitrii Borovoi
  • 2,814
  • 9
  • 32
  • 50
  • What `issues` do you have? testng tests are running in the `testmodule` for the `default` profile? Or tests are running in `testNGOnly`? If latter, why include that module in the `default` profile? – Raghuram Jun 13 '12 at 05:14
  • testmodule - contains and junit and testng tests. TestNgOnly module contatins only testng tests. Junit module contains only junit tests. When I start tetsNGOnly profile only testng tests must be launched. When I start deafult profile only junit tests must be launched. – Dmitrii Borovoi Jun 13 '12 at 05:41
  • 1
    I created a skeletal multi-module project illustrating this use case [here](https://github.com/maruhgar/mvn-examples/tree/master/mixtestsmultimodule). See if this solves your problem. I can detail it as answer, if so. – Raghuram Jun 13 '12 at 08:56
  • thanx a lot. Looks like it what I need. I just add configuration> 1.6 1.6 to your code and all start to work! – Dmitrii Borovoi Jun 13 '12 at 10:25

1 Answers1

1

There are three modules - junit, testng and mixed.

The default profile should execute only junit tests - i.e. run junit module and run junit tests of mixed module. testNG profile should execute only testNG tests - i.e. run testNG module and run testNG tests of mixed module.

The way profiles work in maven, it is not possible to exclude a module in a profile. However, there is no need to build a module which is not required.

Thus, in default profile, have modules junit and mixed.

In testNG profile, add module testNG.

Following maven best practice, define <dependencies> in the parent pom within <dependencyManagement>. This allows us to only use junit dependency in junit module and testNG dependency in testNG module.

There is nothing common about surefire configuration across the three modules and as such not required to be specified in parent pom.

Since it not required to run junit tests in testNG profile, add a plugin configuration to skipTests in testNG profile for junit module.

Configure the mixed pom using the tip in this answer for the related SO question. This gets tricky since surefire needs to use junit or testng runner based on the profile.

I have incorporated the above suggestions in a sample project here.

Community
  • 1
  • 1
Raghuram
  • 51,854
  • 11
  • 110
  • 122