简体   繁体   English

是否有任何扩展 JUnit4 测试类的约定?

[英]Are there any conventions of extending JUnit4 test classes?

I have a lot of common logic for my test, so I decide to share it by extending.我的测试有很多共同的逻辑,所以我决定通过扩展来分享。 I've wrote two classes: TestNumberOne which extends TestBase .我写了两个类: TestNumberOne ,它扩展了TestBase

TestBase.java测试库

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

/**
 * @author Pavel
 * @since 2013-03-03
 */
public class TestBase {

    @BeforeClass
    public static void beforeClass() {
        System.out.println("beforeClass() in TestBase");
        System.out.flush();
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("afterClass() in TestBase");
        System.out.flush();
    }

    @Before
    public void before() {
        System.out.println("before() in TestBase");
        System.out.flush();
    }

    @After
    public void after() {
        System.out.println("after() in TestBase");
        System.out.flush();
    }
}

TestNumberOne.java TestNumberOne.java

import org.junit.*;

/**
 * @author Pavel
 * @since 2013-03-03
 */
public class TestNumberOne extends TestBase {

    @Test
    public void anyTest() {
        System.out.println("anyTest() in TestNumberOne");
        System.out.flush();
    }
}

I've got such a strange output when I execute my tests:当我执行测试时,我得到了如此奇怪的输出:

before() in TestBase
anyTest() in TestNumberOne
after() in TestBase
beforeClass() in TestBase
afterClass() in TestBase

Why does it has so strange order?为什么它有这么奇怪的顺序? And are there any conventions of extending JUnit test classes?是否有任何扩展 JUnit 测试类的约定?

UPDATE:更新:

  1. Tests are run in IDEA测试在 IDEA 中运行
  2. To get such a strange results I've run them several times (other results was as expected)为了得到如此奇怪的结果,我已经运行了几次(其他结果正如预期的那样)

This is definitely an IntelliJ IDEA issue.这绝对是一个 IntelliJ IDEA 问题。

If I run your code by maven, it runs ok;如果我通过 maven 运行你的代码,它运行正常; if I run it in IntelliJ several times, then I sometimes get the incorrect output as you do.如果我在 IntelliJ 中多次运行它,那么有时我会像您一样得到不正确的输出。

Actually, I found a way to reproduce it:实际上,我找到了一种重现它的方法:

  • Add Thread.sleep(1000) after each output message.在每个输出消息之后添加 Thread.sleep(1000)。
  • Turn off "Track running test" in Run test window (blue circle above list of run tests)关闭“运行测试”窗口中的“跟踪运行测试”(运行测试列表上方的蓝色圆圈)
  • Run whole TestNumberOne test class in IntelliJ (even though you have only one test method) -> output should be in correct order在 IntelliJ 中运行整个 TestNumberOne 测试类(即使您只有一种测试方法)-> 输出应该按正确顺序
  • click on the anyTest method in the test list and then at the TestNumberOne -> output is in incorrect order单击测试列表中的 anyTest 方法,然后在 TestNumberOne -> 输出顺序不正确

(also if you run it with sleep, you see that the output is in correct order but gets reordered when the test ends) (此外,如果您使用 sleep 运行它,您会看到输出顺序正确,但在测试结束时会重新排序)

So they are run in correct order, only the output is messed up.所以它们以正确的顺序运行,只有输出混乱。

When I try your code, I just get the expected output, ie当我尝试你的代码时,我只是得到了预期的输出,即

beforeClass() in TestBase
before() in TestBase
anyTest() in TestNumberOne
after() in TestBase
afterClass() in TestBase

(launched with Eclipse). (与 Eclipse 一起启动)。 Which is the convention ^^ Your result is really strange indeed ...这是惯例^^你的结果确实很奇怪......

don't know of any conventions.不知道任何约定。 in junit 3, the base class was usually abstract.在junit 3 中,基类通常是抽象的。

the following seems to come out in a sane order (except perhaps for the teardowns).以下内容似乎以合理的顺序出现(也许拆解除外)。

import org.junit.*;
public class BaseTestCase {
    public static String method() {
        return Thread.currentThread().getStackTrace()[2].getMethodName() + "()";
    }
    @BeforeClass public static void classSetupBaseClass() {
        System.out.println(method());
    }
    @AfterClass public static void classTeardownBaseClass() {
        System.out.println(method());
    }
    @Before public void setupBaseClass() {
        System.out.println(method());
    }
    @After public void teardownBaseClass() {
        System.out.println(method());
    }
    @Test public void aTestInBaseClass() {
        System.out.println(method());
    }
}


import static org.junit.Assert.*;
import org.junit.*;

public class  So15183669 extends BaseTestCase {
    @BeforeClass public static void classSetup() {
        System.out.println(method());
    }
    @AfterClass public static void classTeardown() {
        System.out.println(method());
    }
    @Before public void setup() {
        System.out.println(method());
    }
    @After public void teardown() {
        System.out.println(method());
    }
    @Test public void aTest() {
        System.out.println(method());
    }
}


classSetupBaseClass()
classSetup()
setupBaseClass()
setup()
aTest()
teardown()
teardownBaseClass()
setupBaseClass()
setup()
aTestInBaseClass()
teardown()
teardownBaseClass()
classTeardown()
classTeardownBaseClass()

I think when you run the tests, your TestBase is also being run as it's own junit test.我认为当您运行测试时,您的 TestBase 也正在运行,因为它是自己的 junit 测试。 Try calling it HelperBase instead.尝试将其称为 HelperBase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM