Skip to content
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

[Core] Add Before and AfterStep hooks #1323

Merged
merged 33 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cucumber.runtime.formatter;

import android.util.Log;
import cucumber.api.PickleStepTestStep;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseStarted;
Expand Down Expand Up @@ -40,8 +41,9 @@ public void receive(TestCaseStarted event) {
private final EventHandler<TestStepStarted> testStepStartedHandler = new EventHandler<TestStepStarted>() {
@Override
public void receive(TestStepStarted event) {
if (!event.testStep.isHook()) {
Log.d(logTag, String.format("%s", event.testStep.getStepText()));
if (event.testStep instanceof PickleStepTestStep) {
PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
Log.d(logTag, String.format("%s", testStep.getStepText()));
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-html</artifactId>
</dependency>
<dependency>
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/cucumber/api/Argument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cucumber.api;

public interface Argument {
Integer getOffset();

String getVal();
}
19 changes: 19 additions & 0 deletions core/src/main/java/cucumber/api/HookTestStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cucumber.api;

/**
* Hooks are invoked before and after each scenario and before and
* after each gherkin step in a scenario.
*
* @see cucumber.api.event.TestCaseStarted
* @see cucumber.api.event.TestCaseFinished
*/
public interface HookTestStep extends TestStep {

/**
* Returns the hook hook type.
*
* @return the hook type.
*/
HookType getHookType();

}
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, BeforeStep, AfterStep;

@Override
public String toString() {
Expand Down
65 changes: 65 additions & 0 deletions core/src/main/java/cucumber/api/PickleStepTestStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cucumber.api;

import java.util.List;

/**
* A pickle test step matches a line in a Gherkin scenario or background.
*/
public interface PickleStepTestStep extends TestStep {

/**
* The pattern or expression used to match the glue code to the Gherkin step.
*
* @return a pattern or expression
*/
String getPattern();

/**
* The matched Gherkin step as a compiled Pickle
*
* @return the matched step
*/
gherkin.pickles.PickleStep getPickleStep();


/**
* Returns the arguments provided to the step definition.
*
* For example the step definition <code>Given (.*) pickles</code>
* when matched with <code>Given 15 pickles</code> will receive
* as argument <code>"15"</code>
*
* @return argument provided to the step definition
*/
List<cucumber.api.Argument> getDefinitionArgument();

/**
* Returns arguments provided to the Gherkin step. E.g:
* a data table or doc string.
*
* @return arguments provided to the gherkin step.
*/

List<gherkin.pickles.Argument> getStepArgument();

/**
* The line in the feature file defining this step.
*
* @return a line number
*/
int getStepLine();

/**
* A uri to to the feature and line of this step.
*
* @return a uri
*/
String getStepLocation();

/**
* The full text of the Gherkin step.
*
* @return the step text
*/
String getStepText();
}
21 changes: 20 additions & 1 deletion 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 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 +
'}';
}
}
90 changes: 7 additions & 83 deletions core/src/main/java/cucumber/api/TestCase.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,19 @@
package cucumber.api;

import cucumber.api.event.TestCaseFinished;
import cucumber.api.event.TestCaseStarted;
import cucumber.runner.EventBus;
import cucumber.runtime.ScenarioImpl;
import gherkin.events.PickleEvent;
import gherkin.pickles.PickleLocation;
import gherkin.pickles.PickleTag;

import java.util.List;

public class TestCase {
private final PickleEvent pickleEvent;
private final List<TestStep> testSteps;
private final boolean dryRun;
public interface TestCase {
int getLine();

/**
* Creates a new instance of a test case.
*
* @param testSteps of the test case
* @param pickleEvent the pickle executed by this test case
* @deprecated not part of the public api
*/
@Deprecated
public TestCase(List<TestStep> testSteps, PickleEvent pickleEvent) {
this(testSteps, pickleEvent, false);
}
String getName();

/**
* Creates a new instance of a test case.
*
* @param testSteps of the test case
* @param pickleEvent the pickle executed by this test case
* @param dryRun skip execution of the test steps
* @deprecated not part of the public api
*/
@Deprecated
public TestCase(List<TestStep> testSteps, PickleEvent pickleEvent, boolean dryRun) {
this.testSteps = testSteps;
this.pickleEvent = pickleEvent;
this.dryRun = dryRun;
}
String getScenarioDesignation();

/**
* Executes the test case.
*
* @param bus to which events should be broadcast
* @deprecated not part of the public api
*/
@Deprecated
public void run(EventBus bus) {
boolean skipNextStep = this.dryRun;
Long startTime = bus.getTime();
bus.send(new TestCaseStarted(startTime, this));
ScenarioImpl scenarioResult = new ScenarioImpl(bus, pickleEvent);
for (TestStep step : testSteps) {
Result stepResult = step.run(bus, pickleEvent.pickle.getLanguage(), scenarioResult, skipNextStep);
if (!stepResult.is(Result.Type.PASSED)) {
skipNextStep = true;
}
scenarioResult.add(stepResult);
}
Long stopTime = bus.getTime();
bus.send(new TestCaseFinished(stopTime, this, new Result(scenarioResult.getStatus(), stopTime - startTime, scenarioResult.getError())));
}
List<PickleTag> getTags();

public List<TestStep> getTestSteps() {
return testSteps;
}
List<TestStep> getTestSteps();

public String getName() {
return pickleEvent.pickle.getName();
}

public String getScenarioDesignation() {
return fileColonLine(pickleEvent.pickle.getLocations().get(0)) + " # " + getName();
}

public String getUri() {
return pickleEvent.uri;
}

public int getLine() {
return pickleEvent.pickle.getLocations().get(0).getLine();
}

private String fileColonLine(PickleLocation location) {
return pickleEvent.uri + ":" + location.getLine();
}

public List<PickleTag> getTags() {
return pickleEvent.pickle.getTags();
}
String getUri();
}
Loading