In Robot Framework, how to perform data-driven testing by creating a separate test case for each line of data in a text file?

前端 未结 3 1462
温柔的废话
温柔的废话 2021-01-13 15:46

In Robot Framework, we can use Test Template to perform data-driven testing. However, in this method, the number of test cases are fixed. We are not able to add new test cas

3条回答
  •  清歌不尽
    2021-01-13 16:41

    I am not sure if you are really looking to create separate Test Cases (ie. with separate PASS / FAIL status etc in the output report), or simply looking to repeat a sequence of test steps using a set of data?

    If the latter, you can easily read in lines from external files using the OperatingSystem library, parse the contents of the file using the String library, then repeatedly call a user keyword with the contents of each line.

    | *** Settings ***
    | Library | OperatingSystem | WITH NAME | os |
    | Library | String | WITH NAME | str |
    
    | *** Test Cases *** |
    | Read Data From File |
    | | ${fileContents}= | os.Get File | data.txt |
    | | ${rows}= | str.Split To Lines | ${fileContents} |
    | | :FOR | ${row} | IN | @{rows} |
    | |      | ${cols}= | str.Split String | ${row} | , |
    | |      | My Test Keyword | @{cols} |
    
    | *** Keywords *** |
    | My Test Keyword |
    | | [Arguments] | @{fields} |
    | | Log Many | ${fields} |
    

    The first failure of My Test Keyword would normally fail the entire Read Data From File test case. If you wanted to run as many as possible, and then collate the results, use the Run Keyword And Ignore Error keyword from the BuiltIn library.

提交回复
热议问题