好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot+TestNG单元测试的实现

背景

由于开发任务进度紧张,接口及基础数据提供不全,即使设计全面的接口测试用例也无法全面有效的进行覆盖测试;且又因为单接口测试用例设计的方向是入参和出参,从入参着手就是参数必填校验、参数类型及参数边界值,再有入参的组合入参,例如一个接口5个参数,3个必填,2个非必填,数据类型有string、int等,还有字符长度限制条件,那么这样的单接口测试用例数设计起来那就有,嗯......数不过来,如果入参个数及参数类型变得多起来,那么这个数量就不可言喻了,所以,这就需要考量测试人员对于接口测试用例设计的方法掌握程度。

接口测试用例,针对入参进行设计:

1、数值类型的参数

等价类划分:取值范围内、取值范围外,这个怎么理解?
如果接口文档有描述这个参数应该取哪几个值或区间,就在这里规定的范围内外取舍
边界值分析:最大最小刚刚好、最大+1、最小-1,这是从取值范围找边界,其中最大最小是数据类型边界
特殊值设计:0或非正数、可能设计小数
遍历:无捷径,穷尽其取值区间,这个一般会被等价类、边界值给过滤掉,不必穷尽;

2、字符串类型

字符串长度
等价类:取值区间内外
边界值:规定范围边界;类型边界
特殊值:这个需要与字符串类型的特殊字符区分,这里指的是0或空字符串、null
字符串内容
特定类型:中英文、大小写、简繁体
特殊字符:emoji表情符、标点符号运算符号、输入法的其他特殊字符

3、少见的数组或链表类型:例如list类型,可能是int[]\也可以是string[],他的设计方法也逃不过上面的几种,这里就不再赘述了。

针对业务逻辑进行设计、针对出参进行设计等这些方面,尤其是业务逻辑一般采取正向用例设计,少量通过入参设计异常场景,出参的设计几乎在入参设计都能得到期望结果。

言归正传!

1、本地使用eclipse作为java开发工具,同时支持python环境编程,所以不想同时使用Pycharm和IDEA;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!-- 首先创建springboot框架的maven项目pom添加如下依赖,ide可以安装插件:spring assistant,一键创建springboot框架的maven项目 -->

 

< parent >

    < groupId >org.springframework.boot</ groupId >

    < artifactId >spring-boot-starter-parent</ artifactId >

    < version >2.4.0</ version > <!-- 2.3.0.RELEASE版本可选 -->

    < relativePath /> <!-- lookup parent from repository -->

</ parent >

 

<!-- springboot框架的主要依赖 -->

< dependency >

    < groupId >org.springframework.boot</ groupId >

    < artifactId >spring-boot-starter-web</ artifactId >

    < version >2.4.0</ version >

</ dependency >

tips:@SpringBootApplication\@RestController\@RequestMapping等等注解

2、导入本地开发环境,eclipse需要外部安装lombok插件,eclipse配置文件eclipse.ini最下添加参数:-javaagent:lombok.jar,附下载地址,除了安装以外,且java团队是否在pom中配置了依赖

?

1

2

3

4

5

6

< dependency >

  < groupId >org.projectlombok</ groupId >

  < artifactId >lombok</ artifactId >

     < version >1.18.12</ version >

  < optional >true</ optional >

</ dependency >

3、我是测试人员,原来的环境中已经安装了jacoco插件,所以在maven项目中也增加了插件<顺带提一下做java测试代码覆盖率的另一个依赖cobertura>:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<!-- 顺带提一下的另一个做代码覆盖率检测的插件 -->

< dependency >

  < groupId >org.codehaus.mojo</ groupId >

  < artifactId >cobertura-maven-plugin</ artifactId >

  < version >2.7</ version >

</ dependency >

 

< plugin >

       < groupId >org.jacoco</ groupId >

       < artifactId >jacoco-maven-plugin</ artifactId >

       < version >0.8.3</ version >

       < configuration >

      <!--指定生成 .exec 文件的存放位置 -->

      < destFile >target/coverage-reports/jacoco-unit.exec</ destFile >

      <!--Jacoco 是根据 .exec 文件生成最终的报告,所以需指定 .exec 的存放路径 -->

             < dataFile >target/coverage-reports/jacoco-unit.exec</ dataFile >

      </ configuration >

      < executions >

          < execution >

   < id >jacoco-initialize</ id >

            < goals >

                < goal >prepare-agent</ goal >

             </ goals >

          </ execution >

          < execution >

   < id >jacoco-site</ id >

            < phase >package</ phase >

             < goals >

              < goal >report</ goal >

             </ goals >

         </ execution >

     </ executions >

</ plugin >

 

<!-- tips:jacoco在maven中执行的命令是:mvn clean jacoco:prepare-agent install jacoco:report -Dmaven.test.failure.ignore=true -->

4、之前使用java辅助jmeter测试的时候,习惯了testng单元测试框架,并且还没在springboot框架上做单元测试,所以pom文件中还需添加如下依赖:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

< dependency >

     < groupId >org.springframework.boot</ groupId >

  < artifactId >spring-boot-starter-test</ artifactId >

  < scope >test</ scope >

  </ dependency >

 

<!-- https://mvnrepository测试数据/artifact/org.testng/testng -->

< dependency >

  < groupId >org.testng</ groupId >

  < artifactId >testng</ artifactId >

  < version >6.9.10</ version >

</ dependency >

<!-- 右键选择Coverage as 执行框架即可,运行完在指定目录或当前控制台查看覆盖率 -->

5、开发习惯使用junit4.x做单元测试,其中也是会使用spring-boot-starter-test这个插件,只是继承的类不一样;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

# junit4.x是如下使用:

 

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

 

@SpringBootTest

@RunWith (SpringRunner. class )

public class BaseTest  {

}

 

# 而换成testng就是如下使用:

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;

 

@SpringBootTest

public class BaseTest extends AbstractTestNGSpringContextTests {

}

 

# 同样是使用 @SpringBootTest 注解,但是它启动sprintboot服务不一样,testng必须继承AbstractTestNGSpringContextTests使用;

# 扩展:还可以是继承AbstractTransactionalTestNGSpringContextTests类,二选一;

# @SpringBootTest (classes={是你的SpringBoot启动app类}),例如: @SpringBootTest (classes=UserApplication. class )

6、至此,就可以在eclipse上按Ctrl+1,将junit框架的测试用例转换成testng框架愉快的测试了:Convert to TestNG(Annotations);
7、拓展:添加swagger依赖,生成接口文档;lombok依赖支持声明字段的getter和setter方法,同样有集成Log日志等工具。

剩下的就不要看我这边文章了,要看你自己的发挥了,单元测试案例如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package   com.text;

 

import xxx.xxx.xxx

 

public TestSampler extends BaseTest{

    

     @BeforeClass

     void bf(){

         // 测试写测试的前置条件,那些只需要执行一次的数据

     }

     @Test

     void test_add(){

         // 这里写需要测试的代码

     }

    

//    。。。

}

到此这篇关于SpringBoot+TestNG单元测试的实现的文章就介绍到这了,更多相关SpringBoot TestNG单元测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/6983169214748033054

查看更多关于SpringBoot+TestNG单元测试的实现的详细内容...

  阅读:26次