java JUnit 5 - 使用 JUnit Jupiter 引擎时 IntelliJ IDEA 中的空测试套件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39085905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit 5 - Empty test suite in IntelliJ IDEA when using JUnit Jupiter engine
提问by Davideas
How to execute All Suite tests with JUnit 5 in IntelliJ IDEA v2016.2.2?
如何在 IntelliJ IDEA v2016.2.2 中使用 JUnit 5 执行所有套件测试?
I get Empty test suiterunning this code:
我得到运行此代码的空测试套件:
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 {
}
I receive:
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
Empty test suite.
[root]
JUnit Jupiter
JUnit Vintage
OR
或者
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 {
}
I receive:
我收到:
INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
[root]
|+--JUnit Vintage
| +--eu.....AllTests
|+--JUnit Jupiter
I was able to run suite with JUnit 4, but it doesn't work with JUnit 5.
我能够使用 JUnit 4 运行套件,但它不适用于 JUnit 5。
回答by Sam Brannen
Short Answer
简答
If you are using IntelliJ IDEA 2016.2, it is currently not possible to execute a test class annotated with @RunWith(JUnitPlatform.class)
within the IDE.
如果您使用的是 IntelliJ IDEA 2016.2,目前无法@RunWith(JUnitPlatform.class)
在 IDE 中执行注释的测试类。
Long Answer
长答案
Based on the behavior you reported, after some painstaking investigative work, I believe I have the answer to your question...
根据你举报的行为,经过一番艰苦的调查工作,我相信我有你的问题的答案......
If you are using IntelliJ IDEA 2016.2 which has built-in support for JUnit 5, then the following is what is happening.
如果您使用的 IntelliJ IDEA 2016.2 内置了对 JUnit 5 的支持,那么正在发生的事情如下。
- IDEA launches the JUnit Platform via the
Launcher
API, selecting the test class annotated with@RunWith(JUnitPlatform.class)
(let's call itTestSuite
). - The
Launcher
detects both thejunit-jupiter
andjunit-vintage
TestEngine
implementations. - The JUnit Jupiter engine ignores
TestSuite
since it is technically not a JUnit Jupiter test class. - The JUnit Vintage engine also ignores
TestSuite
since it is annotated with@RunWith(JUnitPlatform.class)
. - The end result is that neither registered test engine claims that it can run the
TestSuite
class.
- IDEA 通过
Launcher
API启动 JUnit 平台,选择带有注释的测试类@RunWith(JUnitPlatform.class)
(我们称之为TestSuite
)。 - 将
Launcher
检测这两个junit-jupiter
和junit-vintage
TestEngine
实现。 - JUnit Jupiter 引擎会忽略
TestSuite
它,因为它在技术上不是 JUnit Jupiter 测试类。 - JUnit Vintage 引擎也会忽略,
TestSuite
因为它用@RunWith(JUnitPlatform.class)
. - 最终结果是注册的测试引擎都没有声称它可以运行
TestSuite
该类。
The non-intuitive part is that the JUnit Vintage engine ignores TestSuite
, when it in fact looks like a JUnit 4 based test class since it's annotated with @RunWith()
. The reason it is ignored is to avoid infinite recursion, which is explained in the source code for DefensiveAllDefaultPossibilitiesBuilder:
不直观的部分是 JUnit Vintage 引擎忽略了TestSuite
,而实际上它看起来像一个基于 JUnit 4 的测试类,因为它用@RunWith()
. 忽略它的原因是为了避免无限递归,这在DefensiveAllDefaultPossibilitiesBuilder的源代码中有解释:
if ("org.junit.platform.runner.JUnitPlatform".equals(runnerClass.getName())) {
return null;
}
The fact that the above code returns null
in such scenarios results in an empty suite.
上述代码null
在这种情况下返回的事实导致一个空套件。
Of course, it would certainly be better if the user were informed of such scenarios -- for example, via a log statement. I have therefore opened issues for both JUnit 5and IntelliJto improve the usability in such scenarios.
当然,如果将此类情况告知用户当然会更好——例如,通过日志语句。因此,我为JUnit 5和IntelliJ打开了问题,以提高这种情况下的可用性。
On the plus side, since you're using IntelliJ IDEA 2016.2, you don't need to use the test suite support. Instead, you can simply right-click on src/test/java
in the project view in IDEA and select Run 'All Tests'
, and that will run all your tests.
从好的方面来说,由于您使用的是 IntelliJ IDEA 2016.2,因此您不需要使用测试套件支持。相反,您只需src/test/java
在 IDEA 的项目视图中右键单击并选择Run 'All Tests'
,这将运行您的所有测试。
Regards,
问候,
Sam (JUnit 5 core committer)
Sam (JUnit 5 核心提交者)
回答by Highchiller
Additional to Sam Brannen's answer. I needed a TestSuit to setup the backend before running all test classes in a package. This is probably not possible via Run 'All Tests'
.
除了Sam Branen 的回答。在运行包中的所有测试类之前,我需要一个 TestSuit 来设置后端。这可能无法通过Run 'All Tests'
.
But I think I found a good workaround for it. I came up with this:
但我想我找到了一个很好的解决方法。我想出了这个:
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 {}
}
You could extend each test class from another package and add the @Nested
annotation to it. It is not the best solution, but it is a workaround until IDEA or JUnit 5 find another solution for this cases.
您可以从另一个包中扩展每个测试类@Nested
并向其添加注释。这不是最好的解决方案,但它是一种解决方法,直到 IDEA 或 JUnit 5 为这种情况找到另一个解决方案。
回答by tnas
According to junit oficial website:
根据junit官方网站:
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
只需确保 junit-vintage-engine 工件在您的测试运行时路径中。在这种情况下,JUnit 3 和 JUnit 4 测试将被 JUnit 平台启动器自动选取