SpecFlow/Cucumber/Gherkin - 在场景大纲中使用表格 [英] SpecFlow/Cucumber/Gherkin - Using tables in a scenario outline

查看:32
本文介绍了SpecFlow/Cucumber/Gherkin - 在场景大纲中使用表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望我能清楚地解释我的问题,让其他人理解,我们开始吧,假设我有以下两个假设场景:

Hopefully I can explain my issue clearly enough for others to understand, here we go, imagine I have the two following hypothetical scenarios:

Scenario: Filter sweets by king size and nut content
Given I am on the "Sweet/List" Page
When I filter sweets by 
    | Field               | Value  |
    | Filter.KingSize     | True   |
    | Filter.ContainsNuts | False  |
Then I should see :
    | Value            |
    | Yorkie King Size |
    | Mars King Size   |

Scenario: Filter sweets by make
Given I am on the "Sweet/List" Page
When I filter sweets by 
    | Field        | Value  |
    | Filter.Make  | Haribo |
Then I should see :
    | Value   |
    | Starmix |

这些场景很有用,因为我可以根据需要添加任意多的字段/值行和然后值条目,而无需更改相关的已编译测试步骤.然而,不同过滤器测试的复制/粘贴场景将变得重复并占用大量代码 - 我想避免这种情况.理想情况下,我想创建一个场景大纲并保持上面测试的动态特性,但是当我尝试这样做时,我遇到了定义示例表的问题,我无法添加我认为合适的新行,因为那将是一个新的测试实例,目前我有这个:

These scenarios are useful because I can add as many When rows of Field/Value and Then Value entries as I like without changing the associated compiled test steps. However copy/pasting scenarios for different filter tests will become repetitive and take up alot of code - something I would like to avoid. Ideally I would like to create a scenario outline and keep the dynamic nature I have with the tests above, however when I try to do that I run into a problem defining the example table I cant add new rows as I see fit because that would be a new test instance, currently I have this:

Scenario Outline: Filter Sweets 
Given I am on the <page> Page
When I filter chocolates by 
    | Field    | Value   |
    | <filter> | <value> |
Then I should see :
    | Output   |
    | <output> |
Examples:
    | page       | filter      | value  | output  |
    | Sweet/List | Filter.Make | Haribo | Starmix |

所以我遇到了在使用场景大纲时能够动态地将行添加到我的过滤器和预期数据的问题,有人知道解决这个问题的方法吗?我应该从不同的角度来解决这个问题吗?

So I have the problem of being able to dynamically add rows to my filter and expected data when using a scenario outline, is anyone aware of a way around this? Should I be approaching this from a different angle?

解决方法可能类似于:

Then I should see :
    | Output |
    | <x>    |
    | <y>    |
    | <z>    |
    Examples:
    | x | y | z |

但这不是很动态....希望有更好的解决方案?:)

But thats not very dynamic.... hoping for a better solution? :)

推荐答案

从技术上讲,我认为您可以尝试从步骤定义中调用步骤:

Technically, I think you could try calling steps from within a step definition:

从步骤定义调用步骤

例如我认为你可以重写

Then I should see :
| Output   |
| <output> |

要像自定义步骤一样

I should have output that contains <output>

其中输出是一个逗号分隔的期望值列表.在自定义步骤中,您可以将逗号分隔的列表分解为一个数组并对其进行迭代调用

Where output is a comma separated list of expected values. In the custom step you could break the comma separated list into an array and iterate over it calling

Then "I should see #{iterated_value}"

您可以使用类似的技术来传递过滤器列表和过滤器值.您的特大号测试示例行可能如下所示

You could use a similar technique to pass in lists of filters and filter values. Your example row for the king size test might look like

| page       | filter                               | value       | output                           |
| Sweet/List | Filter.KingSize, Filter.ContainsNuts | True, False | Yorkie King Size, Mars King Size |

或许

| page       |                              filter-value-pairs | output                           |
| Sweet/List | Filter.KingSize:True, Filter.ContainsNuts:False | Yorkie King Size, Mars King Size |

话虽如此,你或许应该把达伦的话牢记在心.我不确定这种方法是否有助于实现非开发人员可读的场景的最终目标.

That being said, you should perhaps take Darren's words to heart. I'm not really sure that this method would help the ultimate goal of having scenarios that are readable by non-developers.

这篇关于SpecFlow/Cucumber/Gherkin - 在场景大纲中使用表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