【问题标题】:Can we execute JUnit 5 and TestNG tests in the same project using maven-surefire-plugin?我们可以使用 maven-surefire-plugin 在同一个项目中执行 JUnit 5 和 TestNG 测试吗?
【发布时间】:2020-01-22 17:32:38
【问题描述】:

我正在为一个已经有一些 TestNG 测试的项目编写单元测试。由于我使用 JUnit Jupiter(又名 JUnit 5)来编写新测试,我观察到当我执行“mvn clean verify”时仅运行 TestNG 测试的问题

以下是我在pom.xml 中的依赖项

       <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>

由于 2.22.0 以后的surefire插件提供了JUnit 5支持,下面是我的surefire插件部分

           <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
           </plugin> 

有什么方法可以让我同时保留在项目中并能够通过 maven 运行所有测试?这是必要的,以便代码覆盖率报告将所有测试一起显示。

我还注意到,如果我在老式引擎依赖的帮助下使用 JUNIT 4 测试,所有测试(JUnit + TestNG)都会通过 maven 一起运行。我很困惑为什么 JUnit 5 不会发生这种情况。

【问题讨论】:

标签: unit-testing junit testng junit5 maven-surefire-plugin


【解决方案1】:

以“预览功能”样式支持。

  • (1) 在您的测试运行时依赖项中包含TestNGine
  • (2) 帮助(强制)Surefire 使用 JUnit 平台提供程序

查看这个简单的集成测试pom.xml 了解可能的设置:


  <dependencies>

    <dependency> <!-- (1) -->
      <groupId>com.github.testng-team</groupId>
      <artifactId>testng-junit5</artifactId>
      <version>0.0.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.6.0</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <dependencies>
          <dependency> <!-- (2) -->
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit-platform</artifactId>
            <version>2.22.2</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

【讨论】:

    猜你喜欢
    • 2010-11-16
    • 2017-05-06
    • 2019-12-26
    • 2022-01-14
    • 2013-07-22
    • 2020-11-17
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多