java - Junit4 和 TestNG 在与 Maven 的一个项目中

标签 java maven-2 junit4 testng

要一起运行它们,可用的选项很少,但我选择了对 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>

关于java - Junit4 和 TestNG 在与 Maven 的一个项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238017/

相关文章:

Java 正则表达式匹配不匹配或的单词集,其余工作正常

Maven 插件继承自 pluginManagement

maven-2 - Maven忽略提供的范围

java - 使用 SpringFramework 进行 Junit 测试,不使用 Maven : Error - NoSuchMethodError

java - 如何使用不同版本的java执行gradle插件

java - 未知错误 - bundle 字符串数组

java - 我如何在struts2 if标签中使用请求参数

maven - 如何从 Plexus 组件获取当前 MavenSession 或 MavenExecutionRequest

动态功能模块中的 androidTest espresso 测试用例无法构建/运行 - 任务 ':dfm:mergeLibDexDebugAndroidTest' 执行失败

java - 比较 java.awt.Color 对象时,JUnit Test 似乎无法正常工作