Automated tests 在Robot框架中,如何通过为文本文件中的每行数据创建单独的测试用例来执行数据驱动测试?

Automated tests 在Robot框架中,如何通过为文本文件中的每行数据创建单独的测试用例来执行数据驱动测试?,automated-tests,robotframework,Automated Tests,Robotframework,在Robot框架中,我们可以使用来执行数据驱动测试。然而,在这种方法中,测试用例的数量是固定的。我们无法动态添加新的测试用例 假设我有一个CSV文本文件,data.txt: data-1a, data-1b, data-1c data-2a, data-2b, data-2c .... data-Na, data-Nb, data-Nc CSV文件中的行数将不时更改 在我的Robot框架测试用例文件中,我将阅读这个CSV文件。假设该文件中有N行数据,我想创建N测试用例,每个测试用例使用该文件中

在Robot框架中,我们可以使用来执行数据驱动测试。然而,在这种方法中,测试用例的数量是固定的。我们无法动态添加新的测试用例

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

data-1a, data-1b, data-1c
data-2a, data-2b, data-2c
....
data-Na, data-Nb, data-Nc
CSV文件中的行数将不时更改

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


是否可以在Robot框架中执行此操作?

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

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

| *** 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关键字的第一次失败通常会导致整个
从文件读取数据的失败
测试用例。如果希望尽可能多地运行,然后整理结果,请使用内置库中的
run关键字并忽略Error
关键字。

无法直接执行所需操作。相反,您可以编写一个脚本来读取您的数据文件,并根据该数据自动生成robot测试套件。用于运行测试的脚本可以先运行另一个脚本来创建测试文件,然后再运行它


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

我觉得低于1是更好的方法

创建两个库,一个用于读取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
在测试文件中包括这两个库。 使用length,比如“N”,您可以通过以下帮助获得行数据/单元格数据:${index}范围${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}]}

嗨,Richard,谢谢你的回复,实际上我正在尝试在输出报告中创建具有单独通过/失败状态的单独测试用例。请参阅如何生成套件。我有一个基于DB中的行创建65000多个测试用例的设置。@ombre42感谢您的链接!看起来很有趣。我想它可以解决我的问题。