zoukankan      html  css  js  c++  java
  • junit5常用注解

    Junit5使用注解配置测试和扩展框架

    @BeforeAll:表示在所有单元测试之前执行,只执行一次

    @BeforeEach:表示在每个单元测试之前执行,假如测试类有n个测试方法,则被执行n次

    @Test:表示方法是测试方法。但是与junit4的@Test不同,它的职责非常单一,不能声明任何属性,拓展的测试将会由Jupiter提供额外测试

    @AfterEach:表示在每个单元测试之后执行,假如测试类有n个测试方法,则被执行n次

    @AfterAll:表示在所有单元测试之后执行,只执行一次

    示例:

    package com.testcase;
    
    import org.junit.jupiter.api.*;
    
    
    public class Junit5DemoTest {
    
        @BeforeAll
        public static void initAll(){
            System.out.println("init all test");
        }
    
        @BeforeEach
        public void init(){
            System.out.println("init a test");
        }
    
        @Test
        void fun(){
            System.out.println("fun");
        }
    
        @AfterEach
        public void tearDown(){
            System.out.println("tear down a test");
        }
    
        @AfterAll
        public static void tearDownAll(){
            System.out.println("tear down all test");
        }
    }

    运行结果:

    init all test
    
    init a test
    fun
    tear down a test
    
    tear down all test
    
    Process finished with exit code 0

    @RepeatedTest:表示方法额外执行的次数

    package com.testcase;
    
    import org.junit.jupiter.api.*;
    
    
    public class Junit5DemoTest {
    
        @BeforeAll
        public static void initAll(){
            System.out.println("init all test");
        }
    
        @BeforeEach
        public void init(){
            System.out.println("init a test");
        }
    
        @Test
        @RepeatedTest(1)
        void fun(){
            System.out.println("fun");
        }
    
        @AfterEach
        public void tearDown(){
            System.out.println("tear down a test");
        }
    
    
        @AfterAll
        public static void tearDownAll(){
            System.out.println("tear down all test");
        }
    }

    运行结果:

    @Disabled:表示测试类或测试方法不执行,类似于Junit4中的@Ignore

     @DisplayName:为测试类或测试方法设置展示名称

    package com.testcase;
    
    import org.junit.jupiter.api.*;
    
    @DisplayName("Junit5演示类")
    public class Junit5DemoTest {
    
        @BeforeAll
        public static void initAll(){
            System.out.println("init all test");
        }
    
        @BeforeEach
        public void init(){
            System.out.println("init a test");
        }
    
        @DisplayName("fun测试方法")
        @Test
        void fun(){
            System.out.println("fun");
        }
    
        @Test
        @Disabled
        @DisplayName("fun1测试方法")
        void fun1(){
            System.out.println("fun1");
        }
    
        @AfterEach
        public void tearDown(){
            System.out.println("tear down a test");
        }
    
    
        @AfterAll
        public static void tearDownAll(){
            System.out.println("tear down all test");
        }
    }

    运行结果:

    @Tag:表示单元测试类别,类似于Junit4中的@Categories

    表示为测试类或测试方法打一个标签。一般来说不能单独使用,需要配合其他的标签一起使用,具体详看Junit5套件执行

    @ParameterizedTest:表示方法是参数化测试

    具体详看jUnit5参数化

    @Timeout:表示测试方法超过了指定时间将返回错误

    @ExtendWith:为测试类或测试方法提供扩展类引用

    @Nested:表示嵌套执行



    知道、想到、做到、得到
  • 相关阅读:
    webpack基础
    LeetCode232. 用栈实现队列做题笔记
    mysql 时间加减一个月
    leetcode 1381. 设计一个支持增量操作的栈 思路与算法
    LeetCode 141. 环形链表 做题笔记
    leetcode 707. 设计链表 做题笔记
    leetcode 876. 链表的中间结点 做题笔记
    leetcode 143. 重排链表 做题笔记
    leetcode 1365. 有多少小于当前数字的数字 做题笔记
    LeetCode1360. 日期之间隔几天 做题笔记
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14762591.html
Copyright © 2011-2022 走看看