Junit4 和 TestNG 位于一个带有 Maven 的项目中
题
要一起运行它们,可用的选项很少,但我选择为 Junit 和 TestNG 使用不同的配置文件。但现在的问题是排除和包含测试用例。
因为如果我们将 testNG 依赖项添加到 Maven 中的主项目中,它将跳过所有 Junit,我决定将其放在单独的配置文件中。因此,我使用 pom.xml 中的以下条目将默认(主)配置文件中的 TestNG 测试排除在编译之外:
<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>
和 Surefire 插件一样。因此它可以与主配置文件配合使用,并且仅执行 Junit4 测试。但是当我使用 testNG 配置文件时,它不会执行 testNG 测试,即使它不会编译它们。我正在使用以下配置文件来执行它们。
<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>
有人知道为什么它不包括它们并再次编译吗?
解决方案
用于编译器插件的配置排除了TestNG的类型。从配置文件中的配置被合并使用默认配置,所以你的有效编译器配置是:
<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>
这意味着你的TestNG的类型未曾编译,因此无法运行。
如果您指定<排除>在TestNG的截面轮廓它将取代默认排除和你的TestNG的类型将被编译并运行。我不记得是否会使用空不包括标签工作(即<排除/>),您可能需要指定这样的东西,以保证默认配置覆盖。
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
其他提示
最简单的解决方案是这样的
<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>
我在网上没有找到任何适用于 Surefire 和 Failsafe 的组合解决方案。下面的 POM 文件更改对我有用。
- Surefire 技巧是通过显式指定 JUnit 和 TestNG 的子插件来完成的。
- 故障安全技巧是通过定义两个执行来完成的,每个执行都有一个空配置,一个用于 JUnit,另一个用于 TestNG。
这两种解决方案都是我在网上找到的。我认为 Surefire 技巧的链接在这个问题的另一个答案中。
<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>
不隶属于 StackOverflow