1. 程式人生 > >RobotFrameWork(七)資料驅動之Template模板

RobotFrameWork(七)資料驅動之Template模板

在這裡我們介紹RobotFramework一個很有用的功能——測試模板。測試用例中的關鍵字,例如:


執行時,只會執行一次。可有時候我們需要連續執行這個關鍵字。如果我們連續寫這個關鍵字,用例會顯得很臃腫。

RobotFramework提供了一個解決方案,在關鍵字前面加上一個[Template],表示這個用例是一個“測試模板”。

例如:


執行以上用例:


這個用例裡,“log”關鍵字被連續執行了三次。與普通的關鍵字寫法不同,帶有[Template]的關鍵字的引數不能和關鍵字同行。每帶有一行引數,關鍵字就需要執行一次。熟悉QTP的朋友是不是覺得似曾相識啊?


需要特別注意的是,一個用例中只能使用一個[Template],不能多次使用。也不能帶[Template]的關鍵字和普通的關鍵字混合使用。 利用“[Template]”,robotFramework可以輕鬆的實現資料驅動。

測試模板,關鍵字驅動 => 資料驅動

關鍵字驅動, 用例主體由若干關鍵字+引數構成
vs.
資料驅動, 用例主體只由Template關鍵字的引數構成

用途舉例:
對每個測試用例,或者一個測試檔案中的所有用例,重複執行同一個關鍵字多次(使用不同資料)
也可以只針對測試用例,或者每個測試檔案只執行一次

模板關鍵字可以接受普通的位置引數,命名引數
關鍵字名中可以使用引數
不可以使用變數定義模板關鍵字

樣例1
[Template]會覆蓋Setting Table中的template設定
如果[Template]為空值,意味著沒有模板

* Test Cases 
Normal test case

Example keyword first argument second argument

Templated test case

[Template] Example keyword
first argument second argument

樣例2
對於多行資料,模板關鍵字會逐行呼叫執行,一次一行 如果其中有些失敗,其他也會執行。 對於普通用例的continue on failure模式,對於模板關鍵字是預設行為。

Settings 
Test Template Example keyword

Test Cases 
Templated test case

first round 1 first round 2
second round 1 second round 2
third round 1 third round 2

樣例3
模板關鍵字支援嵌入引數的語法
關鍵字名字就作為引數的持有者,在實際執行中這些引數會被模板關鍵字解析出實際的引數,傳遞給低階的底層關鍵字作為引數

Test Cases 
Normal test case with embedded arguments

The result of 1 + 1 should be 2
The result of 1 + 2 should be 3

Template with embedded arguments

[Template] The result of ${calculation} should be ${expected}
1 + 1 2
1 + 2 3

Keywords 
The result of ${calculation} should be ${expected}

${result} = Calculate ${calculation}
Should Be Equal ${result} ${expected}

樣例4
模板關鍵字名字中的引數個數必須匹配它將使用的引數數量
引數名不需要匹配原始關鍵字的引數

Test Cases 
Different argument names

[Template] The result of ${foo} should be ${bar}
1 + 1 2
1 + 2 3

Only some arguments

[Template] The result of ${calculation} should be 3
1 + 2
4 - 1

New arguments

[Template] The ${meaning} of ${life} should be 42
result 21 * 2

樣例5 帶有for迴圈的模板關鍵字

Test Cases 
Template and for

[Template] Example keyword
:FOR ${item} IN @{ITEMS}
\ ${item} 2nd arg
:FOR ${index} IN RANGE 42
\ 1st arg ${index}

不同的測試用例風格

  • 關鍵字驅動
    • 描述工作流
    • 若干關鍵字和他們必要的引數
  • 資料驅動
    • 針對相同工作流,執行不同的輸入資料
    • 只使用一個高階的使用者關鍵字,其中定義了工作流,然後使用不同的輸入和輸出資料測試相同的場景
    • 每個測試中可以重複同一個關鍵字,但是test template功能只允許定義以此被使用的關鍵字
  • 行為驅動:
    • 描述工作流
    • Acceptance Test Driven Development, ATDD
    • Specification by Example
    • BDD's Given-When-Then
    • And or But,如果測試步驟中操作較多
    • 支援嵌入資料到關鍵字名

樣例1

Settings 
Test Template Login with invalid credentials should fail

Test Cases USERNAME PASSWORD
Invalid User Name invalid ${VALID PASSWORD}
Invalid Password ${VALID USER} invalid
Invalid User Name and Password invalid invalid
Empty User Name ${EMPTY} ${VALID PASSWORD}
Empty Password ${VALID USER} ${EMPTY}
Empty User Name and Password ${EMPTY} ${EMPTY}

樣例2

Test Cases 
Invalid Password

[Template] Login with invalid credentials should fail
invalid ${VALID PASSWORD}
${VALID USER} invalid
invalid whatever
${EMPTY} ${VALID PASSWORD}
${VALID USER} ${EMPTY}
${EMPTY} ${EMPTY}

樣例1和樣例2都是資料驅動的test template樣例。
樣例1有命令列,方便閱讀理解; test template在setting table中定義; 每行有名字也方便檢視結果(如果行數不是太多的話)
樣例2在一個用例中完成所有的事情

+

樣例3
搜尋關鍵字的時候, 如果full name沒有搜尋到, Given-When-Then-And-But等字首會被忽略

Test Cases 
Valid Login

Given login page is open
When valid username and password are inserted
and credentials are submitted
Then welcome page should be open