【问题标题】:JBehave sub-scenarios?JBehave子场景?
【发布时间】:2014-07-29 08:55:09
【问题描述】:

是否可以将多个 given-when-then 块添加到场景(类似于子场景)?

这是我想到的一个例子:

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...

作为一种解决方法,我可以使用多种方案,但这需要重写一些胶水代码来初始化结构(方法之前和之后)。

任何提示我该如何避免这种情况?提前致谢!

【问题讨论】:

    标签: java jbehave


    【解决方案1】:

    是的,你可以这样做。

    故事的简单例子:

    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 关键字是如何匹配的。
    AndGiven 行下使用时,它被视为Given,如果在Then 以下 - 则它被匹配为Then 关键字等。

    【讨论】:

    • 哇,谢谢!会尽快检查的!不知道为什么它对我不起作用,让我们再给一次机会:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多