junit5——入门


本文档来自于自己对官方文档的学习和翻译。

JUnit 5是JUnit的下一代。目标是为JVM上的开发人员端测试创建一个最新的基础。这包括专注于Java 8及更高版本,以及启用许多不同风格的测试。

Junit 5 用户指南

概述

与以前版本的 JUnit 不同,JUnit 5 由来自三个不同子项目的几个不同模块组成。

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit Platform为在JVM上启动测试框架提供基础。它还定义了TestEngineAPI, 用来开发在平台上运行的测试框架。此外,平台提供了一个控制台启动器,用于从命令行启动平台,并为Gradle和Maven提供构建插件以及基于JUnit 4的Runner,用于在平台上运行任意TestEngine

JUnit Jupiter是在JUnit 5中编写测试和扩展的新型[编程模型]和[扩展模型]的组合。Jupiter子项目提供了TestEngine,用于在平台上运行基于Jupiter的测试。

JUnit Vintage提供TestEngine,用于在平台上运行基于JUnit 3和JUnit 4的测试。

JUnit 5 在运行时需要 Java 8(或更高版本)。但是,您仍然可以测试使用以前版本的 JDK 编译的代码。

编写测试

第一个测试用例

package com.example.project;

public class Calculator {

	public int add(int a, int b) {
		return a + b;
	}

}

测试用例


package com.example.project;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class CalculatorTests {

	@Test
	@DisplayName("1 + 1 = 2")
	void addsTwoNumbers() {
		Calculator calculator = new Calculator();
		assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
	}
}

注解

JUnit Jupiter 支持以下用于配置测试和扩展框架的注解。

除非另有说明,所有核心注释都位于模块中的org.junit.jupiter.api包中junit-jupiter-api

注解 描述
@Test 表示方法是测试方法。
@ParameterizedTest 表示方法是参数化测试。
@RepeatedTest 表示方法是重复测试的测试模板。
@TestFactory 表示方法是动态测试测试工厂。
@TestTemplate 表示方法是测试用例模板,旨在根据注册提供程序返回的调用上下文的数量多次调用。这些方法是继承的,除非它们被覆盖
@TestMethodOrder 用于为注解的测试类配置测试方法执行顺序。
@TestInstance 用于为带注释的测试类配置测试实例生命周期。
@DisplayName 声明测试类或测试方法的自定义显示名称。此类注释不会被继承。
@DisplayNameGeneration 为测试类声明一个自定义显示名称生成器。此类注释是继承的
@BeforeEach 表示被注解的方法应该在当前类中的每个、、、 或方法之前 执行;类似于 JUnit 4 的. 这些方法是继承的,除非它们被覆盖@Test``@RepeatedTest``@ParameterizedTest``@TestFactory``@Before
@AfterEach 表示该注释的方法应该被执行之后 每个 @Test@RepeatedTest@ParameterizedTest,或@TestFactory方法在当前类; 类似于 JUnit 4 的@After
@BeforeAll 表示该注释的方法应该被执行之前 所有 @Test@RepeatedTest@ParameterizedTest,和@TestFactory方法在当前类; 类似于 JUnit 4 的@BeforeClass. 此类方法是继承的(除非它们被隐藏覆盖)并且必须是static(除非使用“class“测试实例生命周期)。
@AfterAll 表示该注释的方法应该被执行之后 的所有 @Test@RepeatedTest@ParameterizedTest,和@TestFactory方法在当前类; 类似于 JUnit 4 的@AfterClass. 此类方法是继承的(除非它们被隐藏覆盖)并且必须是static(除非使用“class”[测试实例生命周期])。

标准测试类

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class StandardTests {

    @BeforeAll
    static void initAll() {
    }

    @BeforeEach
    void init() {
    }

    @Test
    void succeedingTest() {
    }

    @Test
    void failingTest() {
        fail("a failing test");
    }

    @Test
    @Disabled("for demonstration purposes")
    void skippedTest() {
        // not executed
    }

    @Test
    void abortedTest() {
        assumeTrue("abc".contains("Z"));
        fail("test should have been aborted");
    }

    @AfterEach
    void tearDown() {
    }

    @AfterAll
    static void tearDownAll() {
    }

}

文章作者: WangQingLei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WangQingLei !
  目录