Elements of scenario development

Below are links to selected parts of the reference guide which deal with scenario development:

Refer to the reference guide menu for a more exhaustive selection of topics.

Skipping and Ignoring failures of scenarios

It may be useful at times to skip running scenarios or complete all scenarios ignoring any failures that may occur. It is possible to control the behaviour via the flags:


  • skip: defaults to false

  • ignoreFailure: defaults to false


If you are using Maven, it is strongly advised to use profiles to configure these behaviours:

  <profiles>
    <profile>
      <id>skip-scenarios</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-maven-plugin</artifactId>
            <configuration>
              <skip>true</skip>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>ignore-failure-scenarios</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-maven-plugin</artifactId>
            <configuration>
              <ignoreFailure>true</ignoreFailure>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

and run with

mvn clean install -Pskip-scenarios
or
mvn clean install -Pignore-failure-scenarios
.

@BeforeScenario and @AfterScenario

Often it is useful to be able to set up the scenario environment before running it and and tearing it down after having run it.

JBehave allows scenario developers to do so using annotations: BeforeScenario and AfterScenario.

Let’s walk through an example, e.g. the noughtsandcrosses one. First, let’s define our Steps instance that will be responsible for managing the before and after scenario behaviour:

public class BeforeAndAfterSteps extends Steps {
 
    private final OAndXUniverse universe;
 
    public BeforeAndAfterSteps(OAndXUniverse universe) {
        this.universe = universe;
    }
 
    @BeforeScenario
    public void runThisBeforeScenarios() throws Exception {
    	universe.reset();
    }
 
    @AfterScenario
    public void runThisAfterScenarios() throws Exception {
    	universe.destroy();
    }
}

Then, let’s added it the Steps being passed to the Scenario:

public PlayersCanHazTurns(final ClassLoader classLoader, OAndXUniverse universe) {
  super(new MostUsefulConfiguration() {
    public KeyWords keywords() {
      return new KeyWords("I can haz", "Gief", "Wen", "Den", "And");
    }
    public ClasspathScenarioDefiner forDefiningScenarios() {
      return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(), new PatternScenarioParser(this),
                        classLoader);
    }
  }, new LolCatzSteps(universe), new BeforeAndAfterSteps(universe));
}

Finally, just run scenario and the methods annotated with @BeforeScenario and @AfterScenario will be performed before and after each scenario.

NOTE: the before/after steps are not required to be in a separate Steps instance, but we recommend this separation so it can be reused by several scenarios.

AfterScenario Outcome

The @AfterScenario annotation has an optional Outcome parameter that allows the developer to declare if the method should be performed, depending on the Outcome. By default it is performed upon any outcome, but it can be executed only upon success or failure:

public class BeforeAndAfterSteps extends Steps {
 
    private final OAndXUniverse universe;
 
    public BeforeAndAfterSteps(OAndXUniverse universe) {
        this.universe = universe;
    }
 
    @BeforeScenario
    public void runThisBeforeScenarios() throws Exception {
    	universe.reset();
    }
 
    @AfterScenario(uponOutcome=AfterScenario.Outcome.SUCCESS)
    public void runThisAfterSuccessfulScenarios() throws Exception {
    	universe.destroy();
    }
 
    @AfterScenario(uponOutcome=AfterScenario.Outcome.FAILURE)
    public void runThisAfterFailedScenarios() throws Exception {
    	universe.destroy();
        // may want to do some other cleanup action here
    }
}

Trackbacks/Pingbacks

  1. [...] The Scenario class is a JUnit test. You can right-click and run it in most new IDEs, or use a build script that runs JUnit tests. You can also run scenarios as Ant tasks or Maven goals. Learn more about scenarios [...]



Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word