我在一个使用 Maven 的项目中遇到了与 Junit4 和 TestNG相同的问题,但我仍然有问题。我创建了需要使用junit4和testng的简单项目这是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>
模块 JUnitOnly 和 TestModule 只是继承而不覆盖任何属性。这里是 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>
好吧,我需要忽略默认配置文件的 testng 测试,并仅启动配置文件 testNGOnly 的 testng 测试。