使用Beanshell Assertion实现Jmeter测试结果和数据库比对

上一篇 / 下一篇  2010-08-12 20:03:02 / 个人分类:JMeter

    在使用jemeter的过程中,有时候希望能够将jmeter的响应结果直接和数据库进行比对,可以使用Beanshell Assertion实现Jmeter测试结果和数据库比对,参考如下


import java.sql.*;
import java.util.*;
import java.lang.*;
import org.apache.regexp.*;

String drive = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://19x.16x.xx.xx:3306/";
String dbName = "Name";
String user = "user";
String pass = "pass";

String Myvar = vars.get("Param");

String query = "SELECT MyDescription From Mytable Where Mycriteria = '" + Myvar + "'";
Connection Mycon = null;
Statement Mystmt = null;
ResultSet Myrset = null;

String[] Comp = new String[24];
RE rex;
boolean found;

String Mypattern;
byte[ ] Myarr = (byte[ ])ResponseData;
String Myresp = new String(Myarr);

Mycon = DriverManager.getConnection(url+dbName, user, pass);
Mystmt = Mycon.createStatement();
Myrset = Mystmt.executeQuery(query);

int a=1; int b=0;
while (Myrset.next())
{
Comp[b] = Myrset.getString(1);
print(" "+ (a) +" " + Comp[b]);

Mypattern = Comp[b];
rex = new RE(Mypattern);
found = rex.match(Myresp);
print(Mypattern + (found ? " DOES MATCH " : " DOESN'T MATCH ") + "Response Returned\n");

if (found==false) 
{ Failure = true;
FailureMessage = "The Values in the Response do not match the expected data."; }

a=a+1; b=b+1;
}

Myrset.close();
Mystmt.close();


    以上代码可以根据实际情况修改,可以实现数据库验证
     

TAG: Beanshell 数据库 Jmeter JMeter 验证 Assertion

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar