JUnit 5条件测试示例

JUnit 5条件测试

本文向您展示如何使用JUnit 5根据条件启用或禁用测试。

PS已通过JUnit 5.5.2测试

1.操作系统

1.1通过@EnabledOnOs@DisabledOnOs注释基于特定操作系统启用或禁用的测试。

OperatingSystemTest.java
package com.mkyong.conditional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

public class OperatingSystemTest {

    @Test
    @EnabledOnOs({OS.LINUX, OS.WINDOWS})
    void onLinuxOrWindows() {
        System.out.println("Run this on Linux or Windows!");
    }

    @Test
    @EnabledOnOs({OS.WINDOWS})
    void onWindows() {
        System.out.println("Run this on Windows!");
    }

    @Test
    @DisabledOnOs(OS.WINDOWS)
    void notOnWindows() {
        System.out.println("Do not run this on Windows!");
    }

    @Test
    @DisabledOnOs({OS.WINDOWS, OS.AIX, OS.SOLARIS, OS.MAC})
    void notOnWindowsOrAixOrSolarisOrMac() {
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现对快速排序条件覆盖测试,可以借助JUnit框架实现。以下是一个可能的实现示例: ```java import static org.junit.Assert.assertArrayEquals; import org.junit.Test; public class QuickSortTest { @Test public void testQuickSort() { int[] arr = { 3, 7, 2, 1, 6, 5, 4 }; int[] expected = { 1, 2, 3, 4, 5, 6, 7 }; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } @Test public void testQuickSortWithEmptyArray() { int[] arr = {}; int[] expected = {}; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } @Test public void testQuickSortWithSingleElement() { int[] arr = { 1 }; int[] expected = { 1 }; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } @Test public void testQuickSortWithAllSameElements() { int[] arr = { 5, 5, 5, 5, 5 }; int[] expected = { 5, 5, 5, 5, 5 }; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } @Test public void testQuickSortWithSortedArray() { int[] arr = { 1, 2, 3, 4, 5 }; int[] expected = { 1, 2, 3, 4, 5 }; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } @Test public void testQuickSortWithReverseSortedArray() { int[] arr = { 5, 4, 3, 2, 1 }; int[] expected = { 1, 2, 3, 4, 5 }; QuickSort.quickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } // 可以继续添加更多的测试用例,覆盖更多的情况 } ``` 在上面的示例代码中,我们针对不同情况编写了多个测试用例,以尽可能地覆盖快速排序算法中的各个条件分支: - `testQuickSort`:测试基本情况,输入数组中包含多个不同元素。 - `testQuickSortWithEmptyArray`:测试特殊情况,输入数组为空。 - `testQuickSortWithSingleElement`:测试特殊情况,输入数组只包含一个元素。 - `testQuickSortWithAllSameElements`:测试特殊情况,输入数组中所有元素都相等。 - `testQuickSortWithSortedArray`:测试特殊情况,输入数组已经有序。 - `testQuickSortWithReverseSortedArray`:测试特殊情况,输入数组已经逆序排列。 在每个测试方法中,我们先定义一个输入数组`arr`和期望输出数组`expected`,然后调用快速排序算法对`arr`进行排序,并使用`assertArrayEquals`方法来比较排序后的结果`arr`和期望输出数组`expected`是否相等。如果不相等,JUnit将会抛出异常,提示测试失败。 这样,我们就可以使用JUnit框架来实现对快速排序条件覆盖测试了。当然,要想达到更好的测试覆盖率,我们可以针对更多不同的输入情况编写测试用例,以尽可能地覆盖快速排序算法中的各个条件分支。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值