是否可以将多个 given-when-then
block 添加到场景(类似于子场景)?
这是我想到的一个例子:
A sample story with a collection of scenarios
Narrative:
As a dev
In order to do work
I want multiple sub-scenarios :-)
Scenario: A sample collection scenario
Given step1...
When step1...
Then step1...
Given step2...
When step2...
Then step2...
作为一种解决方法,我可以使用多种方案,但这需要重写一些粘合代码来初始化结构(在方法之前和之后)。
有什么提示我可以如何避免这种情况吗?提前致谢!
最佳答案
是的,你可以这样做。
故事的简单示例:
A sample story with a collection of scenarios
Narrative:
As a dev
In order to do work
I want multiple sub-scenarios :-)
Scenario: A sample collection scenario
Given step 1
And step 11
When step 1
And step 11
Then step 1
And step 11
Given step 2
When step 2
Then step 2
Java代码:
public class Test extends Steps {
@Given("step 1")
public void givenStep1() {
System.out.println("Given Step 1");
}
@Given("step 11")
public void givenStep11() {
System.out.println("Given Step 11");
}
@Given("step 2")
public void givenStep2() {
System.out.println("Given Step 2");
}
@When("step $step")
public void when(String step){
System.out.println("When Step " + step);
}
@Then("step $step")
public void then(String step){
System.out.println("Then Step " + step);
}
}
和测试结果:
Running story main/resources/test.story
Given Step 1
Given Step 11
When Step 1
When Step 11
Then Step 1
Then Step 11
Given Step 2
When Step 2
Then Step 2
请注意此示例中 And
关键字的匹配方式。
当 And
在 Given
行下使用时,它被视为 Given
,如果在 Then
下方 - 那么它会被匹配为 Then
关键字等。
关于java - JBehave 子场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25011530/