如何在IntelliJ IDEA v2016.2.2中使用JUnit 5执行所有Suite测试?
我得到运行此代码的Empty测试套件:
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。
简短答案
如果使用的是IntelliJ IDEA 2016.2,则当前无法@RunWith(JUnitPlatform.class)
在IDE中执行带注释的测试类。
长答案
根据您报告的行为,经过一些艰苦的调查工作,我相信我可以回答您的问题...
如果您使用的是IntelliJ IDEA 2016.2,它内置了对JUnit 5的支持,那么正在发生以下情况。
Launcher
API 启动JUnit Platform ,选择带有注释的测试类@RunWith(JUnitPlatform.class)
(我们称之为TestSuite
)。Launcher
检测junit-jupiter
和junit-vintage
TestEngine
实现。TestSuite
它,因为从技术上讲,它不是JUnit Jupiter测试类。TestSuite
带有注释,因此也将忽略@RunWith(JUnitPlatform.class)
。TestSuite
该类。不直观的部分是JUnit Vintage引擎会忽略TestSuite
,因为它实际上是基于JUnit 4的测试类,因为它带有注释@RunWith()
。被忽略的原因是为了避免无限递归,在DefensiveAllDefaultPossibilitiesBuilder的源代码中对此进行了解释:
if ("org.junit.platform.runner.JUnitPlatform".equals(runnerClass.getName())) {
return null;
}
上面的代码null
在这种情况下返回的事实导致一个空套件。
当然,如果将这样的情况通知用户(例如通过log语句)肯定会更好。因此,我为JUnit 5和IntelliJ打开了问题,以改善这种情况下的可用性。
从好的方面来说,由于您使用的是IntelliJ IDEA 2016.2,因此不需要使用测试套件支持。相反,您只需src/test/java
在IDEA的项目视图中右键单击并选择Run 'All Tests'
,这将运行所有测试。
问候,
Sam (JUnit 5核心提交者)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句