@ Before,@ BeforeClass,@ BeforeEach和@BeforeAll之间的区别


432

之间的主要区别是什么

  • @Before@BeforeClass
    • 和JUnit中5 @BeforeEach@BeforeAll
  • @After@AfterClass

根据JUnit @Before,在以下情况下使用Api

编写测试时,通常会发现几个测试需要先创建类似的对象,然后才能运行。

@BeforeClass可以用来建立数据库连接。但是不能@Before一样吗?

Answers:


623

标记的代码@Before在每次测试之前执行,而@BeforeClass在整个测试夹具之前运行一次。如果您的测试类有十个测试,则@Before代码将执行十次,但@BeforeClass仅执行一次。

通常,@BeforeClass当多个测试需要共享相同的计算昂贵的设置代码时,可以使用。建立数据库连接属于此类。您可以将代码从@BeforeClass移到@Before,但测试运行可能需要更长的时间。注意,标记的代码@BeforeClass作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。

JUnit 5中,标记@BeforeEach和与JUnit 4中@BeforeAll@Before和等效。@BeforeClass它们的名称更能指示它们的运行时间,松散地解释为:“在每个测试之前”和“一次在所有测试之前”。


4
嗯,现在使用数据库连接的例子很有意义。谢谢!
2013年

6
@pacoverflow @BeforeClas是静态的。它在创建测试类实例之前运行。
dasblinkenlight 2015年

1
请记住,当您使用@BeforeClass时,您的方法/参数必须是静态的
tiagocarvalho92

它没有直接关系,但是这是一种计算“类别测试”计数器的方法
Bsquare

我只添加@BeforeAll可能是非静态的,并在每次运行新的测试实例时调用它。请参阅相应的答案stackoverflow.com/a/55720750/1477873
Sergey,

124

每个注释之间的区别是:

+-------------------------------------------------------------------------------------------------------+
¦                                       Feature                            ¦   Junit 4    ¦   Junit 5   ¦
¦--------------------------------------------------------------------------+--------------+-------------¦
¦ Execute before all test methods of the class are executed.               ¦ @BeforeClass ¦ @BeforeAll  ¦
¦ Used with static method.                                                 ¦              ¦             ¦
¦ For example, This method could contain some initialization code          ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after all test methods in the current class.                     ¦ @AfterClass  ¦ @AfterAll   ¦
¦ Used with static method.                                                 ¦              ¦             ¦
¦ For example, This method could contain some cleanup code.                ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute before each test method.                                         ¦ @Before      ¦ @BeforeEach ¦
¦ Used with non-static method.                                             ¦              ¦             ¦
¦ For example, to reinitialize some class attributes used by the methods.  ¦              ¦             ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after each test method.                                          ¦ @After       ¦ @AfterEach  ¦
¦ Used with non-static method.                                             ¦              ¦             ¦
¦ For example, to roll back database modifications.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

两个版本中的大多数注释都相同,但几乎没有区别。

参考

执行顺序。

虚线框->可选注释。

在此处输入图片说明


10

JUnit中的BeforeClass和BeforeClass

函数@Before注释将在具有@Test注释的类中的每个测试函数之前执行,但是with函数@BeforeClass将仅在类中的所有测试函数之前执行一次。

类似地,带有@After注解的函数将在类中具有@Test注解的每个测试函数之后执行,但是带注解的函数@AfterClass将在类中的所有测试函数之后仅执行一次。

样本类

public class SampleClass {
    public String initializeData(){
        return "Initialize";
    }

    public String processDate(){
        return "Process";
    }
 }

样品测试

public class SampleTest {

    private SampleClass sampleClass;

    @BeforeClass
    public static void beforeClassFunction(){
        System.out.println("Before Class");
    }

    @Before
    public void beforeFunction(){
        sampleClass=new SampleClass();
        System.out.println("Before Function");
    }

    @After
    public void afterFunction(){
        System.out.println("After Function");
    }

    @AfterClass
    public static void afterClassFunction(){
        System.out.println("After Class");
    }

    @Test
    public void initializeTest(){
        Assert.assertEquals("Initailization check", "Initialize", sampleClass.initializeData() );
    }

    @Test
    public void processTest(){
        Assert.assertEquals("Process check", "Process", sampleClass.processDate() );
    }

}

输出量

Before Class
Before Function
After Function
Before Function
After Function
After Class

在Junit 5中

@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll

1
很好的例子。
kanaparthikiran 18/09/15

2
import org.junit.Assert
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test

class FeatureTest {
    companion object {
        private lateinit var heavyFeature: HeavyFeature
        @BeforeClass
        @JvmStatic
        fun beforeHeavy() {
            heavyFeature = HeavyFeature()
        }
    }

    private lateinit var feature: Feature

    @Before
    fun before() {
        feature = Feature()
    }

    @Test
    fun testCool() {
        Assert.assertTrue(heavyFeature.cool())
        Assert.assertTrue(feature.cool())
    }

    @Test
    fun testWow() {
        Assert.assertTrue(heavyFeature.wow())
        Assert.assertTrue(feature.wow())
    }
}

如同

import org.junit.Assert
import org.junit.Test

 class FeatureTest {
    companion object {
        private val heavyFeature = HeavyFeature()
    }

    private val feature = Feature()

    @Test
    fun testCool() {
        Assert.assertTrue(heavyFeature.cool())
        Assert.assertTrue(feature.cool())
    }

    @Test
    fun testWow() {
        Assert.assertTrue(heavyFeature.wow())
        Assert.assertTrue(feature.wow())
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.