简体   繁体   中英

Junit4 and TestNG in one project with Maven with inheritance

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.

There are three modules - junit , testng and mixed .

The default profile should execute only junit tests - ie run junit module and run junit tests of mixed module. testNG profile should execute only testNG tests - ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM