junit @Before、@BeforeClass、@BeforeEach和@BeforeAll之间的差异

thigvfpy  于 2022-11-11  发布在  其他
关注(0)|答案(7)|浏览(276)

两者的主要区别是什么

  • @Before@BeforeClass
  • 以及JUnit 5中的@BeforeEach@BeforeAll
  • @After@AfterClass

根据JUnit Api@Before用于以下情况:
在编写测试时,通常会发现多个测试需要创建类似的对象才能运行。
@BeforeClass可以用来建立数据库连接,但是@Before不能做同样的事情吗?

k3fezbri

k3fezbri1#

标记为@Before的代码在每次测试之前执行,而@BeforeClass在整个测试夹具之前运行一次。如果您的测试类有十个测试,@Before代码将执行十次,但@BeforeClass将只执行一次。
一般来说,当多个测试需要共享计算开销很大的相同设置代码时,您可以使用@BeforeClass。建立数据库连接福尔斯这一类。您可以将代码从@BeforeClass移动到@Before,但您的测试运行可能需要更长的时间。请注意,标记为@BeforeClass的代码是作为静态初始化程序运行的,因此它将在测试fixture的类示例创建之前运行。
JUnit 5中,标记@BeforeEach@BeforeAll相当于JUnit 4中得@Before@BeforeClass.它们得名称更多地表示它们得运行时间,解释得比较松散:“每次测试前”和“所有测试前一次”。

ruarlubt

ruarlubt2#

每个注解之间的差异是:

+-------------------------------------------------------------------------------------------------------+
¦                                       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.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

两个版本中的注解大部分相同,但很少有不同。
Reference

执行命令。

虚线框-〉可选注解。

dbf7pr2w

dbf7pr2w3#

JUnit中的Before和BeforeClass

函数@Before注解将在具有@Test注解的类中的每个测试函数之前执行,但具有@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
ujv3wf0j

ujv3wf0j4#

JUnit @每个之前、@每个之后、@所有之前、@所有之后

@Before(JUnit 4)-〉@BeforeEach(JUnit 5)-每次测试之前调用方法
@After(JUnit 4)-〉@AfterEach(JUnit 5)-每次测试调用方法
@BeforeClass(JUnit 4)-〉@BeforeAll(JUnit 5)-static方法在执行该类中的所有测试之前被调用。它可能是一个大型任务,如启动服务器、读取文件、建立数据库连接...
@AfterClass(JUnit 4)-〉@AfterAll(JUnit 5)-静态方法执行该类中的所有测试后被调用。

kx1ctssn

kx1ctssn5#

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())
    }
}
qcuzuvrc

qcuzuvrc6#

所有这些注解之间的基本区别如下-
1.@BeforeEach-用于在执行每个测试方法之前(例如设置)运行公共代码。类似于JUnit 4的@Before。
1.@AfterEach-用于在每个测试方法执行之后(例如,tearDown)运行公共代码。类似于JUnit 4的@After。
1.@BeforeAll-用于在执行任何测试之前对每个类运行一次。类似于JUnit 4的@BeforeClass。
1.@AfterAll-用于在执行所有测试后对每个类运行一次。类似于JUnit 4@AfterClass。
所有这些注解沿着用法都在Codingeek - Junit5 Test Lifecycle上定义

uqzxnwby

uqzxnwby7#

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

相关问题