Skip to content

Skip test step execution if --dry-run is specified #1220

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 3 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions core/src/main/java/cucumber/api/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
public class TestCase {
private final PickleEvent pickleEvent;
private final List<TestStep> testSteps;
private final boolean dryRun;

public TestCase(List<TestStep> testSteps, PickleEvent pickleEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Because this is an api package, should the ctor with the current signature be kept for backward compatibility? @mpkorstanje WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Along with adding a deprecation tag that would be the correct course of of action.

In the long run (and I'm only just starting to think about this) I'd like TestCase to be an interface so we can exclude the constructors and run method from the public api. These methods aren't needed as part of the event bus protocol.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we push this as a extra commit on this feature branch so we don't have to bother Adrian with this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't. Permissions have apparently not been set.

Copy link
Contributor

@mpkorstanje mpkorstanje Sep 12, 2017

Choose a reason for hiding this comment

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

I've pushed my suggested changes to #1223. I reckon we can finish up this discussion the PR for that branch. I've got no further objections to this branch being merged.

Copy link
Author

Choose a reason for hiding this comment

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

Great, thanks. Yes wasn't comfortable with having to change an api package, but in the current state didn't seem avoidable.

public TestCase(List<TestStep> testSteps, PickleEvent pickleEvent, boolean dryRun) {
this.testSteps = testSteps;
this.pickleEvent = pickleEvent;
this.dryRun = dryRun;
}

public void run(EventBus bus) {
boolean skipNextStep = false;
boolean skipNextStep = this.dryRun;
Long startTime = bus.getTime();
bus.send(new TestCaseStarted(startTime, this));
ScenarioImpl scenarioResult = new ScenarioImpl(bus, pickleEvent);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private TestCase createTestCaseForPickle(PickleEvent pickleEvent) {
if (!runtimeOptions.isDryRun()) {
addTestStepsForAfterHooks(testSteps, pickleEvent.pickle.getTags());
}
return new TestCase(testSteps, pickleEvent);
return new TestCase(testSteps, pickleEvent, runtimeOptions.isDryRun());
}

private void addTestStepsForPickleSteps(List<TestStep> testSteps, PickleEvent pickleEvent) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/cucumber/api/TestCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void run_wraps_execute_in_test_case_started_and_finished_events() throws
TestStep testStep = mock(TestStep.class);
when(testStep.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.UNDEFINED));

TestCase testCase = new TestCase(Arrays.asList(testStep), pickleEvent());
TestCase testCase = new TestCase(Arrays.asList(testStep), pickleEvent(), false);
testCase.run(bus);

InOrder order = inOrder(bus, testStep);
Expand All @@ -50,7 +50,7 @@ public void run_all_steps() throws Throwable {
TestStep testStep2 = mock(TestStep.class);
when(testStep2.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.PASSED));

TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent());
TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent(), false);
testCase.run(bus);

InOrder order = inOrder(testStep1, testStep2);
Expand All @@ -67,7 +67,7 @@ public void skip_steps_after_the_first_non_passed_result() throws Throwable {
TestStep testStep2 = mock(TestStep.class);
when(testStep2.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.SKIPPED));

TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent());
TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent(), false);
testCase.run(bus);

InOrder order = inOrder(testStep1, testStep2);
Expand Down
21 changes: 18 additions & 3 deletions core/src/test/java/cucumber/runner/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RunnerTest {
private final Backend backend = mock(Backend.class);
private final Runtime runtime = createRuntime(backend);
private final Runner runner = runtime.getRunner();

@Test
public void hooks_execute_when_world_exist() throws Throwable {
HookDefinition beforeHook = addBeforeHook(runtime);
Expand All @@ -61,7 +61,7 @@ public void steps_are_skipped_after_failure() throws Throwable {
doThrow(RuntimeException.class).when(failingBeforeHook).execute(Matchers.<Scenario>any());
StepDefinition stepDefinition = mock(StepDefinition.class);

runner.runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition)));
runner.runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), runtime));

InOrder inOrder = inOrder(failingBeforeHook, stepDefinition);
inOrder.verify(failingBeforeHook).execute(Matchers.<Scenario>any());
Expand All @@ -82,7 +82,22 @@ public void hooks_execute_also_after_failure() throws Throwable {
inOrder.verify(beforeHook).execute(Matchers.<Scenario>any());
inOrder.verify(afterHook).execute(Matchers.<Scenario>any());
}

@Test
public void steps_are_executed() throws Throwable {
final StepDefinition stepDefinition = mock(StepDefinition.class);
runtime.getRunner().runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), runtime));
verify(stepDefinition).execute(Matchers.anyString(), Matchers.<Object[]>any());
}

@Test
public void steps_are_not_executed_on_dry_run() throws Throwable {
final StepDefinition stepDefinition = mock(StepDefinition.class);
final Runtime dryRuntime = createRuntime(backend, "--dry-run");
dryRuntime.getRunner().runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), dryRuntime));
verify(stepDefinition, never()).execute(Matchers.anyString(), Matchers.<Object[]>any());
}

@Test
public void hooks_not_executed_in_dry_run_mode() throws Throwable {
Runtime runtime = createRuntime(backend, "--dry-run");
Expand Down Expand Up @@ -141,7 +156,7 @@ private PickleEvent createEmptyPickleEvent() {
return new PickleEvent("uri", new Pickle(NAME, ENGLISH, NO_STEPS, NO_TAGS, MOCK_LOCATIONS));
}

private PickleEvent createPickleEventMatchingStepDefinitions(List<StepDefinition> stepDefinitions) {
private PickleEvent createPickleEventMatchingStepDefinitions(List<StepDefinition> stepDefinitions, Runtime runtime) {
List<PickleStep> steps = new ArrayList<PickleStep>(stepDefinitions.size());
int i = 0;
for (StepDefinition stepDefinition : stepDefinitions) {
Expand Down