Skip to content

[Core] Add Before and AfterStep hooks #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
783828d
Adding AfterStep Feature
Feb 14, 2018
89a4c90
add tests for AfterStep
Feb 14, 2018
f870014
Adding some more tests and refactoring
Feb 15, 2018
442a036
fix JSON formatter and add test to validate afterstep
Feb 16, 2018
b98656b
Skipping AfterStep in case step fails or is skipped
Feb 17, 2018
5528374
Some code refactoring as per review comments
Feb 26, 2018
e0f3980
Updating json formatter to be in sync with ruby implementation for af…
Feb 26, 2018
f3c908c
Run after step hook until the next test step from a gherkin step
brasmusson Mar 4, 2018
3ae1cff
Represent AfterHookSteps as substeps of a PickleTestStep
mpkorstanje Mar 6, 2018
1da5039
Extract Step and HookSteps from TestStep
mpkorstanje Mar 7, 2018
f926057
Make UNDEFINED less severe then AMBIGUOUS
mpkorstanje Mar 8, 2018
3be655e
Treat before and after hooks like hooks rather then unskipable steps
mpkorstanje Mar 8, 2018
50ef0bf
Hide step implementation details from public api
mpkorstanje Mar 9, 2018
423b961
Hide cucumber.runtime.arguments from the public api
mpkorstanje Mar 9, 2018
75b59bc
Add before step hook
mpkorstanje Mar 9, 2018
c7b1cfd
Add lambda before and after step hooks
mpkorstanje Mar 9, 2018
44d4f4f
Use concrete types in runner implementation
mpkorstanje Mar 9, 2018
189aea4
Move getDefinitionArgument down to TestStep
mpkorstanje Mar 9, 2018
f6b96da
Update JSONFormatter to include BeforeStep
Mar 12, 2018
5fa4360
Add javadoc to steps and events
mpkorstanje Mar 12, 2018
5742014
Add javadoc to TestStep.get*Argument
mpkorstanje Mar 12, 2018
a2ffa0f
Nested If..else -> switch
Mar 13, 2018
34c11e9
AfterStep should be skipped if BeforeStep fails
Mar 13, 2018
c1ef976
Rename TestStep to PickleTestStep, HookStep to TestStep, Step to Test…
mpkorstanje Mar 14, 2018
e55c769
Rename TestSteps
mpkorstanje Mar 17, 2018
7e3d10d
Always invoke after step hooks when before step hooks have been invoked
mpkorstanje Mar 17, 2018
4cae4bf
Restore test step methods as deprecated methods
mpkorstanje Mar 22, 2018
5266c0e
Remove unused and unsupported methods from internal api
mpkorstanje Mar 22, 2018
6bce153
Use Step, PickleStep and Hook naming convention
mpkorstanje Mar 22, 2018
26a30d8
Tighten accessibility
mpkorstanje Mar 22, 2018
373f729
Merge branch 'master' of github.com:cucumber/cucumber-jvm into featur…
mpkorstanje Apr 1, 2018
e632250
Merge branch 'master' into feature_afterstep
mpkorstanje Apr 8, 2018
92e4fa6
[pom] Update the version of the cucumber-html dependency
brasmusson Apr 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/api/HookType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cucumber.api;

