[英]Confusing execution order of JUnit methods
I've created a custom runner by extending Suite
:我通过扩展Suite
创建了一个自定义运行器:
public class MyRunner extends Suite {
public MyRunner(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
@Override
public void run(RunNotifier notifier) {
notifier.addListener(new MyRunListener());
notifier.fireTestRunStarted(getDescription());
super.run(notifier);
}
}
As seen in run
this registeres a MyRunListener
which looks like this:正如在run
中看到的那样,它注册了一个MyRunListener
,如下所示:
class MyRunListener extends RunListener {
@Override
public void testRunStarted(Description description) {
System.err.println("1: run started");
}
@Override
public void testStarted(Description description) {
System.err.println("2: test started");
}
@Override
public void testFinished(Description description) {
System.err.println("3: test finished");
}
@Override
public void testRunFinished(Result result) {
System.err.println("4: run finished");
}
}
I've added 1:
, 2:
, 3:
and 4:
according to the order in which I expect these methods to be called.根据我希望调用这些方法的顺序,我添加了1:
、 2:
、 3:
和4:
。
I've then created a test suite that looks as follows:然后我创建了一个如下所示的测试套件:
@RunWith(MyRunner.class)
@Suite.SuiteClasses({ MyTest.class })
public class MyTestSuite {
}
And the following test:以及以下测试:
public class MyTest {
@BeforeClass
public static void beforeClass() {
System.err.println("A: beforeClass");
}
@Before
public void before() {
System.err.println("B: before");
}
@Test
public void test() {
System.err.println("C: Running actual test...");
}
@After
public void after() {
System.err.println("D: after");
}
@AfterClass
public static void afterClass() {
System.err.println("E: afterClass");
}
}
Again, output labeled according to the order I expect.同样,output 按照我期望的顺序标记。
Here's the output I get when I run MyTestSuite
through IntelliJ (as a JUnit run configuration):这是我通过 IntelliJ 运行MyTestSuite
时得到的 output(作为 JUnit 运行配置):
2: test started
B: before
C: Running actual test...
D: after
3: test finished
E: afterClass
4: run finished
1: run started
A: beforeClass
Why on earth am I getting that output order?为什么我会收到 output 订单? I must be doing something very wrong in my runner, but I've just implemented it according to tutorials on the web.我的跑步者一定做错了什么,但我刚刚根据 web 上的教程实现了它。 (Using JUnit 4.12) (使用 JUnit 4.12)
As @DmitryB suggests in the comments, it seems to be some form of output buffering.正如@DmitryB 在评论中所建议的那样,它似乎是某种形式的 output 缓冲。 The various parts of the output is then stitched together in a weird way after the test has finished.测试完成后,output 的各个部分以一种奇怪的方式缝合在一起。
I added Thread.sleep(1000)
after each printout and this is how it looks when I run it in IntelliJ:我在每次打印输出后添加了Thread.sleep(1000)
,这就是我在 IntelliJ 中运行它时的样子:
The workaround for now is to use the gradle task run configuration rather than the JUnit one.目前的解决方法是使用 gradle 任务运行配置而不是 JUnit 之一。
Filed this ticket at Jetbrains.在 Jetbrains 提交了这张票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.