robot framework学习笔记之七—连接mysql数据库

 

     1.安装Database-Library

     输入命令:pip install robotframework_databaselibrary

     2.添加Database的Library

    3.实例

*** Test Cases ***
查询单条数据 
    ${data}    create dictionary    shopCode=xxx    coordinate=
    ${response}    Rest.Get    /api/shop/current.json    ${data}    form    ${hosts["weidian"]}
    Connect To Database Using Custom Params    pymysql    host='115.29.xx.xxx', port=3306, user='xx', passwd='xxx', db='test', charset= 'utf8'
    ${name}    query    select nickname from authshop where code='xxx'
    log    ${response["data"]["shop"]["shopName"]}
    should be equal    ${response['data']['shop']['shopName']}    ${name[0][0]}
    log    ${name[0][0]}

查询多条数据


数据库中存在
    #Check If Exists In Database
    ${data}    create dictionary    shopCode=xxx    coordinate=
    ${response}    Rest.Get    /api/shop/current.json    ${data}    form    ${hosts["weidian"]}
    Connect To Database Using Custom Params    pymysql    host='115.29.xx.xx', port=3306, user='test', passwd='xxx', db='test', charset= 'utf8'
    Check If Exists In Database    select nickname from authshop where code='xxx'

数据库中不存在
    #Check If Not Exists In Database
    ${data}    create dictionary    shopCode=xxx    coordinate=
    ${response}    Rest.Get    /api/shop/current.json    ${data}    form    ${hosts["weidian"]}
    Connect To Database Using Custom Params    pymysql    host='115.29.xx.xxx', port=3306, user='xx', passwd='xx', db='test', charset= 'utf8'
    Check If Exists In Database    select nickname from authshop where code='xxx'

查询
    #Description
    ${data}    create dictionary    shopCode=xxx    coordinate=
    ${response}    Rest.Get    /api/shop/current.json    ${data}    form    ${hosts["weidian"]}
    Connect To Database Using Custom Params    pymysql    host='115.29.xx.xxx', port=3306, user='xxx', passwd='xxx', db='test', charset= 'utf8'
    ${qeeryResults}    query    select nickname from authshop where code='xxx'
    log    ${qeeryResults}

返回查询结果总行数
#Row Count
    ${data}    create dictionary    shopCode=xxx    coordinate=
    ${response}    Rest.Get    /api/shop/current.json    ${data}    form    ${hosts["weidian"]}
    Connect To Database Using Custom Params    pymysql    host='115.29.xx.xxx', port=3306, user='xxx', passwd='xxx', db='xxx', charset= 'utf8'
    ${qeeryResults}    row count    select shop_code from authshop_relation where site_id='1'
    log    ${qeeryResults}

  4.api详情

API参数说明
Check If Exists In Database查询语句数据库中存在:存在,则为PASS,不存在,则为FAIL
Check If Not Exists In Database查询语句数据库中不存在:不存在,则为PASS,存在,则为FAIL
Connect To DatabasedbapiModuleName=None, dbName=None, dbUsername=None, dbPassword=None, dbHost=localhost, dbPort=5432, dbConfigFile=./resources/db.cfg连接数据库配制信息,通过配制各项属性完成
Connect To Database Using Custom ParamsdbapiModuleName=None, db_connect_string=连接数据库配制信息,通过db_connect_string来传递。如database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432
Delete All Rows From Table表名删除数据库中表的所有行:删除成功,则PASS;失败,则Fail
Description查询语句返回查询语句的结果,内容格式为: [Column(name='id', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)] [Column(name='first_name', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)] [Column(name='last_name', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)]
Disconnect From Database断开数据库链接
Execute Sql ScriptSqlScriptFileName执行脚本文件。SqlScriptFileName物理路径+FileName。
Execute Sql StringSqlString执行Sql语句
Query查询语句返回查询语句的查询结果
Row Count查询语句返回查询语句的查询结果行总数
Row Count Is 0查询语句查询语句的查询结果行总数:为0,则PASS;非0,则FAIL
Row Count Is Equal To X查询语句,X

查询语句的查询结果行总数:为X,则PASS;非X,则FAIL

X,为预期的查询结果行数,第二个需要传递的参数

Row Count Is Greater Than X查询语句,X

查询语句的查询结果行总数:大于X,则PASS;小于或等于X,则FAIL

X,为预期的查询结果行数,第二个需要传递的参数

Row Count Is Less Than X查询语句,X

查询语句的查询结果行总数:大于X,则PASS;小于或等于X,则FAIL

X,为预期的查询结果行数,第二个需要传递的参数

Table Must Exist表名表名必须存在:存在,则PASS;不存在,则FAIL

 

   5.实例二

    #Connect To Database Using Custom Params:使用参数来连接数据库    pymysql:连接的数据库     charset=‘utf8’解决中文显示成???的问题
    Connect To Database Using Custom Params    pymysql    host=‘115.29.10.xxx’, port=3306, user=‘xxx’, passwd=‘xxx’, db=‘xxx’,charset=‘utf8’
    #使用Query方法来执行"select name from wx_account_detail where type=0”,并将查询的结果返回对变量@{name}
    @{name}    query    select name from wx_account_detail where type=0
    #循环获取多条记录的值,并打印
    : FOR    ${i}    IN RANGE    0    len(@{name})
    \    log    @{name}[${i}]

 

   

转载于:https://www.cnblogs.com/chengchengla1990/p/8298562.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值