[Turn] jmeter achieve matching verification data DB data interface

Original link: http://www.51testing.com/html/58/n-3719058.html

Introduction: According to the parameters of the interface in conjunction with the verification data DB, efforts to make more accurate check ~
JMeter comes widget JDBC Request Sampler
The Sampler may provide a database send a request jdbc (sql statement), and acquires the returned data database operation. It often requires an original configuration and JDBC Connection Configuration (configuration-related database connection properties, such as connection name, password, etc.) used together.
setp1: Preparations
1. As used herein, sqlserver database testing , database user named sa (your user name), user name, password ********* (your password)
2. examples of the database: The --- TMSS_DB2, library name: seg6654, table name: configuration, table data as follows:

 

 

3, add the required driver jar package
to use a different database, we need to introduce a different jar package.
Method 1: Copy the jar packets directly to the next lib \ ext directory jmeter the
mysql database : jar without introducing other database-driven package.
SQL  Server  Database : Download sqljdbc42.jar into jmeter root lib directory
oracle database : oracle installation directory data following \ product \ 10.2.0 \ db_1 \ jdbc \ lib \ ojdbc14.jar into the root directory jmeter lib directory
embodiment 2: the Test Plan
if we do not use the copy jar to the embodiment lib directories jmeter, we can also use jmeter the Test Plan introduced corresponding jar package, such as the introduction sqlserver data following jar package step2: configuration JDBC connection Configuration important parameters: Variable the name: the name of the database connection pool, we can have multiple jdbc connection configuration, each of which can play a different name, you can use to select the appropriate connection pool by this name in jdbc request. Database URL: database url, jdbc: mysql: // host machine name or ip: mysql listening port number / name of the database, such as: jdbc: mysql: // localhost: 3306 / the Test JDBC Driver class: JDBC Driver






username: database login user name
passwrod: database login passwords
of different databases concrete fill mode, you can refer to the following table: fill roughly as follows: step3: Configure JDBC Request important parameters Description: Variable the Name: database connection pool name, the need to maintain the Variable name Bound Pool name JDBC Connection Configuration unanimous Query: fill sql statement is not the end Do not add ";" the parameter valus: parameter value parameter types: parameter type, refer to: Javadoc for  the Java .sql.Types Variable names: save sql statement returns the result variable name result variable name: create an object variable, to save all the results returned by the query timeout: query timeout handle result set: how to define the results are returned by the callable statements statement of the results: the implementation here, we as it has been the investigation data from the database, but the specifics of how to remove the data we need to do, obviously, if we sql query returns only one data, the above approach has to meet our needs But if we just public_adresskey data, how to get it? This time we need JDBC Request parameters used in the above-said


















起重使用Variable names和Result variable name均可以,但取值时key不一样,比如我们现在就如上面,加一个Debug sampler
Variable names 对应返回结果的key为count_1,Result variable name对应返回结果的key就为result。

这样我们值的值既然赋值给一个key了,那么我们如何获取result中的public_adress呢?
由于结果为一个json数据格式,因此先获取json数据,之后通过jsonpath读取
我们增加一个Beanshell PostProcessor,通过vars.getObject(variable).get(index).get(field)函数获取
重要的参数说明:
Variable :为Variable names或Result variable name设置的变量名称
index:表示在查询结果的位置(由于我前面sql语句只限制查询content,因此为0)
field:为查询字段名称

获取到此值后跟接口出参进行一致性校验(因为前面已经vars.put进去了~现直接使用即可)

到此结束~
另外讲下JDBC其它方法的使用
step4:JDBC Request 参数化
方法(一)、定义变量,在sql query中使用变量:

1、在User Defined Variables或者TestPlan或者CSV Data Set Config 中定义一个变量:
2、sql query 中使用${变量名}的方式引用:

方法(二)、在sql query中使用”?“作为占位符,并传递参数值和参数类型,如下图所示:
1、传递的参数值是常量,如图传递2个变量,多个变量使用” , “ 分隔。这里假如你有数据是int类型的,也要在Parameter types 那里标示为varchar类型,否则无法运行。

2、传递的参数值是变量,使用${变量名}的方式

step5:Variables names 参数使用方法:
jmeter官网给的解释是:如果给这个参数设置了值,它会保存sql语句返回的数据和返回数据的总行数。假如,sql语句返回2行,3列,且variables names设置为A,,C,那么如下变量会被设置为:
A_#=2 (总行数)
A_1=第1列, 第1行
A_2=第1列, 第2行
C_#=2 (总行数)
C_1=第3列, 第1行
C_2=第3列, 第2行
如果返回结果为0,那么A_#和C_#会被设置为0,其它变量不会设置值。
如果第一次返回6行数据,第二次只返回3行数据,那么第一次那多的3行数据变量会被清除。
可以使用${A_#}、${A_1}...来获取相应的值
示例:
我们还是用上面的数据库,把所有数据查出来,test表有有3个字段,5条 记录(忘记了的可以回到第一步那里查看)
1、添加一个jdbc request名为”参数4“,添加一个”Debug Sampler“用来查看输出的结果,设置 variables name为column1,column2,column3:

2、执行结果:

解析:
column1代表第一列所有的数据,column1_#可以获取到第一列的行数
column1_n:获得第一列第n行的数据。
column2和column3的功能类似, 假如我们只需要第一列和第三列的数据,可以写成column1,,column3,中间的","不可以省略。
step6:Result variable name 参数使用方法:
如果给这个参数设置值,它会创建一个对象变量,保存所有返回的结果,获取具体值的方法:columnValue = vars.getObject("resultObject").get(0).get("Column Name")

执行结果:
 
 

Guess you like

Origin www.cnblogs.com/87060524test/p/11596604.html