使用 Cucumber 和 Scala 进行数据表测试

Datatable test with Cucumber and Scala

我正在尝试使用 Cucumber/Gherkins 和 Scala 创建一个简单的计算器应用程序,其中信息在数据表中给出,但不确定如何去做?我对 BDD 测试还很陌生,想知道其他程序员是如何克服这个问题的

我创建了一个包含其他操作(例如加法、减法、乘法和除法)的特征文件,其中值在 'when, then' 语句中提供,工作正常但不确定如何使用 Cucumber 处理数据表斯卡拉

如有任何帮助,我们将不胜感激

专题文件:

Scenario Outline: Addition
Given my calculator is running
When I add <inputOne> and <inputTwo>
Then result should be equal to <output>
Examples:
  | inputOne | inputTwo | output |
  | 20       | 30       | 50     |
  | 2        | 5        | 7      |
  | 0        | 40       | 40     |

步骤定义文件:

class CalcSteps extends ScalaDsl with EN {

var calc: MyCalc = _
var result: Int= _

Given("""^my calculator is running$""") { () =>
calc = new MyCalc 
}

When("^I add \"(.*?)\" and \"(.*?)\":$") { (firstNum: Int, secondNum: Int, values: DataTable) =>
//not sure what to do here
//result = calc.add(firstNum, secondNum)
}

Then("^result should be equal to \"(.*?)\"$") { (expectedResult: Int) =>
assert(result == expectedResult, "Incorrect result of calculator computation")

}

我的计算器:

class MyCalc {

 def add(first:Int, second: Int): Int = {
   first + second
  }
}

感谢@Grasshopper,我只需将功能文件更改为即可解决问题:

Scenario Outline: Addition
Given my calculator is running
When I add <inputOne> and <inputTwo>
Then result should be equal to <output>
Examples:
  | inputOne | inputTwo | output |
  | 20       | 30       | 50     |
  | 2        | 5        | 7      |
  | 0        | 40       | 40     |

并从步骤定义中删除 'values:Datatable' 字段,结果是:

  When("^I add \"(.*?)\" and \"(.*?)\":$") { (firstNum: Int, secondNum: Int) =>

result = calc.add(firstNum, secondNum)
}