天天看點

robotframework技巧,讓一個關鍵字自動識别所處 在測試架構中的位置

如何寫一個關鍵字,讓關鍵字自己判斷是在  suitesetup testsetup test testteardown suiteteardown的哪個裡面呢

有些場景,我們希望簡化使用,在以上5個階段,使用相同的關鍵字,關鍵字自行根據所在位置,執行不同的動作;減少寫腳本時自行判斷的工作量

例如一個統一得的關鍵字,叫做  ”執行“”:

在suite_setup自動調用 分支   執行_suite_setup

在suite_setup自動調用 分支    執行_suite_setup

自動化腳本簡化為:多清爽

*** Settings ***
Suite Setup       執行
Suite Teardown    執行

test1
    執行

    [Teardown]    執行
           

以下是思考方式和實作:就是根據狀态+各種suite和test的目前資訊的組合來判斷

目前test_setup和test執行過程,無法區分,有思路的有同學請告知

${測試套件資訊狀态_元組格式}=    Run Keyword And Ignore Error    log    ${SUITE_MESSAGE}
    ${測試用例資訊狀态_元組格式}=    Run Keyword And Ignore Error    log    ${TEST_MESSAGE}
    ${測試套件名字_元組格式}=    Run Keyword And Ignore Error    log    ${SUITE_NAME}
    ${測試用例名字_元組格式}=    Run Keyword And Ignore Error    log    ${TEST_NAME}
    Comment    ${測試用例狀态_元組格式}=    Run Keyword And Ignore Error    log    ${TEST_STATUS}
    ${測試套件資訊狀态}=    Get From List    ${測試套件資訊狀态_元組格式}    0
    ${測試套件名字}=    Get From List    ${測試套件名字_元組格式}    0
    ${測試用例資訊狀态}=    Get From List    ${測試用例資訊狀态_元組格式}    0
    ${測試用例名字}=    Get From List    ${測試用例名字_元組格式}    0
    Comment    ${測試用例狀态}=    Get From List    ${測試用例狀态_元組格式}    0
    ${用例執行階段}=    Set Variable    ${測試套件資訊狀态}${測試套件名字}${測試用例資訊狀态}${測試用例名字}
    Run Keyword And Return If    "${用例執行階段}"=="FAILPASSFAILPASS"    test(setup&test)
    Run Keyword And Return If    "${用例執行階段}"=="FAILPASSPASSPASS"    test_teardown   
    Run Keyword And Return If    "${用例執行階段}"=="FAILPASSFAILFAIL"    suite_setup    
    Run Keyword And Return If    "${用例執行階段}"=="PASSPASSFAILFAIL"    suite_teardown  
           

繼續閱讀