RobotFramework查询MongoDB数据库比较容易,搜索一下都是讲怎么查询的。但是查询之后需要从返回数据中提取需要的信息就没人说了,现总结如下
安装需要的工具和包
1.安装python,建议使用3.7.因为ride对高版本兼容不好
2.安装包
pip install robotframework
pip install robotframework-ride
pip install pymongo
pip install robotframework-mongodblibrary
我这边需求是在数据库中取出需要的两个字段信息,然后拼接成一个json格式的header信息作为其它测试用例的输入。
说明:关键信息用xxx代替,请根据自身情况进行修改
*** Settings ***
Library MongoDBLibrary
Resource ../Common/CommonParams.txt
*** Test Cases ***
test_mongodb
${ret} Get Header {"LoginName":"xxx"}
Log ${ret}
*** Keywords ***
Get Header
[Arguments] ${recordJSON}
Connect To MongoDB mongodb://${mongoUser}:${mongoPwd}@${mongoUrl}:${mongoPort}
${id} Retrieve Mongodb Records With Desired Fields ${mongoName} Account ${recordJSON} _id returnDocuments=True
${result} Retrieve Mongodb Records With Desired Fields ${mongoName} User {"AccountID":"${id}[0][_id]"} aa,bb returnDocuments=True
Disconnect From Mongodb
[Return] {"Content-Type":"application/json","aa":"${result}[0][aa]","bb":"${result}[0][bb]"}
代码说明
*** Settings ***
Library MongoDBLibrary
Resource ../Common/CommonParams.txt
导入MongoDBLibrary
由于我的MongoDB数据库的连接参数都是定义到变量文件中的,所以要先引用
*** Keywords ***
Get Header
[Arguments] ${recordJSON}
Connect To MongoDB mongodb://${mongoUser}:${mongoPwd}@${mongoUrl}:${mongoPort}
${id} Retrieve Mongodb Records With Desired Fields ${mongoName} Account ${recordJSON} _id returnDocuments=True
${result} Retrieve Mongodb Records With Desired Fields \${mongoName} User {"AccountID":"${id}[0][_id]"} aa,bb returnDocuments=True
Disconnect From Mongodb
[Return] {"Content-Type":"application/json","aa":"${result}[0][aa]","bb":"${result}[0][bb]"}
将数据库的整个操作定义了一个用户关键字Get Header
[Arguments]定义一个参数${recordJSON},使用该关键字需要传入,即MongoDB中查询需要传入的jsonConnect To MongoDB
连接MongoDB数据库
需要传的参数有4个,mongodb://user:pwd:@host:port。${id} Retrieve Mongodb Records With Desired Fields ${mongoName} A ${recordJSON} _id returnDocuments=True
此处是先从A中查询出_id值赋值给变量${id}作为下一个查询的输入.返回结果是[{'_id': 'xxx'}],则可以通过列表索引、字典key取值的方式取出'_id'的值为'xxx'${result} Retrieve Mongodb Records With Desired Fields ${mongoName} U {"AccountID":"${id}[0][_id]"} aa,bb returnDocuments=True
从U中查询aa和bb两个字段的值,returnDocuments=True表示返回的是一个列表,方便后续提取数据。默认returnDocuments=False返回的是字符串不方便提取,只能用来校验查询结果Disconnect From Mongodb
查询结束后断开数据库连接[Return] {"Content-Type":"application/json","key1":"${result}[0][aa]","key2":"${result}[0][bb]"}
返回拼接好的字符串
${result}结果为[{'_id': 'xxx', 'aa': 'xxx','bb': 'xxx'}],取出'aa'和'bb'的值分别为"${result}[0][aa]","${result}[0][bb]",之后再作为新字典'key1'、'key2'的value返回
*** Test Cases ***
test_mongodb
${ret} Get Header {"LoginName":"xxx"}
Log ${ret}
最后在*** Test Cases ***中创建测试用例调用关键字测试
输出结果为
Starting test: YAMI.Test.Test.test_mongodb
20210511 15:17:44.991 : INFO : ${id} = [{'_id': 'xxx'}]
20210511 15:17:45.029 : INFO : ${result} = [{'_id': 'xxx', 'aa': 'xxx','bb': 'xxx'}]
20210511 15:17:45.068 : INFO : ${ret} = {"Content-Type":"application/json","aa":"xxx","bb":"xxx"}
20210511 15:17:45.070 : INFO : {"Content-Type":"application/json","key1":"xxx","key2":"xxx"}
Ending test: YAMI.Test.Test.test_mongodb
{"Content-Type":"application/json","key1":"xxx","key2":"xxx"}就是返回的拼接后的字符串