soapui脚本链接mysql_SoapUI中使用Groovy脚本操作数据库

SoapUI中支持使用Groovy脚本,通过Groovy脚本可以操作DB,并可以对DB结果进行校验,从而完成用例的检查。

1:在SoapUI中放置第三方Jar包

连接的数据库的jar包需要放置于bin/ext目录下

原文如下:

If you need to add external libraries to the soapUI classpath for your Groovy scripts (for example jdbc drivers), put these in the bin/ext folder under the soapUI installation, these will be read upon started and added to the soapUI classloader.

2: 在SoapUI的Test Case中新增数据库连接的配置,此配置的Properties文件可以直接从本地文件中导入

3:在SoapUI的Test Case中新建Groovy Script连接数据库

接口如下

def sql = Sql.newInstance(

地址,

用户名,

密码,

驱动 )

实现样例如下:

import groovy.sql.Sql

//通过读取配置文件连接数据库

def DBProperties = testRunner.testCase.getTestStepByName( "DBProperties" );

def sql = Sql.newInstance(DBProperties.getPropertyValue( "connection-url" ),DBProperties.getPropertyValue( "sysdb-user-name" ),

DBProperties.getPropertyValue( "sysdb-password" ),DBProperties.getPropertyValue( "driver-class" ))

4:在SoapUI中通过Groovy脚本操作数据库

1)删除和新建表

//删除表

try {

sql.execute("drop table PERSON")

} catch(Exception e){}

//新建表

sql.execute('''create table PERSON (

id integer not null primary key,

firstname varchar(20),

lastname varchar(20),

location_id integer,

location_name varchar(30)

)''')

2)插入记录

插入记录有两种方式

//向表中插入记录

sql.execute("insert into PERSON (id,firstname,lastname,location_id,location_name) values (1,'gao','shuaihong',1,'hshen') ")

//插入记录另外一种方式

def people = sql.dataSet("PERSON")

people.add( firstname:"James", lastname:"Strachan", id:4, location_id:10, location_name:'London' )

3)查询记录

//选择一行记录

def gaoshuaihong = sql.firstRow("select * from PERSON where id = 1")

log.info(gaoshuaihong.firstname)

//选择多条记录

def allPerson  = sql.rows(" select * from  PERSON")

log.info(allPerson)

log.info(allPerson[0])

sql.eachRow("select * from PERSON"){ row ->

log.info(row.lastname)

}

4)校验结果

assert allPerson[0].lastname== "shuaihong"

转载,做个备忘

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值