public enum HookType {
Before, After;
Before, After, AfterStep;

@Override
public String toString() {
Expand Down
23 changes: 21 additions & 2 deletions core/src/main/java/cucumber/api/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Comparator;

public class Result {

public final static Comparator<Result> SEVERITY = new Comparator<Result>() {

@Override
public int compare(Result a, Result b) {
return a.status == b.status ? 0 : a.status.ordinal() > b.status.ordinal() ? 1 : -1;
}
};

private static final long serialVersionUID = 1L;

private final Result.Type status;
private final Type status;
private final Long duration;
private final Throwable error;
public static final Result SKIPPED = new Result(Result.Type.SKIPPED, null, null);
Expand All @@ -15,8 +25,8 @@ public enum Type {
PASSED,
SKIPPED,
PENDING,
UNDEFINED,
AMBIGUOUS,
UNDEFINED,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ambiguous and Failed both unconditionally make the exit code non-zero, but Undefined does not, so Undefined must be less severe than Ambiguous.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

FAILED;

public static Type fromLowerCaseName(String lowerCaseName) {
Expand Down Expand Up @@ -85,4 +95,13 @@ private String getErrorMessage(Throwable error) {
error.printStackTrace(printWriter);
return stringWriter.getBuffer().toString();
}

@Override
public String toString() {
return "Result{" +
"status=" + status +
", duration=" + duration +
", error=" + error +
'}';
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/cucumber/api/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class TestCase {
private final PickleEvent pickleEvent;
private final List<TestStep> testSteps;
private final boolean dryRun;
public enum SkipStatus {
RUN_ALL,
SKIP_ALL_SKIPABLE
};

/**
* Creates a new instance of a test case.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package cucumber.runner;

import cucumber.api.HookType;
import cucumber.api.Result;
import cucumber.api.Scenario;
import cucumber.api.TestStep;
import cucumber.runtime.DefinitionMatch;
import gherkin.pickles.Argument;
import gherkin.pickles.PickleStep;

import java.util.List;

public class UnskipableStep extends TestStep {
public class HookStep extends TestStep {
private final HookType hookType;

public UnskipableStep(HookType hookType, DefinitionMatch definitionMatch) {
public HookStep(HookType hookType, DefinitionMatch definitionMatch) {
super(definitionMatch);
this.hookType = hookType;
}

protected Result.Type executeStep(String language, Scenario scenario, boolean skipSteps) throws Throwable {
definitionMatch.runStep(language, scenario);
return Result.Type.PASSED;
}

@Override
public boolean isHook() {
return true;
Expand Down Expand Up @@ -57,4 +50,5 @@ public List<Argument> getStepArgument() {
public HookType getHookType() {
return hookType;
}

}
31 changes: 29 additions & 2 deletions core/src/main/java/cucumber/runner/PickleTestStep.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
package cucumber.runner;

import cucumber.api.HookType;
import cucumber.api.Result;
import cucumber.api.Scenario;
import cucumber.api.TestStep;
import cucumber.runtime.DefinitionMatch;
import cucumber.runtime.StepDefinitionMatch;
import gherkin.pickles.Argument;
import gherkin.pickles.PickleStep;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static cucumber.api.Result.SEVERITY;
import static java.util.Collections.max;

public class PickleTestStep extends TestStep {
private String uri;
private PickleStep step;
private final String uri;
private final PickleStep step;
private final List<TestStep> afterStepHooks;

public PickleTestStep(String uri, PickleStep step, DefinitionMatch definitionMatch) {
this(uri, step, Collections.<TestStep>emptyList(), definitionMatch);
}

public PickleTestStep(String uri, PickleStep step, List<TestStep> afterStepHooks, DefinitionMatch definitionMatch) {
super(definitionMatch);
this.uri = uri;
this.step = step;
this.afterStepHooks = afterStepHooks;
}

@Override
public Result run(EventBus bus, String language, Scenario scenario, boolean skipSteps) {
List<Result> results = new ArrayList<Result>();

results.add(super.run(bus, language, scenario, skipSteps));

for(TestStep after : afterStepHooks){
results.add(after.run(bus, language, scenario, skipSteps));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method call should also accept param skipNextStep instead of skipSteps. Currently if skipNextStep evaluates to true after a failed BeforeStep, the step is skipped but AfterStep still executes

}

return max(results, SEVERITY);
}


@Override
public boolean isHook() {
return false;
Expand Down
26 changes: 21 additions & 5 deletions core/src/main/java/cucumber/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,43 @@ private void addTestStepsForPickleSteps(List<TestStep> testSteps, PickleEvent pi
} catch (Throwable t) {
match = new FailedStepInstantiationMatch(pickleEvent.uri, step, t);
}
testSteps.add(new PickleTestStep(pickleEvent.uri, step, match));


List<TestStep> afterStepHooks = getAfterStepHooks(pickleEvent.pickle.getTags());
testSteps.add(new PickleTestStep(pickleEvent.uri, step, afterStepHooks, match));
}
}

private void addTestStepsForBeforeHooks(List<TestStep> testSteps, List<PickleTag> tags) {
addTestStepsForHooks(testSteps, tags, glue.getBeforeHooks(), HookType.Before);
addTestStepsForScenarioHooks(testSteps, tags, glue.getBeforeHooks(), HookType.Before);
}

private void addTestStepsForAfterHooks(List<TestStep> testSteps, List<PickleTag> tags) {
addTestStepsForHooks(testSteps, tags, glue.getAfterHooks(), HookType.After);
addTestStepsForScenarioHooks(testSteps, tags, glue.getAfterHooks(), HookType.After);
}

private void addTestStepsForHooks(List<TestStep> testSteps, List<PickleTag> tags, List<HookDefinition> hooks, HookType hookType) {
private void addTestStepsForScenarioHooks(List<TestStep> testSteps, List<PickleTag> tags, List<HookDefinition> hooks, HookType hookType) {
for (HookDefinition hook : hooks) {
if (hook.matches(tags)) {
TestStep testStep = new UnskipableStep(hookType, new HookDefinitionMatch(hook));
TestStep testStep = new UnskipableHookStep(hookType, new HookDefinitionMatch(hook));
testSteps.add(testStep);
}
}
}

private List<TestStep> getAfterStepHooks(List<PickleTag> tags) {
List<TestStep> hooks = new ArrayList<TestStep>();

for (HookDefinition hook : glue.getAfterStepHooks()) {
if (hook.matches(tags)) {
TestStep testStep = new HookStep(HookType.AfterStep, new HookDefinitionMatch(hook));
hooks.add(testStep);
}
}

return hooks;
}

private void buildBackendWorlds() {
runtimeOptions.getPlugins(); // To make sure that the plugins are instantiated after
// the features have been parsed but before the pickles starts to execute.
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/cucumber/runner/UnskipableHookStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cucumber.runner;

import cucumber.api.HookType;
import cucumber.api.Result;
import cucumber.api.Result.Type;
import cucumber.api.Scenario;
import cucumber.api.TestCase.SkipStatus;
import cucumber.runtime.DefinitionMatch;

public class UnskipableHookStep extends HookStep {

public UnskipableHookStep(HookType hookType, DefinitionMatch definitionMatch) {
super(hookType, definitionMatch);
}

@Override
protected Type executeStep(String language, Scenario scenario, boolean skipSteps) throws Throwable {
definitionMatch.runStep(language, scenario);
return Result.Type.PASSED;
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/cucumber/runtime/Glue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public interface Glue {

void addAfterHook(HookDefinition hookDefinition);

void addAfterStepHook(HookDefinition hookDefinition);

List<HookDefinition> getBeforeHooks();

List<HookDefinition> getAfterHooks();

List<HookDefinition> getAfterStepHooks();

StepDefinitionMatch stepDefinitionMatch(String featurePath, PickleStep step);

void reportStepDefinitions(StepDefinitionReporter stepDefinitionReporter);
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/cucumber/runtime/RuntimeGlue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class RuntimeGlue implements Glue {
final Map<String, StepDefinition> stepDefinitionsByPattern = new TreeMap<String, StepDefinition>();
final List<HookDefinition> beforeHooks = new ArrayList<HookDefinition>();
final List<HookDefinition> afterHooks = new ArrayList<HookDefinition>();
final List<HookDefinition> afterStepHooks = new ArrayList<HookDefinition>();
final Map<String, CacheEntry> matchedStepDefinitionsCache = new HashMap<String, CacheEntry>();
private final LocalizedXStreams localizedXStreams;

Expand Down Expand Up @@ -49,6 +50,12 @@ public void addAfterHook(HookDefinition hookDefinition) {
Collections.sort(afterHooks, new HookComparator(false));
}

@Override
public void addAfterStepHook(HookDefinition hookDefinition) {
afterStepHooks.add(hookDefinition);
Collections.sort(afterStepHooks, new HookComparator(false));
}

@Override
public List<HookDefinition> getBeforeHooks() {
return beforeHooks;
Expand All @@ -59,6 +66,11 @@ public List<HookDefinition> getAfterHooks() {
return afterHooks;
}

@Override
public List<HookDefinition> getAfterStepHooks() {
return afterStepHooks;
}

@Override
public StepDefinitionMatch stepDefinitionMatch(String featurePath, PickleStep step) {
String stepText = step.getText();
Expand Down
24 changes: 8 additions & 16 deletions core/src/main/java/cucumber/runtime/ScenarioImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Collections.max;

public class ScenarioImpl implements Scenario {
private static final List<Result.Type> SEVERITY = asList(Result.Type.PASSED, Result.Type.SKIPPED, Result.Type.PENDING, Result.Type.UNDEFINED, Result.Type.AMBIGUOUS, Result.Type.FAILED);

private final List<Result> stepResults = new ArrayList<Result>();
private final List<PickleTag> tags;
private final String uri;
Expand Down Expand Up @@ -63,11 +63,8 @@ public Result.Type getStatus() {
if (stepResults.isEmpty()) {
return Result.Type.UNDEFINED;
}
int pos = 0;
for (Result stepResult : stepResults) {
pos = Math.max(pos, SEVERITY.indexOf(stepResult.getStatus()));
}
return SEVERITY.get(pos);

return max(stepResults, Result.SEVERITY).getStatus();
}

@Override
Expand Down Expand Up @@ -110,15 +107,10 @@ public List<Integer> getLines() {
}

public Throwable getError() {
Throwable error = null;
int maxPos = 0;
for (Result stepResult : stepResults) {
int currentPos = SEVERITY.indexOf(stepResult.getStatus());
if (currentPos > maxPos) {
maxPos = currentPos;
error = stepResult.getError();
}
if(stepResults.isEmpty()){
return null;
}
return error;

return max(stepResults, Result.SEVERITY).getError();
}
}
21 changes: 18 additions & 3 deletions core/src/main/java/cucumber/runtime/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,25 @@ private Map<String, Object> createHookStep(TestStep testStep) {
}

private void addHookStepToTestCaseMap(Map<String, Object> currentStepOrHookMap, HookType hookType) {
if (!currentTestCaseMap.containsKey(hookType.toString())) {
currentTestCaseMap.put(hookType.toString(), new ArrayList<Map<String, Object>>());
String hookName;
if (hookType.toString().contains("after"))
hookName = "after";
else
hookName = "before";

Map<String, Object> mapToAddTo;
if (hookType == HookType.After || hookType == HookType.Before) {
mapToAddTo = currentTestCaseMap;
}
else {
//get latest added step
mapToAddTo = currentStepsList.get(currentStepsList.size() - 1);
}

if (!mapToAddTo.containsKey(hookName)) {
mapToAddTo.put(hookName, new ArrayList<Map<String, Object>>());
}
((List<Map<String, Object>>)currentTestCaseMap.get(hookType.toString())).add(currentStepOrHookMap);
((List<Map<String, Object>>)mapToAddTo.get(hookName)).add(currentStepOrHookMap);
}

private void addOutputToHookMap(String text) {
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/java/cucumber/api/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@

import org.junit.Test;

import java.util.List;

import static cucumber.api.Result.SEVERITY;
import static java.util.Arrays.asList;
import static java.util.Collections.sort;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class ResultTest {

@Test
public void severity_from_low_to_high_is_passed_skipped_pending_undefined_ambiguous_failed() {
Result passed = new Result(Result.Type.PASSED, null, null);
Result skipped = new Result(Result.Type.SKIPPED, null, null);
Result pending = new Result(Result.Type.PENDING, null, null);
Result undefined = new Result(Result.Type.UNDEFINED, null, null);
Result ambiguous = new Result(Result.Type.AMBIGUOUS, null, null);
Result failed = new Result(Result.Type.FAILED, null, null);

List<Result> results = asList(pending, passed, skipped, failed, ambiguous, undefined);

sort(results, SEVERITY);

assertThat(results, equalTo(asList(passed, skipped, pending, ambiguous, undefined, failed)));
}

@Test
public void passed_result_is_always_ok() {
Result passedResult = new Result(Result.Type.PASSED, null, null);
Expand Down
Loading