Skip to content

Commit

Permalink
Merge branch 'master' into discovery-abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
paveljandejsek authored Dec 22, 2023
2 parents 46deddf + 2a6cbc0 commit 31eacd6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 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,8 @@ 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 +173,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 +230,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 +319,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,8 @@ 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 +168,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 +225,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 +314,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
44 changes: 42 additions & 2 deletions src/test/java/org/jbehavesupport/runner/story/steps/TestSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
package org.jbehavesupport.runner.story.steps;

import org.jbehave.core.annotations.AfterScenario;
import org.jbehave.core.annotations.AfterStories;
import org.jbehave.core.annotations.AfterStory;
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.BeforeStories;
import org.jbehave.core.annotations.BeforeStory;
import org.jbehave.core.annotations.Composite;
import org.jbehave.core.annotations.Given;
Expand All @@ -33,12 +38,47 @@
*/
public class TestSteps {

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

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

private static final Logger logger = LoggerFactory.getLogger(TestSteps.class);
@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");
}

@Given("say Hello")
public void sayHello() {
Expand Down

0 comments on commit 31eacd6

Please sign in to comment.