【问题标题】:In Robot Framework, how to perform data-driven testing by creating a separate test case for each line of data in a text file?在 Robot Framework 中,如何通过为文本文件中的每一行数据创建单独的测试用例来执行数据驱动测试?
【发布时间】:2014-11-28 17:44:17
【问题描述】:

在 Robot Framework 中,我们可以使用Test Template 来执行数据驱动测试。但是,在这种方法中,测试用例的数量是固定的。我们无法即时添加新的测试用例。

假设我有一个 CSV 文本文件,data.txt

data-1a, data-1b, data-1c
data-2a, data-2b, data-2c
....
data-Na, data-Nb, data-Nc

CSV 文件中的行数会不时改变。

在我的 Robot Framework 测试用例文件中,我将读取这个 CSV 文件。假设该文件中有N lines 的数据,我想创建N 测试用例,每个测试用例都使用该文件中的1 行数据作为参数。

是否可以在 Robot Framework 中做到这一点?

【问题讨论】:

    标签: automated-tests robotframework


    【解决方案1】:

    我不确定您是否真的希望创建单独的测试用例(即在输出报告中具有单独的 PASS/FAIL 状态等),或者只是希望使用一组数据重复一系列测试步骤?

    如果是后者,您可以使用OperatingSystem 库轻松地从外部文件中读取行,使用String 库解析文件的内容,然后使用每行的内容重复调用用户关键字。

    | *** 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} |
    

    My Test Keyword 的第一次失败通常会使整个Read Data From File 测试用例失败。如果您想尽可能多地运行,然后整理结果,请使用 BuiltIn 库中的 Run Keyword And Ignore Error 关键字。

    【讨论】:

    • 嗨,Richard,感谢您的回复,实际上我正在尝试在输出报告中创建具有单独 PASS/FAIL 状态的单独测试用例。
    【解决方案2】:

    没有办法直接做你想做的事。您可以做的是编写一个脚本来读取您的数据文件并根据该数据自动生成机器人测试套件。您用于运行测试的脚本可以先运行此其他脚本以创建测试文件,然后再运行它。

    您也可以通过套件设置创建测试套件,但我认为我不建议这样做,因为我认为没有任何好处,而且它会使您的套件更加复杂。

    【讨论】:

    • 请参阅code.google.com/p/robotframework/wiki/…,了解如何生成套件。我有一个基于数据库中的行创建超过 65,000 个测试用例的设置。
    • @ombre42 感谢您的链接!它看起来很有趣。我认为它可以解决我的问题。
    【解决方案3】:

    我觉得下面一个是更好的方法。

    创建两个库,一个用于读取 csv 数据,另一个用于获取如下所示的行数。

    csvLibrary.py

     1 import csv
     2 class csvLibrary(object):
     3
     4     def read_csv_file(self, filename):
     5         '''This creates a keyword named "Read CSV File"
     6
     7         This keyword takes one argument, which is a path to a .csv file. It
     8         returns a list of rows, with each row being a list of the data in
     9         each column.
     10         '''
     11         data = []
     12         with open(filename, 'rb') as csvfile:
     13             reader = csv.reader(csvfile)
     14             for row in reader:
     15                 data.append(row)
     16         return data
    

    csvLibraryNoOfRows.py

    1 import csv
    2 class csvLibraryNoOfRows(object):
    3
    4     def csv_length(self, filename):
    5         '''This creates a keyword named "CSV Length"
    6
    7         This keyword takes one argument, which is a path to a .csv file. It
    8         returns a list of rows, with each row being a list of the data in
    9         each column.
    10         '''
    11         length=0
    12         with open(filename, 'rb') as csvfile:
    13             reader = csv.reader(csvfile)
    14             for row in reader:
    15                 length+=1
    16         return length
    

    在您的测试文件中包含这两个库。 使用长度,让我们说“N”,您可以借助 :FOR ${index} IN RANGE ${csvlength}

    获取行数据/单元格数据

    示例代码如下。

    Library        csvLibrary.py
    Library        csvLibraryNoOfRows.py
    
    *** Test Cases ***
    
    Reading a csv file
    ${csvdata}=    read csv file   sample.csv
    ${csvlength}=   csv length  sample.csv
    :FOR    ${index}    IN RANGE    ${csvlength}
        \   log ${csvdata[${index}]}
    

    【讨论】:

      【解决方案4】:

      查看https://pypi.org/project/RobotFramework-Examples/ 这个库可以让你读取数据,然后引用它来自动生成测试用例。

      这应该可行:

      *** Settings ***
      Library    OperatingSystem
      Library    Examples    autoexpand=Off
      Suite Setup      Setup this suite
      
      *** Test cases ***
      My test with examples for ${column1}
          Log    Hello ${column2}, welcome to ${column3}    console=True
          
          Examples:    column1    column2    column3    --
                ...    @{test data}
      
      *** Keywords ***
      Setup this suite
          ${fileContents}=   Get File    data.txt
          ${test data}       Evaluate    re.split("\n|,", $filecontents)   modules=re
          Expand test examples
      

      【讨论】:

        猜你喜欢
        • 2019-04-03
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        相关资源
        最近更新 更多