RobotFramework查詢MongoDB數(shù)據(jù)庫并提取數(shù)據(jù)

RobotFramework查詢MongoDB數(shù)據(jù)庫比較容易,搜索一下都是講怎么查詢的。但是查詢之后需要從返回?cái)?shù)據(jù)中提取需要的信息就沒人說了,現(xiàn)總結(jié)如下

安裝需要的工具和包

1.安裝python,建議使用3.7.因?yàn)閞ide對(duì)高版本兼容不好
2.安裝包

pip install robotframework
pip install robotframework-ride
pip install pymongo
pip install robotframework-mongodblibrary

我這邊需求是在數(shù)據(jù)庫中取出需要的兩個(gè)字段信息,然后拼接成一個(gè)json格式的header信息作為其它測(cè)試用例的輸入。

說明:關(guān)鍵信息用xxx代替,請(qǐng)根據(jù)自身情況進(jìn)行修改

*** 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ù)庫的整個(gè)操作定義了一個(gè)用戶關(guān)鍵字Get Header
[Arguments]定義一個(gè)參數(shù)${recordJSON},使用該關(guān)鍵字需要傳入,即MongoDB中查詢需要傳入的json

Connect To MongoDB
連接MongoDB數(shù)據(jù)庫
需要傳的參數(shù)有4個(gè),mongodb://user:pwd:@host:port。

${id} Retrieve Mongodb Records With Desired Fields ${mongoName} A ${recordJSON} _id returnDocuments=True
此處是先從A中查詢出_id值賦值給變量${id}作為下一個(gè)查詢的輸入.返回結(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兩個(gè)字段的值,returnDocuments=True表示返回的是一個(gè)列表,方便后續(xù)提取數(shù)據(jù)。默認(rèn)returnDocuments=False返回的是字符串不方便提取,只能用來校驗(yàn)查詢結(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)建測(cè)試用例調(diào)用關(guān)鍵字測(cè)試

輸出結(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"}就是返回的拼接后的字符串

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容