Skip to content

Commit

Permalink
Fix behavior with @BeforeScenario and @AfterScenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel.Smrcek committed Sep 27, 2023
1 parent 5c2d788 commit d0b3cbe
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ In the IDE reporting is shown:
| 4.8.0 | 4.8 |
| 4.8.3 | 4.8.3 |
| 5.0.0 | 5.0 |
| 5.0.1 | 5.0 |
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.failures.PendingStepFound;
import org.jbehave.core.failures.UUIDExceptionWrapper;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Step;
import org.jbehave.core.model.Story;
Expand Down Expand Up @@ -57,6 +58,10 @@ public class StepLoggingReporter extends AbstractLoggingReporter {
private Deque<TestDescriptor> currentStepDescriptor = new ArrayDeque<>();

private boolean isInBeforeStories = false;

private boolean isInBeforeScenario = false;

private boolean isInAfterScenario = false;
private boolean isInAfterStories = false;
private boolean isInMainScenario = false;

Expand Down Expand Up @@ -170,6 +175,32 @@ public void beforeScenario(Scenario scenario) {
}
}

@Override
public void beforeScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @BeforeScenario steps are executed between cycle SYSTEM and stage BEFORE and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.BEFORE) {
isInBeforeScenario = true;
} else {
isInBeforeScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

@Override
public void afterScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @AfterScenario steps are executed between cycle USER and stage AFTER and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.USER && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = true;
} else if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

private List<TestDescriptor> getAllExamples(Set<? extends TestDescriptor> children) {
List<TestDescriptor> result = new ArrayList<>();
for (TestDescriptor child : children) {
Expand Down Expand Up @@ -201,7 +232,7 @@ private List<TestDescriptor> getAllChildren(Set<? extends TestDescriptor> childr
@Override
public void afterScenario(Timing timing) {
super.afterScenario(timing);
if (shouldReportStep()) {
if (notAGivenStory() && (!isInBeforeStories || !isInAfterStories)) {
engineExecutionListener.executionFinished(currentScenarioDescriptor, TestExecutionResult.successful());
// main scenario starts before given stories are run,
// so we need to handle the case of afterScenario of given story
Expand Down Expand Up @@ -290,9 +321,12 @@ public void ignorable(String step) {
private boolean shouldReportStep() {
// not a given story
// not in before stories or after stories
// not in before scenario or after scenario
// and is in scenario of the main story (e.g. not some custom before story hook on method or something like that)
return notAGivenStory()
&& (!isInBeforeStories || !isInAfterStories)
&& !isInBeforeScenario
&& !isInAfterScenario
&& isInMainScenario;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.failures.UUIDExceptionWrapper;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Step;
import org.jbehave.core.model.Story;
Expand Down Expand Up @@ -53,6 +54,10 @@ public class JUnitStepReporter extends AbstractJUnitReporter {
private Deque<Description> currentStepDescription = new ArrayDeque<>();

private boolean isInBeforeStories = false;

private boolean isInBeforeScenario = false;

private boolean isInAfterScenario = false;
private boolean isInAfterStories = false;
private boolean isInMainScenario = false;

Expand Down Expand Up @@ -165,6 +170,32 @@ public void beforeScenario(Scenario scenario) {
}
}

@Override
public void beforeScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @BeforeScenario steps are executed between cycle SYSTEM and stage BEFORE and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.BEFORE) {
isInBeforeScenario = true;
} else {
isInBeforeScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

@Override
public void afterScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @AfterScenario steps are executed between cycle USER and stage AFTER and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.USER && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = true;
} else if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

private List<Description> getAllExamples(ArrayList<Description> children) {
List<Description> result = new ArrayList<>();
for (Description child : children) {
Expand Down Expand Up @@ -196,7 +227,7 @@ private List<Description> getAllChildren(ArrayList<Description> children, List<D
@Override
public void afterScenario(Timing timing) {
super.afterScenario(timing);
if (shouldReportStep()) {
if (notAGivenStory() && (!isInBeforeStories || !isInAfterStories)) {
notifier.fireTestFinished(currentScenarioDescription);
// main scenario starts before given stories are run,
// so we need to handle the case of afterScenario of given story
Expand Down Expand Up @@ -285,9 +316,12 @@ public void ignorable(String step) {
private boolean shouldReportStep() {
// not a given story
// not in before stories or after stories
// not in before scenario or after scenario
// and is in scenario of the main story (e.g. not some custom before story hook on method or something like that)
return notAGivenStory()
&& (!isInBeforeStories || !isInAfterStories)
&& !isInBeforeScenario
&& !isInAfterScenario
&& isInMainScenario;
}

Expand Down
43 changes: 37 additions & 6 deletions src/test/java/org/jbehavesupport/runner/story/steps/TestSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
*/
package org.jbehavesupport.runner.story.steps;

import org.jbehave.core.annotations.BeforeStory;
import org.jbehave.core.annotations.Composite;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.annotations.*;
import org.jbehave.core.model.ExamplesTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,10 +30,45 @@
public class TestSteps {

@BeforeStory
public void before() {
public void beforeStory() {
logger.info("Before story custom step");
}

@BeforeStories
public void beforeStories() {
logger.info("Before Stories custom step");
}

@BeforeScenario
public void beforeScenario() {
logger.info("Before scenario custom step");
}

@BeforeScenario
public void beforeScenario2() {
logger.info("Second before scenario custom step");
}

@AfterScenario
public void afterScenario() {
logger.info("After scenario custom step");
}

@AfterScenario
public void afterScenario2() {
logger.info("Second after scenario custom step");
}

@AfterStory
public void afterStory() {
logger.info("After story custom step");
}

@AfterStories
public void afterStories() {
logger.info("After stories custom step");
}

private static final Logger logger = LoggerFactory.getLogger(TestSteps.class);

@Given("say Hello")
Expand Down

0 comments on commit d0b3cbe

Please sign in to comment.