我有以下兩個測驗,
*** Settings ***
Library CustomizeLibrary
*** Variables ***
${username} anti
${password} anti
${headers} {"Content-Type": "application/json"}
*** Test Cases ***
GET token based on existing user
${token}= Customize Get Token http://127.0.0.1:5000/api/auth/token ${username} ${password} ${headers}
Set suite variable ${token}
GET Users
${token} Customize Get Token http://127.0.0.1:5000/api/auth/token ${token} (Gives error: Variable 'token' is used, but not defined)
${token}在之前的測驗中被定義并設定為套件變數還不能使用它,任何建議?
uj5u.com熱心網友回復:
我建議在套件設定期間通過呼叫關鍵字來初始化變數來設定套件變數。它將在測驗之前運行并消除測驗之間的依賴性。
這是一個虛擬示例:
*** Settings ***
Suite Setup Init Suite Variable
*** Test Cases ***
Scenario: Test 1 - Suite Variable
Log To Console ${suite_variable}
Scenario: Test 2 - Test Variable
Init Test Variable
Log To Console ${test_variable}
*** Keywords ***
Init Suite Variable
Set Suite Variable ${suite_variable} I'm at suite level
Init Test Variable
Set Test Variable ${test_variable} I'm at test level
在你的情況下,它看起來像......
*** Settings ***
Library CustomizeLibrary
Suite Setup GET token based on existing user
*** Variables ***
${username} anti
${password} anti
${headers} {"Content-Type": "application/json"}
*** Test Cases ***
GET Users
${token} Customize Get Token http://127.0.0.1:5000/api/auth/token
*** Keywords ***
GET token based on existing user
${token}= Customize Get Token http://127.0.0.1:5000/api/auth/token ${username} ${password} ${headers}
Set suite variable ${token}
uj5u.com熱心網友回復:
它被稱為“靜態變數”,一個不限于單個測驗/方法/類實體的變數(谷歌了解更多資訊)。此變數存在于所有測驗之外。Robotframework 稱其為“套件變數”,另請參見http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set Suite Variable
如何實作:Robot Framework 中的靜態變數
*** Settings ***
...
*** Variables ***
.... other variables ....
*** Test Cases ***
GET token based on existing user
${token}= Customize Get Token http://127.0.0.1:5000/api/auth/token ${username} ${password} ${headers}
Set suite variable ${token}
GET Users
${token} Customize Get Token http://127.0.0.1:5000/api/auth/token
備注:另一個答案更好,它解決了初始化部分,因此可以以不同的順序注釋/跳過/運行測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/385904.html
