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

Fix test cases execution time in reports #825

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void match(Match match) {
Step runnerStep = fetchAndCheckRunnerStep();
Description description = executionUnitRunner.describeChild(runnerStep);
stepNotifier = new EachTestNotifier(runNotifier, description);
stepNotifier.fireTestStarted();
reporter.match(match);
}

Expand Down Expand Up @@ -91,8 +92,6 @@ public void result(Result result) {
addFailureOrIgnoreStep(result);
} else {
if (stepNotifier != null) {
//Should only fireTestStarted if not ignored
stepNotifier.fireTestStarted();
if (error != null) {
stepNotifier.addFailure(error);
}
Expand Down
65 changes: 38 additions & 27 deletions junit/src/main/java/cucumber/runtime/junit/SanityChecker.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cucumber.runtime.junit;

import junit.framework.AssertionFailedError;
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestListener;
import junit.framework.TestResult;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.notification.RunListener;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -14,22 +12,22 @@
/**
* Listener that makes sure Cucumber fires events in the right order
*/
public class SanityChecker implements TestListener {
public class SanityChecker extends RunListener {
private static final String INDENT = " ";
public static final String INSANITY = "INSANITY";

private List<Test> tests = new ArrayList<Test>();
private List<Description> testDescriptions = new ArrayList<Description>();
private final StringWriter out = new StringWriter();

public static void run(Class<?> testClass) {
run(testClass, false);
}

public static void run(Class<?> testClass, boolean debug) {
JUnit4TestAdapter testAdapter = new JUnit4TestAdapter(testClass);
TestResult result = new TestResult();
JUnitCore runner = new JUnitCore();
SanityChecker listener = new SanityChecker();
result.addListener(listener);
testAdapter.run(result);
runner.addListener(listener);
runner.run(testClass);
String output = listener.getOutput();
if (output.contains(INSANITY)) {
throw new RuntimeException("Something went wrong\n" + output);
Expand All @@ -42,26 +40,18 @@ public static void run(Class<?> testClass, boolean debug) {
}

@Override
public void addError(Test test, Throwable t) {
}

@Override
public void addFailure(Test test, AssertionFailedError t) {
}

@Override
public void startTest(Test started) {
public void testStarted(Description started) {
spaces();
out.append("START " + started.toString()).append("\n");
tests.add(started);
out.append("START " + started.toString()).append("\n");
testDescriptions.add(started);
}

@Override
public void endTest(Test ended) {
public void testFinished(Description ended) {
try {
Test lastStarted = tests.remove(tests.size() - 1);
Description lastStarted = testDescriptions.remove(testDescriptions.size() - 1);
spaces();
out.append("END " + ended.toString()).append("\n");
out.append("END " + ended.toString()).append("\n");
if (!lastStarted.toString().equals(ended.toString())) {
out.append(INSANITY).append("\n");
String errorMessage = String.format("Started : %s\nEnded : %s\n", lastStarted, ended);
Expand All @@ -73,13 +63,34 @@ public void endTest(Test ended) {
}
}

@Override
public void testIgnored(Description ignored) {
try {
Description lastStarted = testDescriptions.remove(testDescriptions.size() - 1);
spaces();
out.append("IGNORE " + ignored.toString()).append("\n");
// For suites both a testIgnored and a testFinished is sent
if (ignored.isSuite()) {
testDescriptions.add(lastStarted);
}
if (!lastStarted.toString().equals(ignored.toString())) {
out.append(INSANITY).append("\n");
String errorMessage = String.format("Started : %s\nIgnored : %s\n", lastStarted, ignored);
out.append(errorMessage).append("\n");
}
} catch (Exception e) {
out.append(INSANITY).append("\n");
e.printStackTrace(new PrintWriter(out));
}
}

private void spaces() {
for (int i = 0; i < tests.size(); i++) {
for (int i = 0; i < testDescriptions.size(); i++) {
out.append(INDENT);
}
}

public String getOutput() {
return out.toString();
}
}
}
25 changes: 18 additions & 7 deletions junit/src/test/java/cucumber/runtime/junit/JUnitReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void result_with_undefined_step_non_strict() {

jUnitReporter.result(Result.UNDEFINED);

verify(stepNotifier, times(0)).fireTestStarted();
verify(stepNotifier, times(0)).fireTestFinished();
verify(stepNotifier, times(0)).addFailure(Matchers.<Throwable>any(Throwable.class));
verify(stepNotifier).fireTestIgnored();
Expand All @@ -81,7 +80,6 @@ public void result_with_undefined_step_strict() {

jUnitReporter.result(Result.UNDEFINED);

verify(stepNotifier, times(1)).fireTestStarted();
verify(stepNotifier, times(1)).fireTestFinished();
verifyAddFailureWithPendingException(stepNotifier);
verifyAddFailureWithPendingException(executionUnitNotifier);
Expand All @@ -106,7 +104,6 @@ public void result_with_pending_step_non_strict() {

jUnitReporter.result(result);

verify(stepNotifier, times(0)).fireTestStarted();
verify(stepNotifier, times(0)).fireTestFinished();
verify(stepNotifier, times(0)).addFailure(Matchers.<Throwable>any(Throwable.class));
verify(stepNotifier).fireTestIgnored();
Expand All @@ -126,7 +123,6 @@ public void result_with_pending_step_strict() {

jUnitReporter.result(result);

verify(stepNotifier, times(1)).fireTestStarted();
verify(stepNotifier, times(1)).fireTestFinished();
verifyAddFailureWithPendingException(stepNotifier);
verifyAddFailureWithPendingException(executionUnitNotifier);
Expand All @@ -143,7 +139,6 @@ public void result_without_error_non_strict() {

jUnitReporter.result(result);

verify(stepNotifier).fireTestStarted();
verify(stepNotifier).fireTestFinished();
verify(stepNotifier, times(0)).addFailure(Matchers.<Throwable>any(Throwable.class));
verify(stepNotifier, times(0)).fireTestIgnored();
Expand All @@ -159,7 +154,6 @@ public void result_without_error_strict() {

jUnitReporter.result(result);

verify(stepNotifier).fireTestStarted();
verify(stepNotifier).fireTestFinished();
verify(stepNotifier, times(0)).addFailure(Matchers.<Throwable>any(Throwable.class));
verify(stepNotifier, times(0)).fireTestIgnored();
Expand Down Expand Up @@ -270,6 +264,23 @@ public void throws_exception_when_runner_step_name_do_no_match_scenario_step_nam
}
}

@Test
public void match_fires_test_started_for_the_step() {
Step runnerStep = mockStep("Step Name");
Description runnerStepDescription = stepDescription(runnerStep);
ExecutionUnitRunner executionUnitRunner = mockExecutionUnitRunner(runnerSteps(runnerStep));
when(executionUnitRunner.describeChild(runnerStep)).thenReturn(runnerStepDescription);
createNonStrictReporter();
runNotifier = mock(RunNotifier.class);

jUnitReporter.startExecutionUnit(executionUnitRunner, runNotifier);
jUnitReporter.startOfScenarioLifeCycle(mock(Scenario.class));
jUnitReporter.step(runnerStep);
jUnitReporter.match(mock(Match.class));

verify(runNotifier).fireTestStarted(runnerStepDescription);
}

private Result mockResult() {
Result result = mock(Result.class);
when(result.getStatus()).thenReturn("passed");
Expand Down Expand Up @@ -335,4 +346,4 @@ private void createReporter(boolean strict) {
jUnitReporter = new JUnitReporter(reporter, formatter, strict);
}

}
}