如何加强企业网站建设 论文,深圳物流公司招聘信息,公众号开发框架,常州外贸集团 网站建设测试|Junit相关内容 文章目录 测试|Junit相关内容0.Junit说明1.Junit注解TestDisabledBeforeAll和AfterAllBeforeEach和AfterEach 2.Junit参数化单参数多参数#xff08;多种/多组#xff09;CSV获取参数#xff08;支持多种#xff09;CSV文件获取参数#xff08;支持多种…测试|Junit相关内容 文章目录 测试|Junit相关内容0.Junit说明1.Junit注解TestDisabledBeforeAll和AfterAllBeforeEach和AfterEach 2.Junit参数化单参数多参数多种/多组CSV获取参数支持多种CSV文件获取参数支持多种多组方法获取参数支持多种多组 补充 3.Junit测试用例执行顺序手动指定执行顺序OrderAnnotation随机执行顺序Random 4.断言断言相等和断言不相等断言为空和断言不为空 5.Junit测试套件常见问题No tests were found 0.Junit说明
Junit是针对Java进行单元测试的一种框架。
注这里使用的版本是Junit5,前边写的Selenium是Selenium5
1.Junit注解
Test
表示当前方法是一个测试用例。
测试用例跑过了 测试用例跑不过只跑一个跑全部的 Disabled
表示忽略当前测试用例跳过当前测试用例 BeforeAll和AfterAll
含义BeforeAll:所有测试用例跑之前跑的AfterAll:所有测试用例跑完后跑的
说明
这两个注解下的方法需要是静态的一般初始化放在BeforeAll所在方法中关闭资源放在AfterAll中如果做UI自动化通常情况下创建驱动打开网页放到BeforeAll中关闭浏览器放到AfterAll中 BeforeEach和AfterEach
BeforeEachAfterEach 和 BeforeAll和AfterAll区别
BeforeAll是在所有测试用例之前跑一次相应的方法BeforeEach是在每个测试用例之前跑一次相应的方法AfterEach 是在每个测试用例之后跑一次相应的方法AfterAll是在所有测试用例之后跑一次相应的方法 2.Junit参数化
不进行参数注册就往注解下的方法中传参会报错这个时候就需要引入相关依赖进行参数注册
!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --
dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.9.1/versionscopetest/scope
/dependency
注意这里的scope还是需要注释掉
其中ParameterizedTest表明当前方法为参数化测试方法
单参数
这里的单不是单个而是单种只不过这一种参数下可以有一个参数也可以有多个参数
使用方法在方法上加上两个注解ParameterizedTest,ValueSource(类型名s{xxxxxx})
传参与入参
ParameterizedTest
ValueSource(strings{1,2,3})
void test05(String num){System.out.println(num);
}多参数多种/多组
其实我觉得这里如果是多个参数对象包装一下会比较方便即对象单参数获取如果是多个对象就是对象数组。
CSV获取参数支持多种
CsvSource注释的值是一个字符串数组每个字符串表示一组参数
每个参数对应一列 入参的个数大于形参的情况 空字符串的传递 不同类型的一组参数主要看第三种情况 CSV文件获取参数支持多种多组
当存在多种参数的时候使用ValueSource不再方便使用csv文件更加方便。
1.类型的相同的多组 2.类型不相等的多组 方法获取参数支持多种多组
有时参数不能直接生成我们就需要使用方法获取参数的方式 补充 虽然不能完全理解也不知道到底是哪些类实现了这些接口但是从这些源码大概能知道单参数的时候起码是数组一定程度上可以帮助理解。
3.Junit测试用例执行顺序
public class JunitTest01 {Testvoid testB(){System.out.println(testB的测试用例);}Testvoid test01(){System.out.println(test01的测试用例);}Testvoid test02(){System.out.println(test02的测试用例);}Testvoid testA(){System.out.println(testA的测试用例);}
}为什么执行顺序是固定的
因为Junit有自己执行顺序的算法如果想要指定执行顺序需要特殊处理
手动指定执行顺序OrderAnnotation
TestMethodOrder(MethodOrderer .OrderAnnotation.class)
public class JunitTest01 {Order(1)Testvoid testB(){System.out.println(testB的测试用例);}Order(2)Testvoid test01(){System.out.println(test01的测试用例);}Order(3)Testvoid test02(){System.out.println(test02的测试用例);}Order(4)Testvoid testA(){System.out.println(testA的测试用例);}
}随机执行顺序Random
TestMethodOrder(MethodOrderer.Random.class)
//TestMethodOrder(MethodOrderer .OrderAnnotation.class)
public class JunitTest01 {
// Order(1)Testvoid testB(){System.out.println(testB的测试用例);}
// Order(2)Testvoid test01(){System.out.println(test01的测试用例);}
// Order(3)Testvoid test02(){System.out.println(test02的测试用例);}
// Order(4)Testvoid testA(){System.out.println(testA的测试用例);}
}4.断言
测试用例需要有校验需要把执行结果和预期结果进行对比。使用assert关键字。
断言相等和断言不相等
断言相等 断言不相等 当断言数组时可以使用 assertArrayEquals 方法来比较两个数组是否相等。以下是一个示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;public class ArrayAssertionTest {Testpublic void testArrayEquals() {int[] expected {1, 2, 3, 4};int[] actual {1, 2, 3, 4};assertArrayEquals(expected, actual);}
}在上述示例中assertArrayEquals 方法将会比较两个数组 expected 和 actual 是否相等。如果数组内容相同则断言通过否则断言失败。
断言为空和断言不为空
期待是不为空和期待是空 5.Junit测试套件
测试套件的相关操作需要引入相关依赖注意因为这里是在main文件夹下而不是在test文件夹下所以记得把scope这个标签注释掉
dependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite-api/artifactIdversion1.9.1/version
!-- scopetest/scope--/dependency!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.9.1/version
!-- scopetest/scope--/dependency使用方法有两种一种是通过class另外一种是通过包。
对应的注解分别是SelectClassesSelectPackage
Suite
//通过class测试用例运行
SelectClasses({JunitTest.class,JunitTest01.class})//通过包
//SelectPackages(value {package01,package02})
public class RunSuite {}public class Test01 {Testpublic void test01(){System.out.println(package01--test01);}
}public class Test01 {Testpublic void test01(){System.out.println(package02--test01);}
}常见问题
No tests were found
原因1这是Test注解方法的权限问题,类中方法默认权限是default
对于Test注解的方法我们可以选择写public也可以选择不写。
如果写成private当前方法就不能被识别出是一个测试用例了。
解决办法:改成public或去掉private 原因2
同样的标签需要导两次…
dependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-java/artifactIdversion3.141.59/version/dependency!-- https://mvnrepository.com/artifact/commons-io/commons-io --dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.11.0/version/dependency!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-api/artifactIdversion5.9.1/version/dependencydependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.9.1/version/dependency!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.9.1/version/dependencydependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite/artifactIdversion1.9.1/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite --dependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite/artifactIdversion1.9.1/version/dependency!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.9.1/versionscopetest/scope/dependencyversion1.9.1/version/dependency!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.9.1/versionscopetest/scope/dependency原因3方法不能有返回值