【发布时间】:2019-04-04 17:42:37
【问题描述】:
我目前正在学习黄瓜,在非常简单的测试中,我有一些疑问: "如何组织我的 StepClasses 的最佳方式。
这是我的 .feature:
Feature: How many potatoes have in the sack
Scenario: I put one potato in the Bag
Given the bag has 10 potatoes
When I put 1 potato
Then I should be told 11 potatoes
Scenario: I remove one potato from the Bag
Given the bag has 10 potatoes
When I remove 1 potato
Then I should be told 9 potatoes
还有我的 StepClass:
公共类 Stepdefs {
private Integer potatoesInTheBag;
@Given("^the bag has 10 potatoes$")
public void the_bag_has_10_potatoes(){
this.potatoesInTheBag=10;
}
@When("^I put 1 potato$")
public void i_put_one_potato(){
this.potatoesInTheBag = potatoesInTheBag + 1;
}
@Then("^I should be told (\\d+) potatoes$")
public void i_should_be_told_potatoes(int potatoes) throws Exception {
assertEquals(potatoesInTheBag.intValue(),potatoes);
}
@When("^I remove 1 potato$")
public void i_remove_one_potato(){
this.potatoesInTheBag = potatoesInTheBag - 1;
}
}
这个例子可以正常工作,但是 i_remove_one_potato() 应该留在这里,还是留在另一个步骤类中? 另一个问题,如果我想使用场景大纲,在这种情况下我会怎么做?因为尽管添加/删除的土豆是相同的,但答案会有所不同。 有一些好的做法可以指导构建黄瓜测试的过程吗?
谢谢
【问题讨论】: