RobotFramework查詢MongoDB數(shù)據(jù)庫比較容易隐岛,搜索一下都是講怎么查詢的冤荆。但是查詢之后需要從返回數(shù)據(jù)中提取需要的信息就沒人說了,現(xiàn)總結(jié)如下
安裝需要的工具和包
1.安裝python,建議使用3.7.因為ride對高版本兼容不好
2.安裝包
pip install robotframework
pip install robotframework-ride
pip install pymongo
pip install robotframework-mongodblibrary
我這邊需求是在數(shù)據(jù)庫中取出需要的兩個字段信息笋颤,然后拼接成一個json格式的header信息作為其它測試用例的輸入。
說明:關(guān)鍵信息用xxx代替,請根據(jù)自身情況進行修改
*** 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
導(dǎo)入MongoDBLibrary
由于我的MongoDB數(shù)據(jù)庫的連接參數(shù)都是定義到變量文件中的伴澄,所以要先引用
*** 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]"}
將數(shù)據(jù)庫的整個操作定義了一個用戶關(guān)鍵字Get Header
[Arguments]定義一個參數(shù)${recordJSON}赋除,使用該關(guān)鍵字需要傳入,即MongoDB中查詢需要傳入的jsonConnect To MongoDB
連接MongoDB數(shù)據(jù)庫
需要傳的參數(shù)有4個非凌,mongodb://user:pwd:@host:port举农。${id} Retrieve Mongodb Records With Desired Fields ${mongoName} A ${recordJSON} _id returnDocuments=True
此處是先從A中查詢出_id值賦值給變量${id}作為下一個查詢的輸入.返回結(jié)果是[{'_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表示返回的是一個列表,方便后續(xù)提取數(shù)據(jù)喉悴。默認returnDocuments=False返回的是字符串不方便提取棱貌,只能用來校驗查詢結(jié)果Disconnect From Mongodb
查詢結(jié)束后斷開數(shù)據(jù)庫連接[Return] {"Content-Type":"application/json","key1":"${result}[0][aa]","key2":"${result}[0][bb]"}
返回拼接好的字符串
${result}結(jié)果為[{'_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 ***中創(chuàng)建測試用例調(diào)用關(guān)鍵字測試
輸出結(jié)果為
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"}就是返回的拼接后的字符串