如何在Cucumber中使用外部的dataTable?

huangapple go评论63阅读模式
英文:

How to use an external dataTable in Cucumber?

问题

Sure, here's the translated content:

我正在使用Cucumber和Java编写BDD测试。

我想从在特性文件中使用datatables迁移到使用链接到具有值的表的方法,因为我有很多字段,并且对于一些步骤是重复的。
从这个方法:

GIVEN 我创建了帐户,其中包含
| 名称  | 类型  |
| 测试  | 基本  |

到这个方法:

GIVEN 我创建了帐户,帐户表为 account.table

account.table文件的内容为:

| 名称  | 类型  |
| 测试  | 基本  |

我不知道如何解析'path to the file with dataTable'为实际的'dataTable'。
我尝试编写这样的步骤:

@GIVEN("^我创建了帐户,帐户表为 \"([^\"]*)\"$")
public void createAccount(DataTable dataTable) { ... }

但这并不起作用。因为从文件路径到dataTable没有自动转换。
这是错误消息:

cucumber.runtime.CucumberException: 不知道如何将
"resources\testdata\account.table" 转换为cucumber.api.DataTable。尝试编写自己的转换器:
@cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(DataTableConverter.class)

有没有解析它的示例或任何想法呢?

英文:

I'm using Cucumber and Java to write BDD tests.

I wanted to migrate from usung datatables in feature files, because I have a lot of fields and it's dublicated for a few steps, to using link to the table with values.
From this one approach:

GIVEN I created account with
| name | type  |
| test | basic |

to the approach:

GIVEN I created account with account.table

account.table file has value:

| name | type  |
| test | basic |

I don't know how to parse 'path to the file with dataTable' to the actual 'dataTable'.
I tried write step like this:

@GIVEN("^I created account with \"([^\"]*)\"$")
    public void createAccount(DataTable dataTable) { ... }

But it's not working. Because there are no automatic convertion from path to file to dataTable.
Here's the error:

> cucumber.runtime.CucumberException: Don't know how to convert
> "resources\testdata\account.table" into cucumber.api.DataTable. Try
> writing your own converter:
> @cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(DataTableConverter.class)

Is there any examples of parsing it or any ideas?

答案1

得分: 0

用以下代码实现了,但想要一些简单的内容,不需要使用Cucumber本身的东西:

public void readFromExternalTable(String fileName) throws IOException {
    List<String> lines = readLinesFromFile(fileName);
    List<String> headers = getColumnsFromRow(lines.get(0));
    List<String> values = getColumnsFromRow(lines.get(1));
    DataTable table = createTable(headers, values);
}

static List<String> readLinesFromFile(String fileName) {
    List<String> lines = new ArrayList<>();
    BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return lines;
}

static List<String> getColumnsFromRow(String line) {
    List<String> list = new ArrayList<>();
    String[] row = line.split("\\|");
    for (int i = 1; i < row.length; i++) {
        list.add(row[i].trim());
    }
    return list;
}

static DataTable createTable(List<String> headers, List<String> values) {
    List<List<String>> rawTable = new ArrayList<>();
    rawTable.add(headers);
    rawTable.add(values);
    return DataTable.create(rawTable);
}
英文:

Did it with following code, but wanted something easy and out of Cucumber box itself:

public void readFromExternalTable(String fileName) throws IOException {
List&lt;String&gt; lines = readLinesFromFile(fileName);
List&lt;String&gt; headers = getColumnsFromRow(lines.get(0));
List&lt;String&gt; values = getColumnsFromRow(lines.get(1));
DataTable table = createTable(headers, values);
}
static List&lt;String&gt; readLinesFromFile(String fileName) {
List&lt;String&gt; lines = new ArrayList&lt;&gt;();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line =  reader.readLine()) != null) {
lines.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
static List&lt;String&gt; getColumnsFromRow(String line) {
List&lt;String&gt; list = new ArrayList&lt;&gt;();
String[] row  = line.split(&quot;\\|&quot;);
for (int i = 1; i &lt; row.length; i++) {
list.add(row[i].trim());
}
return list;
}
static &lt;String&gt; DataTable createTable(List&lt;String&gt; headers, List&lt;String&gt; values) {
List&lt;List&lt;String&gt;&gt; rawTable = new ArrayList&lt;&gt;();
rawTable.add(headers);
rawTable.add(values);
return DataTable.create(rawTable);
}

答案2

得分: 0

以下是翻译好的内容:

你可以通过QAF-BDD2来使用外部测试数据以及其他许多功能。它具有内置数据提供程序,同时还支持自定义数据提供程序。下面的示例是从CSV提供数据。

@dataFile:resources/data/newuser.csv
场景: 创建新用户
英文:

You can take external test data benefit and many more with QAF-BDD2. It has inbuilt data-providers and supports custom data provider as well. Below example is to provide data from CSV.

@dataFile:resources/data/newuser.csv
Scenario: create new user

huangapple
  • 本文由 发表于 2020年9月14日 18:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63882688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定