如何从 Cucumber 场景中传递字符串列表

问题描述 投票:0回答:7

我需要传递来自黄瓜场景的字符串列表,其工作原理如下

Scenario Outline: Verify some scenario 
Given something
When user do something 
Then user should have some "<data>" 
Examples: Some example
|data|
|Test1, Test2, Test3, Test4|

在步骤定义中,我使用 List 来检索某些变量的值。 但是当数据变量的值之一包含逗号(,)时,例如Tes,t4 它变得复杂,因为它认为“Tes”和“t4”是两个不同的值

 Examples: Some example
 |something|
 |Test1, Test2, Test3, Tes,t4|  

那么有没有我可以使用的转义字符,或者有没有其他方法来处理这种情况

cucumber cucumber-jvm scenarios
7个回答
18
投票

找到了一个简单的方法。请参阅以下步骤。

  • 这是我的功能文件。

  • 这里是用代码映射特征步骤的相应代码。

  • 哦,是的。结果很重要。您可以看到调试视图。


11
投票

这应该适合你:

Scenario: Verify some scenario 
Given something
When user do something 
Then user should have following
| Test1 |
| Test2 |
| Test3 |
| Tes,t4| 

在步骤定义中

Then("^user should have following$")
 public void user_should_have_following(List<String> testData) throws Throwable {
 #TODO user your test data as desired
 }

2
投票

在TypeRegistryConfigurer的Transformer中,你可以这样做

@Override
public Object transform(String s, Type type) {
    if(StringUtils.isNotEmpty(s) && s.startsWith("[")){
        s = s.subSequence(1, s.length() - 1).toString();
        return Arrays.array(s.split(","));
    }
    return objectMapper.convertValue(s, objectMapper.constructType(type));
}

2
投票

示例:

颜色 颜色计数
红、绿 5
黄色 8
def function("{colors}"):
context.object.colors = list(colors.split(","))
for color in context.object.colors:
    print(color)

1
投票

尝试在列中设置示例,如下所示:

| data   |
| Test1  |
| Test2  |
| Test3  |
| Tes,t4 |

这将运行该场景 4 次,期望“某些内容”更改为下一个值。首先是“测试 1”,然后是“测试 2”,依此类推

在步骤定义中,您可以像这样使用该数据:

Then(/^user should have some "([^"]*)"$/) do |data|
  puts data
end

如果您想使用

|Test1, Test2, Test3, Tes,t4|
,请将“,”更改为“;”例如:
|Test1; Test2; Test3; Tes,t4|
并在步骤定义中分割数据:

data.split("; ")
导致
["test1", "test2", "test3", "te,st"]

将数据转换为列表(Java 中):

String test = "test1; test2; test3; tes,t4";
String[] myArray = test.split("; ");
List<String> myList = new ArrayList<>();
for (String str : myArray) {
    myList.add(str);
}
System.out.print(myList);

更多相关信息这里


0
投票

您需要将字符串列表从功能传递到步骤代码。好的。让我给你举个例子。这是我的功能文件:

    Feature: Verificar formas de pagamento disponíveis para o cliente

  Scenario Outline: Cliente elegível para múltiplas formas de pagamento
    Given um cliente com <tempo_cliente> meses de cadastro, situação de crédito "<situacao_credito>", valor do último pedido <valor_ultimo_pedido> e último pagamento <valor_ultimo_pagamento>
    And um pedido com valor de entrada <valor_entrada> e valor total <valor_pedido>
    When verificar as formas de pagamento disponíveis
    Then as formas de pagamento disponíveis devem ser <formas_pagamento>

    Examples:
      | tempo_cliente | situacao_credito | valor_ultimo_pedido | valor_ultimo_pagamento | valor_entrada | valor_pedido | formas_pagamento                                         |
      | 6             | boa              | 500                 | 250                    | 100           | 500          | Pagamento à vista, Pagamento em 2 vezes com juros    |
      | 12            | boa              | 1500                | 750                    | 300           | 1500         | Pagamento à vista, Pagamento em 2 vezes com juros, Pagamento em 3 vezes sem juros, Pagamento em 6 vezes sem juros |
      | 7             | regular          | 800                 | 400                    | 200           | 1000         | Pagamento à vista, Pagamento em 2 vezes com juros |

如你所见,我有一张桌子。注意Then步骤:

Then as formas de pagamento disponíveis devem ser <formas_pagamento>

这将传递表中“formas_pagamento”列的值。请注意,列值可以是一个或多个字符串。您希望在 @Then 步骤中捕获这一点。这是我的步骤定义:

@Then("^as formas de pagamento disponíveis devem ser (.*)$")
public void as_formas_de_pagamento_disponíveis_devem_ser(String resultado) {
    List<String> formasEsperadas = Arrays.asList(resultado.split("\\s*,\\s*"));
    assertEquals(formasEsperadas, formasPagamentoDisponiveis);
}

您将收到一个重复自身的正则表达式,并将其声明为字符串参数。然后,您需要拆分每个字符串并将结果数组转换为列表。


-5
投票

不要将数据放入您的场景中。你从中获益甚少,而且还会带来很多问题。相反,为您的数据命名并在场景的“Then”中使用该名称

例如

 Then the user should see something

将数据和示例放入场景中大多毫无意义。适用以下

  1. 数据将与应生成的数据重复
  2. 日期容易打错
  3. 当场景失败时,很难知道代码是错误的(它产生了错误的数据)还是场景是错误的(您输入了错误的数据)
  4. 准确表达复杂的数据真的很难
  5. 没有人真的会足够仔细地阅读您的场景以确保数据准确
© www.soinside.com 2019 - 2024. All rights reserved.