zoukankan      html  css  js  c++  java
  • Soap UI 数据库脚本(转)

    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" ))

     //def sql = Sql.newInstance("jdbc:mysql://localhost:3306/test","root","XXXXX","com.mysql.jdbc.Driver")

    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"

    另外通过Groovy的Sql还支持操作存储过程.

  • 相关阅读:
    Java基础知识回顾-20(泛型)
    Java基础知识回顾-19(Collect接口,Iterator迭代器与增强for循环)
    Java基础知识回顾-18(Math类,Arrays类和大数据运算)
    Java基础知识回顾-17(基本类型包装类与System类)
    Java基础知识回顾-16(Date,DateFormat和Calendar)
    PSP DAILY软件功能说明书
    第六周PSP
    王者荣耀交流协会第二次Scrum立会
    找bug——加分作业
    第五周PSP
  • 原文地址:https://www.cnblogs.com/pepperok/p/3729985.html
Copyright © 2011-2022 走看看