robot接口自動化用例編寫

如何手寫robot自動化用例,網上雖然有一大堆教程,但都是關於ride的,對於如何從腳本角度來寫,一直零零散散,語焉不詳,本文以互聯網最常見的API接口自動化爲例,給出方向性指引,文字不多,足夠你理解,想要深入,請依循文中的文字,再去百度,慢慢體會,逐步精深。

用例編寫前提:已經部署好了robot/python環境

第一個http接口

*** Settings ***
Library  Collections
Library  Process
Library  RequestsLibrary

*** Variables ***
${DOMAIN}       http://10.xx.xx.xx:8091
${URI}          /test/backend/xxxxlogin
${HEADER}      {"Content-Type":"application/json"}
${USER}        {"accountType":"1", "phonenumStr":"iamusername", "password":"iampassword"}
${LIST_ANIMALS}    ["cat", "dog"]

*** Test Cases ***
my first http test case
    create session  myhttp          ${DOMAIN}
    ${uri}          set variable    ${URI}
    # headers一定要轉成 json,不然data傳不過去   
    ${header}       to json         ${HEADER}
    ${postdata}     to json         ${DATA}
    ${resp}         post request    myhttp    uri=${uri}      data=${postdata}       headers=${header}

    # 校驗 http的狀態 
    should be equal as integers  200    ${resp.status_code}
    # 打印 調測信息
    #log     ${resp.content}
    #log     ${json}
    #log     ${json[0]}
    #log     ${json[1]["id"]}

    ${json}   to json     ${resp.content}
    # 校驗返回數據的關鍵值
    should be equal as strings   2.0   ${json[1]["jsonrpc"]}

多步操作

所有的返回值都是字符串類型,轉成json後就可以像字典型一樣隨意取用了

    ${json}   to json     ${resp.content}

把取到值當成下一次調用的入參

建議每個testcase只測試一個功能,接口測試階段,不要構造過於複雜的場景(用例代碼超過一屏將被視爲複雜,需要拆分)

結果校驗

請求http獲得的返回都是字符串類型,解析成json纔可以自由取用值
提供多種校驗函數如下:

should be equal as integers    
should be equal as numbers  
should be equal as strings
should be true
should be empty
should contain  
should contain any
# not 也支持 
should not be equal as integers    
should not be equal as numbers  
should not be equal as strings
should not be true
should not be empty
should not contain  
should not contain any

數據驅動

robot宣稱支持數據驅動,支持按照template從表格讀數據來跑用例,但是問題是,如果要對數據表格裏的case做揀選和拆分就會無力了,所以,數據驅動適合比較大規模的協議測試,而對於有細力度的統計case的需求是不好用的,我們的建議是用測試數據分散在testcase裏,保持case的獨立性和靈活性,如果確實有大量的手工操作的話,提供小工具自動創建testcase。

數據驅動即爲定義好流程(Template),然後就可以按照參數羅列形成用例了,每一條數據都會獨立執行

*** Test Cases ***
Invalid Username
    [Tags]    Iteration-3    Smoke
    [Template]  Login with invalid credentials should fail
    invalidname      qwe123
    091931          invalidpassword

*** Test Cases ***
Login Success
    [Tags]    Iteration-3    Smoke  core
    [Template]  Login success
    068970          qwe123
    052255          qwe123

前置條件(可選)

一個用例文件中包含很多testcase,我想統一加些動作該怎麼辦?框架提供兩個方法,如果是前置條件(每個用例開始前執行)就用Set up,在setting裏編寫你的動作即可,如果動作很複雜,可以做成關鍵字調用即可,就像下面一樣:


    *** Settings ***
    Test Setup      Get config

    *** Keywords ***
    Get config
            log     正在初始化...
            set suite variable  ${config}       {"env":"uat", "user":"haha"}

清理動作(可選)

如果是後置條件(每個testcase結束後調用),就用Tear down,樣例如下:


*** Settings ***
Test Teardown   Do clean

*** Keywords ***
Do clean
    log     正在清理...

關鍵字驅動

框架提供自定義的關鍵字方案,只需要在Keywords下面書寫你的邏輯動作即可,關鍵字可以是英文,也可以是中文,頂格寫,不限制空格和特殊字符,你可以像書寫自然語言一樣寫用例代碼啦

*** Keywords ***
My first keywords
    log     hello keywords

我的第一個關鍵字
    log     你好, 我的關鍵字
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章