如何在黄瓜特征文件中传递复杂对象
How to pass complex object in cucumber feature file
我们的团队正在从干净的 testng 转向 testng+cucumber。在尝试为一个简单的测试创建一个功能时,其中一个不可变对象被发送到服务中,每个数据 运行(testng 世界中的数据提供者)我发现自己需要一个最多 11 个字段的步骤。这有点多 - 这不是一个好习惯。
黄瓜有没有可能从场景大纲中得到复杂的对象?
我试图找到一些关于我的问题的文档,但没有弹出任何内容。也许我找错了地方或使用了错误的查询。
例如。
@Test(
dataProvider = "data"
)
public void sendRequestTest(
SomeComplexRequest request
) {
//Given
some test data prep
//When
Responce resp = sendRequestToService(request);
//Then
assertEquals(resp, expectedResp);
}
@Builder
class SomeComplexRequest{
dataType Field1;
dataType Field2;
dataType Field3;
...
dataType FieldN;
}
虽然可以在数据中使用示例值 table:
Feature: Basic Arithmetic
Background: A Calculator
Given a calculator I just turned on
Scenario Outline: Many additions
Given the previous entries:
| first | second | operation |
| <x> | <y> | <op1> |
| <q> | <r> | <op2> |
When I press +
And I add <a> and <b>
And I press +
Then the result is <c>
Examples: Single digits
| x | y | op1 | q | r | op2 | a | b | c |
| 1 | 1 | + | 2 | 1 | + | 1 | 2 | 8 |
| 0 | 1 | + | 1 | 2 | + | 2 | 3 | 9 |
虽然可以将此数据 table 转换为步骤定义中的对象
@Given("the previous entries:")
public void thePreviousEntries(List<Entry> entries) {
for (Entry entry : entries) {
calc.push(entry.first);
calc.push(entry.second);
calc.push(entry.operation);
}
}
通过使用 table 转换器将 table 转换为条目列表:
https://github.com/cucumber/cucumber-jvm/tree/master/java#transformers
private final ObjectMapper objectMapper = new ObjectMapper();
@DefaultParameterTransformer
@DefaultDataTableEntryTransformer
@DefaultDataTableCellTransformer
public Object transformer(Object fromValue, Type toValueType) {
return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType));
}
您可能没有以最有益的方式使用 Cucumber。使用 11 个参数,您实际上可能甚至没有以最有益的方式使用当前的单元测试。
考虑:
- 消除测试中的所有附带细节。
- 使用原型模式。这样你只需要描述对原型请求所做的更改
- 重新思考您的测试策略并重组您的测试策略,使您的黄瓜测试可以专注于高级行为,同时将细节推入更小的单元测试。
我们的团队正在从干净的 testng 转向 testng+cucumber。在尝试为一个简单的测试创建一个功能时,其中一个不可变对象被发送到服务中,每个数据 运行(testng 世界中的数据提供者)我发现自己需要一个最多 11 个字段的步骤。这有点多 - 这不是一个好习惯。
黄瓜有没有可能从场景大纲中得到复杂的对象? 我试图找到一些关于我的问题的文档,但没有弹出任何内容。也许我找错了地方或使用了错误的查询。
例如。
@Test(
dataProvider = "data"
)
public void sendRequestTest(
SomeComplexRequest request
) {
//Given
some test data prep
//When
Responce resp = sendRequestToService(request);
//Then
assertEquals(resp, expectedResp);
}
@Builder
class SomeComplexRequest{
dataType Field1;
dataType Field2;
dataType Field3;
...
dataType FieldN;
}
虽然可以在数据中使用示例值 table:
Feature: Basic Arithmetic
Background: A Calculator
Given a calculator I just turned on
Scenario Outline: Many additions
Given the previous entries:
| first | second | operation |
| <x> | <y> | <op1> |
| <q> | <r> | <op2> |
When I press +
And I add <a> and <b>
And I press +
Then the result is <c>
Examples: Single digits
| x | y | op1 | q | r | op2 | a | b | c |
| 1 | 1 | + | 2 | 1 | + | 1 | 2 | 8 |
| 0 | 1 | + | 1 | 2 | + | 2 | 3 | 9 |
虽然可以将此数据 table 转换为步骤定义中的对象
@Given("the previous entries:")
public void thePreviousEntries(List<Entry> entries) {
for (Entry entry : entries) {
calc.push(entry.first);
calc.push(entry.second);
calc.push(entry.operation);
}
}
通过使用 table 转换器将 table 转换为条目列表:
https://github.com/cucumber/cucumber-jvm/tree/master/java#transformers
private final ObjectMapper objectMapper = new ObjectMapper();
@DefaultParameterTransformer
@DefaultDataTableEntryTransformer
@DefaultDataTableCellTransformer
public Object transformer(Object fromValue, Type toValueType) {
return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType));
}
您可能没有以最有益的方式使用 Cucumber。使用 11 个参数,您实际上可能甚至没有以最有益的方式使用当前的单元测试。
考虑:
- 消除测试中的所有附带细节。
- 使用原型模式。这样你只需要描述对原型请求所做的更改
- 重新思考您的测试策略并重组您的测试策略,使您的黄瓜测试可以专注于高级行为,同时将细节推入更小的单元测试。