要一起运行它们,可用的选项很少,但我选择了对 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 配置文件中指定
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
关于java - Junit4 和 TestNG 在与 Maven 的一个项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238017/