测试|Junit相关内容
文章目录
- 测试|Junit相关内容
- 0.Junit说明
- 1.Junit注解
- @Test
- @Disabled
- @BeforeAll和@AfterAll
- @BeforeEach和@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
表示当前方法是一个测试用例。
测试用例跑过了:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QN8T6FJ8-1690872547538)(F:\typora插图\image-20230801085852858.png)]](https://img-blog.csdnimg.cn/28e61c9fda30439aae1d6681ee92ee34.png)
测试用例跑不过:(只跑一个,跑全部的)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4aJA9SZn-1690872547539)(F:\typora插图\image-20230801090615074.png)]](https://img-blog.csdnimg.cn/a954c8c7a9214ec6829d520df0f75a30.png)
@Disabled
表示忽略当前测试用例,跳过当前测试用例
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xrhll6DA-1690872547540)(F:\typora插图\image-20230801091522574.png)]](https://img-blog.csdnimg.cn/725f6d6d14cb4fc8bb25e23abf5d9a38.png)
@BeforeAll和@AfterAll
含义:@BeforeAll:所有测试用例跑之前跑的,@AfterAll:所有测试用例跑完后跑的
说明:
- 这两个注解下的方法需要是静态的
- 一般初始化放在BeforeAll所在方法中,关闭资源放在AfterAll中
- 如果做UI自动化,通常情况下,创建驱动,打开网页,放到BeforeAll中;关闭浏览器放到AfterAll中
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZhD92a7H-1690872547540)(F:\typora插图\image-20230801092207325.png)]](https://img-blog.csdnimg.cn/dedc8ef5d0f148d49a80b6006130aae2.png)
@BeforeEach和@AfterEach
@BeforeEach&@AfterEach 和 @BeforeAll和@AfterAll区别:
- @BeforeAll是在所有测试用例之前跑一次相应的方法
- @BeforeEach是在每个测试用例之前跑一次相应的方法
- @AfterEach 是在每个测试用例之后跑一次相应的方法
- @AfterAll是在所有测试用例之后跑一次相应的方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LsjcYNCc-1690872547541)(F:\typora插图\image-20230801092856948.png)]](https://img-blog.csdnimg.cn/cc5fc93c41264c978f22a108a0518753.png)
2.Junit参数化
不进行参数注册,就往注解下的方法中传参,会报错,这个时候就需要引入相关依赖,进行参数注册
<!-- 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>
<scope>test</scope>
</dependency>
注意:这里的scope还是需要注释掉
其中@ParameterizedTest表明当前方法为参数化测试方法
单参数
这里的单不是单个,而是单种,只不过这一种参数下可以有一个参数也可以有多个参数
使用方法:在方法上加上两个注解:@ParameterizedTest,@ValueSource(类型名s={xxxxxx})
传参与入参
@ParameterizedTest
@ValueSource(strings={"1","2","3"})
void test05(String num){
System.out.println(num);
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z4NCLEmK-1690872547542)(F:\typora插图\image-20230801094303684.png)]](https://img-blog.csdnimg.cn/61605ec7779b4b74b451ae388407f61f.png)
多参数(多种/多组)
其实我觉得这里如果是多个参数,对象包装一下会比较方便即对象单参数获取,如果是多个对象就是对象数组。
CSV获取参数(支持多种)
@CsvSource注释的值是一个字符串数组,每个字符串表示一组参数
每个参数对应一列
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5sX2G93I-1690872547542)(F:\typora插图\image-20230801103650424.png)]](https://img-blog.csdnimg.cn/914f8ffca8d748c58b34abfc485da0b3.png)
入参的个数大于形参的情况:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YWF0x5oT-1690872547543)(F:\typora插图\image-20230801103908859.png)]](https://img-blog.csdnimg.cn/a6604022a52846c1888118b5f39a8f63.png)
空字符串的传递:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pWukbtyN-1690872547543)(F:\typora插图\image-20230801104056120.png)]](https://img-blog.csdnimg.cn/9bbe82171fb14e56b706510479197992.png)
不同类型的一组参数:(主要看第三种情况)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xIn0Jq3d-1690872547544)(F:\typora插图\image-20230801111457162.png)]](https://img-blog.csdnimg.cn/afb0adc4f794457095b6b06c2a777b93.png)
CSV文件获取参数(支持多种多组)
当存在多种参数的时候,使用ValueSource不再方便,使用csv文件更加方便。
1.类型的相同的多组
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8OIS4ROt-1690872547544)(F:\typora插图\image-20230801100723844.png)]](https://img-blog.csdnimg.cn/b61808ec9bfc48768aafb51ca6ef3e7d.png)
2.类型不相等的多组
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cw7FaqjQ-1690872547544)(F:\typora插图\image-20230801110745603.png)]](https://img-blog.csdnimg.cn/486a9b7b5f6b49f8a20a31a082b30a72.png)
方法获取参数(支持多种多组)
有时参数不能直接生成,我们就需要使用方法获取参数的方式
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rnM9EIqE-1690872547545)(F:\typora插图\image-20230801101608162.png)]](https://img-blog.csdnimg.cn/f35298f1463b47bd80aeeca5c2df2f98.png)
补充:

虽然不能完全理解,也不知道到底是哪些类实现了这些接口,但是从这些源码大概能知道单参数的时候起码是数组,一定程度上可以帮助理解。
3.Junit测试用例执行顺序
public class JunitTest01 {
@Test
void testB(){
System.out.println("testB的测试用例");
}
@Test
void test01(){
System.out.println("test01的测试用例");
}
@Test
void test02(){
System.out.println("test02的测试用例");
}
@Test
void testA(){
System.out.println("testA的测试用例");
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8pFPffRq-1690872547545)(F:\typora插图\image-20230801112340280.png)]](https://img-blog.csdnimg.cn/47b98ee5efab40fca3f1b9b22c94294c.png)
为什么执行顺序是固定的?
因为Junit有自己执行顺序的算法,如果想要指定执行顺序需要特殊处理
手动指定执行顺序(OrderAnnotation)
@TestMethodOrder(MethodOrderer .OrderAnnotation.class)
public class JunitTest01 {
@Order(1)
@Test
void testB(){
System.out.println("testB的测试用例");
}
@Order(2)
@Test
void test01(){
System.out.println("test01的测试用例");
}
@Order(3)
@Test
void test02(){
System.out.println("test02的测试用例");
}
@Order(4)
@Test
void testA(){
System.out.println("testA的测试用例");
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ektrtvMx-1690872547546)(F:\typora插图\image-20230801112819652.png)]](https://img-blog.csdnimg.cn/20b0cdf21cd244639e1312f34aa383d4.png)
随机执行顺序(Random)
@TestMethodOrder(MethodOrderer.Random.class)
//@TestMethodOrder(MethodOrderer .OrderAnnotation.class)
public class JunitTest01 {
// @Order(1)
@Test
void testB(){
System.out.println("testB的测试用例");
}
// @Order(2)
@Test
void test01(){
System.out.println("test01的测试用例");
}
// @Order(3)
@Test
void test02(){
System.out.println("test02的测试用例");
}
// @Order(4)
@Test
void testA(){
System.out.println("testA的测试用例");
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j9r3ZhcM-1690872547546)(F:\typora插图\image-20230801113328137.png)]](https://img-blog.csdnimg.cn/ced9e20907ed4a7bbeef66f0d8aa1352.png)
4.断言
测试用例需要有校验,需要把执行结果和预期结果进行对比。使用assert关键字。
断言相等和断言不相等
断言相等:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6WXYibkj-1690872547547)(F:\typora插图\image-20230801114756634.png)]](https://img-blog.csdnimg.cn/81c3be8d45a545489f9f30a895ce9e78.png)
断言不相等:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1NnlIvcc-1690872547548)(F:\typora插图\image-20230801114510217.png)]](https://img-blog.csdnimg.cn/b13b770e966b4e099ecd27af4f24a5d0.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xMHFDp2p-1690872547548)(F:\typora插图\image-20230801114722836.png)]](https://img-blog.csdnimg.cn/9df75f07e36e48bc9bf5b5f2eb324cce.png)
当断言数组时,可以使用 assertArrayEquals 方法来比较两个数组是否相等。以下是一个示例:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class ArrayAssertionTest {
@Test
public void testArrayEquals() {
int[] expected = {1, 2, 3, 4};
int[] actual = {1, 2, 3, 4};
assertArrayEquals(expected, actual);
}
}
在上述示例中,assertArrayEquals 方法将会比较两个数组 expected 和 actual 是否相等。如果数组内容相同,则断言通过,否则断言失败。
断言为空和断言不为空
期待是不为空和期待是空:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XZiq0Xmo-1690872547549)(F:\typora插图\image-20230801115111257.png)]](https://img-blog.csdnimg.cn/207449c25cd54c1a92c6c99ce5d94c4b.png)
5.Junit测试套件
测试套件的相关操作需要引入相关依赖,注意,因为这里是在main文件夹下而不是在test文件夹下,所以记得把scope这个标签注释掉
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.9.1</version>
<!-- <scope>test</scope>-->
</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>
使用方法有两种,一种是通过class,另外一种是通过包。
对应的注解分别是@SelectClasses,@SelectPackage
@Suite
//通过class测试用例运行
@SelectClasses({JunitTest.class,JunitTest01.class})
//通过包
//@SelectPackages(value = {"package01","package02"})
public class RunSuite {
}
public class Test01 {
@Test
public void test01(){
System.out.println("package01-->test01");
}
}
public class Test01 {
@Test
public void test01(){
System.out.println("package02-->test01");
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d2mj1eiv-1690872547549)(F:\typora插图\image-20230801143349213.png)]](https://img-blog.csdnimg.cn/ad12a0bd9a904f56aacf49a5cbe84088.png)
常见问题
No tests were found
原因1:这是@Test注解方法的权限问题,类中方法默认权限是default,
对于@Test注解的方法,我们可以选择写public,也可以选择不写。
如果写成private,当前方法就不能被识别出是一个测试用例了。
解决办法:改成public,或去掉private
原因2:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PTPtCo8o-1690872547549)(F:\typora插图\image-20230801121136525.png)]](https://img-blog.csdnimg.cn/15747958e4e746b783477dcb638226cf.png)
同样的标签需要导两次…
<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>
<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>
原因3:方法不能有返回值



















