我有一种测试是有效的:
Feature: TestAddition
Scenario Outline: "Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| 3 | 4 | 7 |
| 2 | 5 | 7 |
| 1 | 4 | 5 |
这就是胶水代码:
package featuresAdditions;
import org.junit.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import math.AdditionEngine;
public class step {
private AdditionEngine testAdditionEngine;
private double resultAddition;
@Given("^A is (\\d+) and B is (\\d+)$")
public void addition(int arg1, int arg2) throws Throwable {
testAdditionEngine = new AdditionEngine();
resultAddition = testAdditionEngine.calculateAdditionAmount(arg1, arg2);
}
@Then("^A + B is (.)$")
public void addition(double arg1) throws Throwable {
Assert.assertEquals(arg1, resultAddition, 0.01);
}
}
但是,我想知道如何创建一个无效表的例子在哪里??表示我不知道在下表中填入什么内容
Examples:
| A | B | result |
| "é3-3" | 5 | ?? |
| "é3-3" | "aB" | ?? |
这应该给出一个java.lang.NumberFormatException
在纯jUnit中,我会像下面的代码一样使用@Test(expected = NumberFormatException.class)
。不过,我得用黄瓜。有人能告诉我如何使用Cucubmer执行这样的测试吗?
public class test {
AdditionEngine testAdditionEngine = new AdditionEngine();
@Test(expected = NumberFormatException.class)
public void test() {
testAdditionEngine.calculateAdditionAmount("é3-3", 5);
}
}
发布于 2018-05-28 14:48:45
Scenario Outline: "Invalid Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| "é3-3" | 5 | java.lang.NumberFormatException |
| "é3-3" | "aB" | java.lang.NumberFormatException |
更改步骤定义,以String
而不是Integer
作为参数。
private Exception excep;
@Given("^A is (.*?) and B is (.*?)$")
public void addValid(String arg1, String arg2) {
try {
testAdditionEngine = new AdditionEngine();
testAdditionEngine.calculateAdditionAmount(arg1, arg2);
} catch (NumberFormatException e) {
excep = e;
}
};
@Then("^A \\+ B is (.*?)$")
public void validResult(String arg1){
assertEquals(arg1, excep.getClass().getName());
};
如果您使用的是Cucumber 2或更高版本,则会收到不明确的步骤消息。这是因为有效的scenariooutline将匹配整数和字符串步骤定义。更改两个方案语句中的任何一个。
https://stackoverflow.com/questions/50558166
复制相似问题