Skip to content

Commit

Permalink
Use the feature name as class name in reports.
Browse files Browse the repository at this point in the history
Change both the JUnit Formatter and the Android Instrumentation Reporter
to use the feature name as the class name. This is the behavior in
v1.2.5, but to avoid having to extract the feature name from the
TestSourceRead event it was changed in the upgrade to Gherkin v4.
  • Loading branch information
brasmusson committed Jul 7, 2017
1 parent c650ee8 commit 18a2bb0
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 72 deletions.
Expand Up @@ -8,9 +8,11 @@
import cucumber.api.event.EventPublisher; import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseFinished; import cucumber.api.event.TestCaseFinished;
import cucumber.api.event.TestCaseStarted; import cucumber.api.event.TestCaseStarted;
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished; import cucumber.api.event.TestStepFinished;
import cucumber.api.formatter.Formatter; import cucumber.api.formatter.Formatter;
import cucumber.runtime.Runtime; import cucumber.runtime.Runtime;
import cucumber.runtime.formatter.TestSourcesModel;


import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
Expand Down Expand Up @@ -56,6 +58,11 @@ public static class StatusCodes {
public static final int OK = 0; public static final int OK = 0;
} }


/**
* The collected TestSourceRead events.
*/
private final TestSourcesModel testSources = new TestSourcesModel();

/** /**
* The current cucumber runtime. * The current cucumber runtime.
*/ */
Expand All @@ -69,7 +76,7 @@ public static class StatusCodes {
/** /**
* The total number of tests which will be executed. * The total number of tests which will be executed.
*/ */
private final int numberOfTests; private int numberOfTests;


/** /**
* The severest step result of the current test execution. * The severest step result of the current test execution.
Expand All @@ -78,15 +85,30 @@ public static class StatusCodes {
private Result severestResult; private Result severestResult;


/** /**
* The location in the feature file of the current test case. * The uri of the feature file of the current test case.
*/
private String currentUri;

/**
* The name of the current feature.
*/ */
private String currentPath; private String currentFeatureName;


/** /**
* The name of the current test case. * The name of the current test case.
*/ */
private String currentTestCaseName; private String currentTestCaseName;


/**
* The event handler for the {@link TestSourceRead} events.
*/
private final EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
@Override
public void receive(TestSourceRead event) {
testSourceRead(event);
}
};

/** /**
* The event handler for the {@link TestCaseStarted} events. * The event handler for the {@link TestCaseStarted} events.
*/ */
Expand Down Expand Up @@ -126,26 +148,36 @@ public void receive(TestCaseFinished event) {
*/ */
public AndroidInstrumentationReporter( public AndroidInstrumentationReporter(
final Runtime runtime, final Runtime runtime,
final Instrumentation instrumentation, final Instrumentation instrumentation) {
final int numberOfTests) {


this.runtime = runtime; this.runtime = runtime;
this.instrumentation = instrumentation; this.instrumentation = instrumentation;
this.numberOfTests = numberOfTests;
} }


@Override @Override
public void setEventPublisher(final EventPublisher publisher) { public void setEventPublisher(final EventPublisher publisher) {
publisher.registerHandlerFor(TestSourceRead.class, testSourceReadHandler);
publisher.registerHandlerFor(TestCaseStarted.class, testCaseStartedHandler); publisher.registerHandlerFor(TestCaseStarted.class, testCaseStartedHandler);
publisher.registerHandlerFor(TestCaseFinished.class, testCaseFinishedHandler); publisher.registerHandlerFor(TestCaseFinished.class, testCaseFinishedHandler);
publisher.registerHandlerFor(TestStepFinished.class, testStepFinishedHandler); publisher.registerHandlerFor(TestStepFinished.class, testStepFinishedHandler);
} }


public void setNumberOfTests(final int numberOfTests) {
this.numberOfTests = numberOfTests;
}

void testSourceRead(final TestSourceRead event) {
testSources.addTestSourceReadEvent(event.path, event);
}

void startTestCase(final TestCase testCase) { void startTestCase(final TestCase testCase) {
currentPath = testCase.getPath(); if (!testCase.getPath().equals(currentUri)) {
currentUri = testCase.getPath();
currentFeatureName = testSources.getFeatureName(currentUri);
}
currentTestCaseName = testCase.getName(); currentTestCaseName = testCase.getName();
resetSeverestResult(); resetSeverestResult();
final Bundle testStart = createBundle(currentPath, currentTestCaseName); final Bundle testStart = createBundle(currentFeatureName, currentTestCaseName);
instrumentation.sendStatus(StatusCodes.START, testStart); instrumentation.sendStatus(StatusCodes.START, testStart);
} }


Expand All @@ -154,7 +186,7 @@ void finishTestStep(final Result result) {
} }


void finishTestCase() { void finishTestCase() {
final Bundle testResult = createBundle(currentPath, currentTestCaseName); final Bundle testResult = createBundle(currentFeatureName, currentTestCaseName);


switch (severestResult.getStatus()) { switch (severestResult.getStatus()) {
case FAILED: case FAILED:
Expand Down
Expand Up @@ -89,18 +89,20 @@ public CucumberExecutor(final Arguments arguments, final Instrumentation instrum


ResourceLoader resourceLoader = new AndroidResourceLoader(context); ResourceLoader resourceLoader = new AndroidResourceLoader(context);
this.runtime = new Runtime(resourceLoader, classLoader, createBackends(), runtimeOptions); this.runtime = new Runtime(resourceLoader, classLoader, createBackends(), runtimeOptions);
AndroidInstrumentationReporter instrumentationReporter = new AndroidInstrumentationReporter(runtime, instrumentation);
runtimeOptions.addPlugin(instrumentationReporter);
runtimeOptions.addPlugin(new AndroidLogcatReporter(runtime, TAG));

List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader, runtime.getEventBus()); List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader, runtime.getEventBus());
this.pickleEvents = FeatureCompiler.compile(cucumberFeatures, this.runtime); this.pickleEvents = FeatureCompiler.compile(cucumberFeatures, this.runtime);
instrumentationReporter.setNumberOfTests(getNumberOfConcreteScenarios());
} }


/** /**
* Runs the cucumber scenarios with the specified arguments. * Runs the cucumber scenarios with the specified arguments.
*/ */
public void execute() { public void execute() {


runtimeOptions.addPlugin(new AndroidInstrumentationReporter(runtime, instrumentation, getNumberOfConcreteScenarios()));
runtimeOptions.addPlugin(new AndroidLogcatReporter(runtime, TAG));

// TODO: This is duplicated in info.cucumber.Runtime. // TODO: This is duplicated in info.cucumber.Runtime.


final StepDefinitionReporter stepDefinitionReporter = runtimeOptions.stepDefinitionReporter(classLoader); final StepDefinitionReporter stepDefinitionReporter = runtimeOptions.stepDefinitionReporter(classLoader);
Expand Down

0 comments on commit 18a2bb0

Please sign in to comment.