当前位置: 首页 > news >正文

【软件测试学习day7】Junit5

Junit 是单元测试框架,本期掌握 Junit5 的基础用法。

1. 注解

首先引入 Junit 依赖:

<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.1</version><scope>test</scope>
</dependency>

1.1 @Test

通过 @Test 这个注解就是代表了,被该注解所修饰的代码为一块测试代码。

    @Testvoid Test01(){System.out.println("第一个单元测试");}


1.2 @Disabled

当我不想让某个测试用例运行时,可使用 @Disabled 注解。

    @Testvoid Test01(){System.out.println("第一个单元测试");}@Disabledvoid Test02(){System.out.println("第二个测试");}


1.3 @BeforeAll和@AfterAll

@BeforeAll@AfterAll 是对一个类进行注解的。

@BeforAll 类似于 SpringBoot 中的前置通知,在所有用例前进行运行。

@AfterAll 类似于 SpringBoot 中的后置通知是在所有测试用例后运行。

    @Testvoid Test01(){System.out.println("第一个单元测试");}@Disabledvoid Test02(){System.out.println("第二个测试");}@BeforeAllstatic void SetUp(){System.out.println("BeforeAll注解");}@AfterAllstatic void TearDown(){System.out.println("AfterAll注解");}

两者之间运用场景,例如 JDBC 链接数据库,配置连接信息以及初始化则用 @BeforAll 注解,关闭连接则使用 @AfterAll 注解。 


1.4 @BeforeEach和@AfterEach

@BeforeEach 在每个测试方法前都执行一次。

@AfterEach 在每个测试方法后都执行一次。

    @Testvoid Test01(){System.out.println("第一个单元测试");}@Testvoid Test02(){System.out.println("第二个测试");}@BeforeAllstatic void SetUp(){System.out.println("BeforeAll注解");}@AfterAllstatic void TearDown(){System.out.println("AfterAll注解");}@BeforeEachvoid BeforeEachTest() {System.out.println("BeforeEach注解");}@AfterEachvoid AfterEachTest() {System.out.println("AfterEach注解");}


 2. 参数化

引入依赖

        <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-params</artifactId><version>5.9.1</version><scope>test</scope></dependency>

2.1 单参数

@ParameterizedTest:参数化测试。

@ValueSource:数据来源,使用时只能表示一种你类型如 ints 代表整型,chars 代表字符型。

    @ParameterizedTest@ValueSource(ints = {1,2,3})void init(int num) {System.out.println(num);}


2.2 多参数

@CsvSource 注解里面的参数可被多个类型识别。

    @ParameterizedTest@CsvSource({"1,2,3"})void Test03(String x,int y,int z){System.out.println(x);System.out.println(y);System.out.println(z);}


2.3 CSV获取参数

@CsvFileSource 注解可访问 .csv 文件。

    @ParameterizedTest@CsvFileSource(resources = "test.csv")void Test02(String name){System.out.println(name);}


3. 测试用例执行顺序

@TestMethodOrder(MethodOrderer.OrderAnnotation.class) 注解声明当前类可被指定顺序。

@Order 注解用来标注执行顺序。

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {@Order(2)@Testvoid A(){System.out.println("AAA");}@Order(1)@Testvoid B(){System.out.println("BBB");}@Testvoid C(){System.out.println("CCC");}
}


4. 断言

if else 语句的替代,可用:

  • Assertions.assertEquals(1,num); - 满足 num 为 1 则通过。
  • Assertions.assertNotEquals(1,num); - 满足 num 不为 1 则通过。
  • Assertions.assertNull(str); - 满足 str 为空则通过。
    @ParameterizedTest@ValueSource(ints = {1})void Test(int num){Assertions.assertEquals(1,num);// 是1则通过Assertions.assertNotEquals(1,num);// 不是1则通过String str =null;Assertions.assertNull(str);// 是null则通过}

5 . 测试套件

有测试类1:

public class JunitTest {@Order(2)@Testvoid A(){System.out.println("AAA");}@Order(1)@Testvoid B(){System.out.println("BBB");}@Testvoid C(){System.out.println("CCC");}
}

测试类2: 

public class JunitTest02 {@Testvoid init(){System.out.println("XXX");}
}

使用 @Sutie 注解 和 @SelectClasses 注解可将上述两类合并执行。

@Suite
@SelectClasses({JunitTest.class,JunitTest02.class})
public class RunSuite {
}

此外,当两个类属于不同包时,也能通过 @SelectPackages 注解来实现合并执行。

@Suite
@SelectPackages(value = {"Test01","Test02"})
public class RunSuite {
}

 


6. 所有依赖

本篇文章的所有依赖。

​
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>my_test</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.1</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-params</artifactId><version>5.9.1</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-params</artifactId><version>5.9.1</version></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.9.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite --><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.9.1</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.9.1</version><scope>test</scope></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>​

相关文章:

  • 抛物线法(二次插值法)
  • C++卡特兰数讲解
  • 避免数据丢失:在存储测试数据之前,要做好Redis持久化
  • 牙科CBCT性能检测模体的革新价值
  • springmvc的入门案例
  • 链表的面试题4之合并有序链表
  • JDBC:java与数据库连接,Maven,MyBatis
  • C++漫步结构与平衡的殿堂:AVL树
  • 基于卫星遥感数据进行农作物长势监测原理简述
  • Spring普通配置类 vs 自动配置类-笔记
  • 2.5 特征值与特征向量
  • 适配国产化,私有化部署的局域网即时通讯工具-BeeWorks
  • C语言if语句的用法(非常详细,通俗易懂)
  • 如果说开启的TIM3定时器有ccr1,ccr2,ccr3,我想要关闭ccr2的PWM输出,怎么通过代码实现
  • 软件测试学习笔记
  • 操作系统 : 线程概念与控制
  • 芯片笔记 - 手册参数注释
  • STM32G070xx将Flash页分块方式存储,固定数据块存储,实现一次擦除多次写入
  • 关系代数操作之复杂扩展操作
  • 【每天学习一点点】使用Python的pathlib模块分割文件路径
  • AI药企英矽智能第三次递表港交所:去年亏损超1700万美元,收入多数来自对外授权
  • 新华每日电讯:给“男性妇科病论文”开一剂复方药
  • 两部门发布山洪灾害气象预警:北京西部、河北西部等局地山洪可能性较大
  • 越秀地产前4个月销售额约411.2亿元,达年度销售目标的34.1%
  • 再有20余篇论文出现“妇科男患者”“前列腺女患者”,如何破除“水论文”灰产链?
  • 昆廷·斯金纳:作为“独立自主”的自由