JUnit 5 Disabled是否被忽略?

时间:2018-10-02 05:10:11

标签: java intellij-idea junit5 junit-jupiter

我正在将JUnit 5与IntelliJ IDEA Community Edition 2018版一起使用。

我的代码很简单:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {

    @Disabled
    @Test
    void addTwoZeroNumerators(){
    int[] resultExpected = {0,0};
    assertArrayEquals(resultExpected, Calculator.calculate(0,0,0,1));
}

我使用@Disabled。但是当我运行测试时,事件日志仍然报告1个测试通过。有人可以告诉我这是怎么了吗?我希望系统忽略此测试。

以下是日志部分的标题:

enter image description here

3 个答案:

答案 0 :(得分:1)

@Nicolai的回答是100%正确,如果您强制IntelliJ执行测试,则IntelliJ将执行测试。

但是,如果要在构建管理系统中启用@Disabled注释,请记住关于surefire插件(details)。如果丢失,注释将不起作用。

答案 1 :(得分:0)

我认为这可能是Maven SureFire插件的一个错误,如果您有一个只有单个@Test的类,并且它也是@Disabled,它仍然会尝试运行它。我试过了maven-surefire-plugin:2.22.0、2.22.2和3.0.0-M3,似乎都出现了问题。

Apache Maven团队入场券:

https://issues.apache.org/jira/browse/SUREFIRE-1700?filter=-2

使用JetBrains打开的机票:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006399720-IDEA-2019-2-2-Ultimate-Edition-ignores-Disabled-for-JUnit5-tests-having-a-single-Test-method

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006399760-IDEA-2019-2-2-Ultimate-Edition-requires-junit-vintage-engine-for-Maven-package-using-only-JUnit5

答案 2 :(得分:0)

@Tony Falabella

在吉拉(Jira)中,我提示了您。请使用最新的快照版本 3.0.0-SNAPSHOT ,并让我知道此问题。可以在Apache Nexus存储库中找到此快照版本。请在插件开发期间尽快向我们提供您的反馈,并尝试使用快照版本,因为这样可以避免出现新问题:

<pluginRepository>
  <id>surefire-snapshot</id>
  <name>surefire-snapshot</name>
  <url>https://repository.apache.org/content/repositories/snapshots/</url>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
 </pluginRepository>
...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-SNAPSHOT</version>
    </plugin>
  </plugins>
</build>

在吉拉,我发现您使用

<artifactId>junit-platform-launcher</artifactId>

请不要使用它。与您无关。它仅与Surefire / Failsafe和IDE的使用有关。