Implementation and Design Jmeter interface test database assertion

Interface test most of them use as the basis of the value of the interface in response to verification of the interface, but the result of the determination in this interface to interact with the database has not judge the correctness of the interface, as an interface jmeter herein will test tools to the interface database assertion

I. Overview of the general idea

    1. jmeter interface request initiated

    2. json extractor acquires content interface in response to key results

    3. jmeter initiate a database operation, the database values ​​obtained

    4. Comparative BeanShell use assertions, and implement the interface in response to the content of the database content

Two. Jmeter initiate interface requests

    To search interface, for example, business requirements are: search by keyword merchandise. Interface information is as follows:

 Interface information:

Input parameters:

Return result:

   Add jmeter http request in accordance with the interface information above, below

III. Adding an interface to request extractor json

     Add json extractor to obtain all of the interface in response name, and the name value to be found in the database one comparisons

     Add json Extractor 3.1

Select search requests - Add - postprocessor extractor --json    

 3.2 extractor provided json

The parameters explained:

1. Names of created variables: variables representing json extractor acquired from the response to the stored content, just as long as no conflict can be defined, since a meaningful name suggestions, do not write the name of abc meaningless

2. JSON Path Expressions: json extraction expression that requires matching content from the response in accordance with this rule, all the figures showing the expression match the name field value corresponding to the response

3. Match No. (0 for Random): matching result storage policy to a variable, the default is nothing to write 0

10 represents a random variable stored, for example, we matched the values ​​of a plurality of name fields, which will then randomly assigned to a variable value specified Names of created variables

2 -1表示使用数字后缀标识每一个匹配的结果,比如我们匹配了多个name字段的值,那么最终每一个结果都会对应一个变量,变量名称则是name_1、name_2、name_3、name_4 ....

3 X表示把第几个匹配到的值赋值给Names of created variables指定的变量,比如写个2,那么就会把匹配到的第2的name字段的值赋值给name

4. Compute concatenation var:表示如果匹配到多个值,可以将多个值存储在一个变量中,变量名为Names of created variables指定的变量名称加_ALL,比如当前图中就是name_ALL,存储的多个值以,分割

5.  Default Values:表示如果json表达式没有匹配到任何值,那么变量的默认值是啥

四. 配置数据库发起数据库请求

    以mysql数据库为例

   4.1  配置数据库连接元件

配置数据库所需要的jar包,下载mysql-connector-java.5.1.46.jar,可以直接在maven上进行下载,下载地址https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.46

下载好以后放入jmeter目录下的lib/ext目录下,然后重启jmeter

配置jdbc元件,选中线程组--添加--配置元件--JDBC  Connection  Configuration

核心字段含义

Variable Name for created pool:表示该连接配置的名称,这个名称将会在后续的jdbc请求中使用

Dabase URL:表示数据库实例的连接地址

JDBC Driver class:表示连接驱动类

Username:表示连接数据库实例的用户名

Password:表示连接数据库实例的密码

4.2 数据库请求,请注意该请求需要放在查询接口请求之前

    选中线程组--添加--取样器--JDBC Request

 

核心字段含义

Variable Name of Pool declared in JDBC Connect Configuration:表示要连接的数据库配置名称,与我们上面配置的一致

Query:填写sql语句,这里我们是查询商品表,通过name字段模糊查询,并且只返回了name字段

Result  variable  name:表示从数据库中查到的内容存到哪个变量里,这里写变量名称

五. 脚本调试

   5.1  添加调试取样器,用来查看各个变量的值是否正确

    选中线程组--添加--取样器--调试取样器

   5.2  添加察看结果树,用来查看请求结果

    选中线程组--添加--监听器--察看结果树

 5.3  执行测试,查看各个变量的值是否正确

dbname=[{name=天喔 西梅454g}, {name=天喔 盐津桃肉170g/瓶}, {name=天喔很牛牛肉粒(沙爹)150g/袋}, {name=天喔 Q猪香辣小香肠100g/袋}]        数组中存着键值对

name_ALL=天喔 西梅454g,天喔 盐津桃肉170g/瓶,天喔很牛牛肉粒(沙爹)150g/袋,天喔 Q猪香辣小香肠100g/袋       逗号分隔的字符串

六. 使用Beanshell 断言完成接口结果和数据库结果的校验

   选中搜索请求--添加--断言--BeanShell 断言

执行整个脚本,执行通过,脚本全貌如下


作  者:Testfan 沙陌老师

出  处:微信公众号:自动化软件测试平台

版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出文章链接

Guess you like

Origin www.cnblogs.com/testfan2019/p/12124093.html