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 Database | dbapiModuleName=None, dbName=None, dbUsername=None, dbPassword=None, dbHost=localhost, dbPort=5432, dbConfigFile=./resources/db.cfg | 连接数据库配制信息,通过配制各项属性完成 |
Connect To Database Using Custom Params | dbapiModuleName=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 Script | SqlScriptFileName | 执行脚本文件。SqlScriptFileName物理路径+FileName。 |
Execute Sql String | SqlString | 执行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}]