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:表示嵌套执行



    知道、想到、做到、得到
  • 相关阅读:
    C#Redis缓存帮助类
    [RxSwift教程]9、过滤操作符:filter、take、skip等
    [RxSwift教程]8、变换操作符:buffer、map、flatMap、scan等
    [RxSwift教程]7、Subjects、Variables
    [RxSwift教程]6、观察者2: 自定义可绑定属性
    [RxSwift教程]5、观察者1: AnyObserver、Binder
    [RxSwift教程]4、Observable订阅、事件监听、订阅销毁
    [RxSwift教程]3、Observable介绍、创建可观察序列
    [RxSwift教程]2、响应式编程与传统式编程的比较样例
    [RxSwift教程]1、安装、介绍
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14762591.html
Copyright © 2011-2022 走看看