Можем ли мы выполнить тесты JUnit 5 и TestNG в одном проекте, используя maven-surefire-plugin? - PullRequest
0 голосов
/ 22 января 2020

Я пишу юнит-тесты для проекта, в котором уже есть несколько тестов TestNG. Поскольку я использую JUnit Jupiter (он же JUnit 5) для написания новых тестов, я обнаружил проблему, состоящую в том, что только TestNG тесты запускаются, когда я выполняю "mvn clean verify"

Ниже приведены мои зависимости в 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>

И так как плагин surefire начиная с 2.22.0 обеспечивает поддержку JUnit 5 ниже, это мой плагин для безошибочного

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

Есть ли способ сохранить оба в проекте и сможете ли запустить все тесты через maven? Это необходимо, чтобы в отчете о покрытии кода были показаны все тесты вместе.

Также я отметил, что все тесты (JUnit + TestNG) выполняются вместе через maven, если я использую тесты JUNIT 4 с помощью зависимости от винтажного движка. Я запутался, почему этого не происходит с JUnit 5.

1 Ответ

0 голосов
/ 25 января 2020

Он поддерживается в стиле «функции предварительного просмотра».

  • (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>
...