我正在尝试使用Cucumber / Gherkins和Scala创建一个简单的计算器应用程序,其中的信息是在数据表中给出但不确定如何进行此操作?我对BDD测试很陌生,想知道其他程序员如何克服这个问题
我已经创建了一个包含其他操作的特征文件,例如加法,减法,乘法和除法,其中值在'when,then'语句中提供,这些语句工作正常但不确定如何使用Scala处理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")
}
MyCalc:
class MyCalc {
def add(first:Int, second: Int): Int = {
first + second
}
}
答案 0 :(得分:2)
感谢@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)
}