Try   HackMD

資料驅動測試

在 SideeX 中,使用者可以使用預先準備好的資料集來執行同一份測試案例。例如,假設您想針對某個登入畫面進行測試,您可以將預先建立的多筆人員資料匯入到 SideeX,由 SideeX 為您帶入每筆資料進行測試。本文將介紹如何使用資料驅動,把資料檔載入 SideeX 並進行測試。

步驟 1:準備資料檔

  1. 資料檔須為 JSON 或 CSV 格式(若資料檔非 JSON 或 CSV 格式,則須先自行轉換)。
  2. 範例:
    • 下列為擁有二個 Keys,四個 Values,一共八筆資料的 JSON 檔:
      ​​​​​​​​[ ​​​​​​​​ { ​​​​​​​​ "key1": "value1", ​​​​​​​​ "key2": "value5" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value2", ​​​​​​​​ "key2": "value6" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value3", ​​​​​​​​ "key2": "value7" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value4", ​​​​​​​​ "key2": "value8" ​​​​​​​​ } ​​​​​​​​]
    • 下列為擁有二個 Keys,四個 Values,一共八筆資料的 CSV 檔:
      ​​​​​​​​key1,key2 ​​​​​​​​value1,value5 ​​​​​​​​value2,value6 ​​​​​​​​value3,value7 ​​​​​​​​value4,value8

步驟 2:匯入資料檔

  1. 進入到「Data Driven」視窗,並按下「Import from file」按鈕:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 選取需要匯入的資料檔後,在 「Table」欄位出現資料集檔名即上傳成功:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    注意

    當欲上傳的資料檔檔名已存在於目前的「Table」欄位,此時上傳會失敗。若想更新同名的資料檔,請先把舊的檔案於「Table」欄位刪除後再進行上傳。

步驟 3:建立資料驅動指令

  1. 使用資料驅動測試,需要搭配 FOR_EACH_RECORDEND 這兩個指令。
  2. 在欲執行資料測試的地方按下滑鼠右鍵,新增 FOR_EACH_RECORD 指令,並在 FOR_EACH_RECORD 指令的 「Target」欄位填入想匯入的資料集檔名。例如:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  3. 使用 ${YOUR_KEY} 的方式帶入資料。例如:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  4. 最後,在欲結束資料驅動測試的地方使用 END 指令作為結尾:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

步驟 4:播放測試案例

  1. 播放測試案例時,FOR_EACH_RECORD 指令會自動把「Target」欄位指定的資料轉為全域變數,並把 FOR_EACH_RECORD 指令到 END 指令之間的指令帶入每筆資料後各執行一次。

    • 從「全域變數」視窗可看到匯入資料:

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • 從執行結果可看到每筆資料被帶入並執行:

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

廷伸案例:一對多資料驅動測試

如果想對資料進行一對多測試,可以預先準備兩個資料集,並使用巢狀 FOR_EACH_RECORD 指令實現。請參考以下範例:

  1. 準備好資料集:
    • CSV 範例資料集一:
      ​​​​​​​​names ​​​​​​​​apple ​​​​​​​​peach ​​​​​​​​lemon ​​​​​​​​mango
    • CSV 範例資料集二:
      ​​​​​​​​prices ​​​​​​​​50 ​​​​​​​​60 ​​​​​​​​70 ​​​​​​​​80
  2. 建立指令:
    ​​​​/* 虛擬碼 */ ​​​​for (each name in names){ ​​​​ for (each price in prices){ ​​​​ echo name ​​​​ echo price ​​​​ } ​​​​}
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  3. 上述指令便會產生出一對多的資料組合:
    ​​​​apple -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​​​ ​​​​peach -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​​​ ​​​​lemon -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​ ​​​​mongo -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80