首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“前”、“前类”、“前”和“前”之间的区别

“前”、“前类”、“前”和“前”之间的区别
EN

Stack Overflow用户
提问于 2013-11-30 01:43:17
回答 7查看 348.4K关注 0票数 494

主要的区别是

  • @Before@BeforeClass
    • 在JUnit 5中,@BeforeEach@BeforeAll

  • @After@AfterClass

根据JUnit Api@Before用于以下情况:

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

@BeforeClass可用于建立数据库连接。但是@Before不能做同样的事情吗?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-11-30 01:50:15

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

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

JUnit 5中,标记@BeforeEach@BeforeAll相当于JUnit 4中的@Before@BeforeClass。它们的名称更多地表示它们何时运行,松散地解释为:“在每个测试之前”和“在所有测试之前一次”。

票数 723
EN

Stack Overflow用户

发布于 2017-07-11 14:49:25

每个注释之间的差异是:

代码语言:javascript
复制
+-------------------------------------------------------------------------------------------------------+
¦                                       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.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

两个版本中的大多数注释都是相同的,但很少有不同之处。

参考文献

执行顺序.

虚线框->可选注释。

票数 168
EN

Stack Overflow用户

发布于 2018-08-13 11:05:38

and与BeforeClass in JUnit

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

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

SampleClass

代码语言:javascript
复制
public class SampleClass {
    public String initializeData(){
        return "Initialize";
    }

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

SampleTest

代码语言:javascript
复制
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() );
    }

}

输出

代码语言:javascript
复制
Before Class
Before Function
After Function
Before Function
After Function
After Class

中的Junit 5

代码语言:javascript
复制
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20295578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档