JUnit 5 - 使用 JUnit Jupiter 引擎时 IntelliJ IDEA 中的空测试套件
JUnit 5 - Empty test suite in IntelliJ IDEA when using JUnit Jupiter engine
如何在 IntelliJ IDEA v2016.2.2 中使用 JUnit 5 执行 All Suite 测试?
我得到空测试套件 运行宁此代码:
import org.junit.platform.runner.IncludeEngines;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.runner.SelectPackages;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@IncludeEngines("junit-jupiter")
@SelectPackages("<eu...package>") //I confirm that <eu...package> is ok.
public class AllTests {
}
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
Empty test suite.
[root]
JUnit Jupiter
JUnit Vintage
或
import eu.....services.ServiceTest;
import eu.....repository.DAOTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ServiceTest.class,
DAOTest.class
})
public class AllTests {
}
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
[root]
|+--JUnit Vintage
| +--eu.....AllTests
|+--JUnit Jupiter
我能够 运行 使用 JUnit 4 套件,但它不适用于 JUnit 5。
Just make sure that the junit-vintage-engine artifact is in your test runtime path. In that case JUnit 3 and JUnit 4 tests will automatically be picked up by the JUnit Platform launcher
简答
如果您使用的是 IntelliJ IDEA 2016.2,目前无法在 IDE.[=30 中执行带有 @RunWith(JUnitPlatform.class)
注释的测试 class =]
长答案
根据您报告的行为,经过一番艰苦的调查工作,我相信我已经找到了您问题的答案...
如果您使用的是 IntelliJ IDEA 2016.2,它内置了对 JUnit 5 的支持,那么会发生以下情况。
- IDEA 通过
Launcher
API 启动 JUnit 平台,select测试 class 用 @RunWith(JUnitPlatform.class)
注释(让我们称之为 TestSuite
).
Launcher
检测 junit-jupiter
和 junit-vintage
TestEngine
实现。
- JUnit Jupiter 引擎忽略
TestSuite
,因为它在技术上不是 JUnit Jupiter 测试 class。
- JUnit Vintage 引擎也会忽略
TestSuite
,因为它被注释为 @RunWith(JUnitPlatform.class)
。
- 最终结果是,没有注册的测试引擎声称它可以 运行
TestSuite
class。
不直观的部分是 JUnit Vintage 引擎忽略 TestSuite
,而实际上它看起来像基于 JUnit 4 的测试 class,因为它用 @RunWith()
注释。忽略它的原因是为了避免无限递归,在DefensiveAllDefaultPossibilitiesBuilder:
的源代码中有解释
if ("org.junit.platform.runner.JUnitPlatform".equals(runnerClass.getName())) {
return null;
}
上述代码 returns null
在这种情况下会导致 空套件 。
当然,如果用户知道这种情况当然会更好——例如,通过日志语句。因此,我已经为 JUnit 5 and IntelliJ 提出了问题,以提高在这种情况下的可用性。
从好的方面来说,由于您使用的是 IntelliJ IDEA 2016.2,因此您不需要使用测试套件支持。相反,您只需在 IDEA 和 select Run 'All Tests'
的项目视图中右键单击 src/test/java
,这将 运行 您的所有测试。
此致,
Sam (JUnit 5 核心提交者)
的补充。我需要一个 TestSuit 来设置后端 运行 所有测试包中的 classes。这可能无法通过 Run 'All Tests'
.
但我想我找到了一个很好的解决方法。我想到了这个:
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import your.other.package.test.classes.*;
public class TestSuit {
@BeforeAll
public static void setup(){}
@Nested
public class TestExtender extends MyTestClass {}
}
您可以从另一个包扩展每个测试 class 并向其添加 @Nested
注释。这不是最佳解决方案,但它是一种解决方法,直到 IDEA 或 JUnit 5 为这种情况找到另一种解决方案。
如何在 IntelliJ IDEA v2016.2.2 中使用 JUnit 5 执行 All Suite 测试?
我得到空测试套件 运行宁此代码:
import org.junit.platform.runner.IncludeEngines;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.runner.SelectPackages;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@IncludeEngines("junit-jupiter")
@SelectPackages("<eu...package>") //I confirm that <eu...package> is ok.
public class AllTests {
}
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
Empty test suite.
[root]
JUnit Jupiter
JUnit Vintage
或
import eu.....services.ServiceTest;
import eu.....repository.DAOTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ServiceTest.class,
DAOTest.class
})
public class AllTests {
}
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
[root]
|+--JUnit Vintage
| +--eu.....AllTests
|+--JUnit Jupiter
我能够 运行 使用 JUnit 4 套件,但它不适用于 JUnit 5。
Just make sure that the junit-vintage-engine artifact is in your test runtime path. In that case JUnit 3 and JUnit 4 tests will automatically be picked up by the JUnit Platform launcher
简答
如果您使用的是 IntelliJ IDEA 2016.2,目前无法在 IDE.[=30 中执行带有 @RunWith(JUnitPlatform.class)
注释的测试 class =]
长答案
根据您报告的行为,经过一番艰苦的调查工作,我相信我已经找到了您问题的答案...
如果您使用的是 IntelliJ IDEA 2016.2,它内置了对 JUnit 5 的支持,那么会发生以下情况。
- IDEA 通过
Launcher
API 启动 JUnit 平台,select测试 class 用@RunWith(JUnitPlatform.class)
注释(让我们称之为TestSuite
). Launcher
检测junit-jupiter
和junit-vintage
TestEngine
实现。- JUnit Jupiter 引擎忽略
TestSuite
,因为它在技术上不是 JUnit Jupiter 测试 class。 - JUnit Vintage 引擎也会忽略
TestSuite
,因为它被注释为@RunWith(JUnitPlatform.class)
。 - 最终结果是,没有注册的测试引擎声称它可以 运行
TestSuite
class。
不直观的部分是 JUnit Vintage 引擎忽略 TestSuite
,而实际上它看起来像基于 JUnit 4 的测试 class,因为它用 @RunWith()
注释。忽略它的原因是为了避免无限递归,在DefensiveAllDefaultPossibilitiesBuilder:
if ("org.junit.platform.runner.JUnitPlatform".equals(runnerClass.getName())) {
return null;
}
上述代码 returns null
在这种情况下会导致 空套件 。
当然,如果用户知道这种情况当然会更好——例如,通过日志语句。因此,我已经为 JUnit 5 and IntelliJ 提出了问题,以提高在这种情况下的可用性。
从好的方面来说,由于您使用的是 IntelliJ IDEA 2016.2,因此您不需要使用测试套件支持。相反,您只需在 IDEA 和 select Run 'All Tests'
的项目视图中右键单击 src/test/java
,这将 运行 您的所有测试。
此致,
Sam (JUnit 5 核心提交者)
Run 'All Tests'
.
但我想我找到了一个很好的解决方法。我想到了这个:
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import your.other.package.test.classes.*;
public class TestSuit {
@BeforeAll
public static void setup(){}
@Nested
public class TestExtender extends MyTestClass {}
}
您可以从另一个包扩展每个测试 class 并向其添加 @Nested
注释。这不是最佳解决方案,但它是一种解决方法,直到 IDEA 或 JUnit 5 为这种情况找到另一种解决方案。