gpt4 book ai didi

java - 数量与 Java Cucumber 数据表不匹配

转载 作者:行者123 更新时间:2023-12-02 02:06:17 32 4
gpt4 key购买 nike

我一直在使用 Jakub Czeczótka's great blog 学习 Cucumber for Java 。但他在描述数据表时迷失了我。
我做了一个带有加法、减法、乘法和除法的计算器类。这些方法中的每一种的单独测试都通过了。当我尝试使用表时,我收到来自 Gherkin 的参数不匹配错误:

cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'com.test.bdd.steps.CalculatorSteps.iWantToPerformADataTableCalculation(DataTable) in file: .../Code/Java/CucumberTest/target/test-classes/' with pattern [^I want to perform a data table calculation$] is declared with 1 parameters. However, the gherkin step has 0 arguments [].

以下是相关文件和路径:

src/main/java/com/test/bdd/calculator/
package com.test.bdd.calculator;
public class Calculator
{

private int result;
public void divide( int a, int b )
{
result = a / b;
}
public int getResult()
{
return result;
}
}


src/test/resources/cucumber/
Scenario: Division
Given I want to perform a calculation
When I divide 14 by 2
Then the result should be 7

Scenario Outline: Division Data Table
Given I want to perform a data table calculation
When I divide <Numerator> by <Divisor>
Then the result should be <Result>
Examples:
| Numerator | Divisor | Result |
| 100 | 2 | 50 |
| 100 | 4 | 25 |
| 1000 | 200 | 5 |


src/test/java/com/test/bdd/steps/
package com.test.bdd.steps;
public class CalculatorSteps
{
private Calculator calculator;
@Before
public void setUp()
{
calculator = new Calculator();
}

@When( "^I divide <Numerator> by <Divisor>$" )
public void iDivideNumeratorByDivisor()
{
// Write code here that turns the phrase above into concrete actions
calculator.divide( 4, 3 );
}


@Then( "^the result should be <Result>$" )
public void theResultShouldBeResult()
{
// Write code here that turns the phrase above into concrete actions
assertEquals( 1, calculator.getResult() );
}


@Given( "^I want to perform a data table calculation$" )
public void iWantToPerformADataTableCalculation( DataTable table )
{
if( table != null )
{
for ( Map<String, Integer> map : table.asMaps( String.class, Integer.class ) )
{
Integer numerator = map.get( "Numerator" );
Integer divisor = map.get( "Divisor" );
Integer result = map.get( "Result" );
System.out.println( format( "Dividing %d by %d yields %d", numerator, divisor, result ) );
}
}
}
}


package com.test.bdd.runner;
@RunWith( Cucumber.class )
@CucumberOptions(
glue = "com.test.bdd.steps",
features = "classpath:cucumber/calculator.feature" )


public class RunCalculatorTests
{
}

所以我的 table 似乎没有到达小 cucumber 。我认为我的 map 也有问题,但当我解决数量问题时会处理这个问题。
我需要做什么来解决这种数量不匹配的问题?

最佳答案

功能文件中没有该场景步骤的数据表。无需向此步骤定义添加 DataTable 参数。

这个示例似乎强调了 ScenarioOutline 而不是 DataTable 的使用。

ScenarioOutline 定义了一组步骤,到目前为止与场景类似。现在可以使用示例表中的数据重复执行这些步骤。该表的每一行对应于所描述步骤的一次执行。因此,如果您有 3 行,则会运行 3 次。

另一方面,DataTable 用于将数据作为一种列表发送到场景的特定步骤。就像购物 list 一样。这仅运行一次。虽然它也可以在 ScenarioOutline 中使用,从而允许您使用变量列表

关于java - 数量与 Java Cucumber 数据表不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50750682/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com