网站的建设教程,网站上做网上支付功能,做百度推广网站被攻击,做杂志的网站注:本篇文章讲解的是junit5 目录
Juint是什么
Juint需要导入的依赖
Juint常用注解 Junit执行顺序 参数化
断言
测试套件 Juint是什么
Juint 是 Java 的一个单元测试框架. 也是回归测试框架. 使用 Junit 能让我们快速的完成单元测试。 注意#xff1a;Junit 测试也是程序… 注:本篇文章讲解的是junit5 目录
Juint是什么
Juint需要导入的依赖
Juint常用注解 Junit执行顺序 参数化
断言
测试套件 Juint是什么
Juint 是 Java 的一个单元测试框架. 也是回归测试框架. 使用 Junit 能让我们快速的完成单元测试。 注意Junit 测试也是程序员测试即白盒测试它需要程序员知道被测试的代码如何完成功能以及完成什么样的功能 Juint需要导入的依赖 本篇文章junit代码需要的依赖, 这里给大家贴出来 !--junit依赖--
!-- api--!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-api/artifactIdversion5.9.2/version/dependency!-- 参数化需要的--!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.9.2/version/dependency!-- 测试套件--!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite --dependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite/artifactIdversion1.9.2/version/dependency!-- 测试套件--!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.9.2/version/dependencyJuint常用注解
Test: 在类上注解,i表名当前类是一个测试用例
BeforeAll: 一个类中,所有方法执行之前会执行一次
AfterAll: 一个类中,所有方法执行结束会执行一次
BeforeEach: 每个方法执行执行都会执行一次这个方法
AfterEach: 每个方法执行结束之后都会执行一次这个方法 BeforeAll AfterAll 与 BeforeEach AfterEach 区别在于 BeforeAll AfterAll 修饰的方法是static void的,也就是说是属于类的,在一个类中一共只会执行一次 BeforeEach AfterEach 修饰的方法是void的,会在类中的每个方法执行 之前/之后 都会执行一次 还有一个常用方法是:
Disabled: 忽略当前测试用例,不执行
public class junit1 {Testvoid test1(){WebDriver webDrivernew ChromeDriver();webDriver.get(https://www.baidu.com/);}Disabled //忽略当前测试用例,不执行Testvoid test2(){System.out.println(测试用例2);}Testvoid test10(){System.out.println(测试用例10);}BeforeAllstatic void test3(){System.out.println(类前置);}AfterAllstatic void test4(){System.out.println(类后置);}BeforeEachvoid test5(){System.out.println(方法前置);}AfterEachvoid test6(){System.out.println(方法后置);}}
通过这个执行结果也可以清晰的看到执行的顺序 以及执行的次数 Junit执行顺序
那么如果方法多了,哪个方法优先执行呢?
默认顺序由方法名hashcode值来决定如果hash值大小一致则按名字的字典顺序确定
由于hashcode的生成和操作系统相关(以native修饰所以对于不同操作系统可能会出现不一样的执行顺序在某一操作系统上多次执行的顺序不变.
我们也可以进行设置: 设置有两种方式:按照指定顺序执行 / 按照随机顺序执行,不可以同时存在 //声明按照指定顺序进行执行
TestMethodOrder(MethodOrderer.OrderAnnotation.class)
//随机顺序执行
//TestInstance(MethodOrderer.Random.class)//不可以同时存在
public class junit {Order(2)Testvoid test1(){WebDriver webDrivernew ChromeDriver();webDriver.get(https://www.baidu.com/);}Disabled //忽略当前测试用例,不执行Testvoid test2(){System.out.println(测试用例2);}Order(5)Testvoid test10(){System.out.println(测试用例10);}Order(4)Testvoid test11(){System.out.println(测试用例11);}Order(8)Testvoid test12(){System.out.println(测试用例12);}Order(1)Testvoid test13(){System.out.println(测试用例13);}BeforeAllstatic void test3(){System.out.println(类前置);}AfterAllstatic void test4(){System.out.println(类后置);}BeforeEachvoid test5(){System.out.println(方法前置);}AfterEachvoid test6(){System.out.println(方法后置);}}执行结果就是按照我们设置的顺序进行执行的, 设置的数字顺序可以不连续, 可以为负数,
他会按照从小到大的顺序进行执行: 参数化
单参数
多参数: 1. cvs 传参 2. 方法传参
//参数传递
public class junit2 {public static StreamArguments m1() {return Stream.of(Arguments.arguments(1,zhang,2,lissss,3,wang,4,lisi));}//单参数// TestParameterizedTest//不可以和Test同时存在ValueSource(strings {1,2,3})void test10(String str){System.out.println(str);}//多参数 Csv1ParameterizedTestCsvSource({1,2,s,3})void test(String s,int n){System.out.println(s n);}//多参数 Csv2ParameterizedTestCsvFileSource(resourcestest1.csv)void test2(int id,String name){System.out.println(id name);}//使用方法进行多参数传递ParameterizedTestMethodSource(m1)void test3(String id,String name){System.out.println(id name);}}断言
//断言ParameterizedTestValueSource(strings{1})void test4(String s){//断言相等Assertions.assertEquals(s,1);//断言不相等Assertions.assertNotEquals(s,2);//断言为空Assertions.assertNull(s);//断言不为空Assertions.assertNotNull(s);}
测试套件
Junit是怎么进行管理测试用例的呢?
就是通过测试套件.
1.以类为单位进行执行
Suite
SelectClasses({junit.class,junit2.class})
public class suit1 {
}2.以包为单位进行执行
Suite
SelectPackages({org.example.junit1,org.example.junit2})
public class suit1 {}