Test Development Knowledge
Test Development Knowledge
TTD
Test Driven Development: 测试驱动开发: 在编写实际功能代码之前先编写测试代码。
基本步骤
- **Writing failed tests **:首先,为即将要开发的功能或修改编写一个测试,并确保在没有相应功能的情况下,这个测试是失败的。
- Write functional code:然后,尽量简单地编写功能代码,使其能够通过测试。
- reconfiguration:当测试通过后,可以考虑重构代码,优化结构,确保在不改变功能的前提下提高代码的可读性和可维护性。在重构的过程中,应当确保已有的测试仍然能够通过,以确保功能没有被破坏。
- Repeat the above steps:对于每一个新的功能或修改,都重复以上的步骤。
Junit Test
Junit5 Basic Conceptions
Library
JUnit Platform: 提供了在 JVM 上启动测试框架的基础。可以运行JUnit 5,也支持其他测试框架。
JUnit Jupiter: JUnit 5 的核心部分,提供新的编写和运行测试的模型。
JUnit Vintage: 提供对运行早期版本(JUnit 3 和 JUnit 4)的测试的支持。
Core Decorator
生命周期方法
@BeforeEach 每次测试前都要执行的方法
@AfterEach 每次测试后都要执行的方法
@BeforeAll 标记所有测试开始前只执行一次的方法。该方法必须是 static
@AfterAll 记所有测试结束后只执行一次的方法。该方法必须是 static
测试方法
@Test
: 标记一个方法为测试方法。
@DisplayName
: 可为测试方法或测试类定义一个自定义的显示名称。
@Nested
: 支持内部测试类,有助于更结构化地组织测试。
@Tag
: 允许分类测试,例如“快速”、“慢速”等。
条件执行
在满足某些条件时才执行测试
@EnabledOnOs,
@EnabledOnJre,
@EnabledIfSystemProperty,
@EnabledIfEnvironmentVariable
禁用某个特定的测试
@Disabled
参数注入
定义一个参数化测试: @ParameterizedTest。
为参数化测试提供数据: @ValueSource,
@EnumSource,
@MethodSource,
@CsvSource,
@CsvFileSource。
多次运行一个测试:@RepeatedTest。
其它参数
@DisplayName
主要是辅助功能,增加代码的可以阅读性
1 | @DisplayName("some readiable content here!") |
Eg:
1 | package org.example; |
Fail() method
Used to clearly indicate that the test case should fail.
When you expect a code block not to be executed, but it is indeed executed.
When you test the exception handling logic, you expect an exception to be thrown, but not.
Eg:
对于下面例子
假设 some应该报错,之后 走 catch 路线
但是没有报错的话, 就走了 fail路线,这样会 爆出 fail(我们在这里设置的异常)
1 | try { |
assertEquals
Used to assert whether two values are equal
Basis assertEquals
1 | assertEquals(Object expected, Object actual) |
With message parameters
1 | assertEquals(Object expected, Object actual, String message) |
For comparing double and float types, add the delta parameter
1 | assertEquals(double expected, double actual, double delta) |
Comparison of Arrays
1 | assertArrayEquals(Object[] expected, Object[] actual) |
Specific Steps (Actual Practice)
Configuring the environment
1 | <!-- JUnit Jupiter (JUnit 5) API --> |
Write a test class
eg:
1 | public class Calculator { |
1 | import static org.junit.jupiter.api.Assertions.assertEquals; |
Eg2:
展示
1 | import org.junit.jupiter.api.*; |
实战
进入某个类文件中
shift + command + T 快捷键 直接创建对应的Test class