Skip to content

Commit

Permalink
Merge pull request #716 from fhoeben/TrackSlimTestAbort
Browse files Browse the repository at this point in the history
Notify jUnit of test system exceptions
  • Loading branch information
amolenaar committed Apr 23, 2015
2 parents 6404675 + 5cc777d commit 4311512
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/fitnesse/junit/FitNesseRunner.java
Expand Up @@ -389,8 +389,8 @@ protected void runChild(WikiPage page, RunNotifier notifier) {

protected void runPages(List<WikiPage>pages, final RunNotifier notifier) {
MultipleTestsRunner testRunner = createTestRunner(pages);
testRunner.addTestSystemListener(new JUnitRunNotifierResultsListener(notifier, suiteClass));
testRunner.addExecutionLogListener(new ConsoleExecutionLogListener());
addTestSystemListeners(notifier, testRunner, suiteClass);
addExecutionLogListener(notifier, testRunner, suiteClass);
System.setProperty(SystemExitSecurityManager.PREVENT_SYSTEM_EXIT, String.valueOf(preventSystemExit));
try {
executeTests(testRunner);
Expand All @@ -401,6 +401,14 @@ protected void runPages(List<WikiPage>pages, final RunNotifier notifier) {
}
}

protected void addTestSystemListeners(RunNotifier notifier, MultipleTestsRunner testRunner, Class<?> suiteClass) {
testRunner.addTestSystemListener(new JUnitRunNotifierResultsListener(notifier, suiteClass));
}

protected void addExecutionLogListener(RunNotifier notifier, MultipleTestsRunner testRunner, Class<?> suiteClass) {
testRunner.addExecutionLogListener(new ConsoleExecutionLogListener());
}

protected List<WikiPage> initChildren() {
WikiPage suiteRoot = getSuiteRootPage();
if (suiteRoot == null) {
Expand Down
33 changes: 32 additions & 1 deletion src/fitnesse/junit/JUnitRunNotifierResultsListener.java
@@ -1,5 +1,6 @@
package fitnesse.junit;

import fitnesse.testrunner.TestsRunnerListener;
import fitnesse.testsystems.Assertion;
import fitnesse.testsystems.ExceptionResult;
import fitnesse.testsystems.ExecutionResult;
Expand All @@ -12,17 +13,31 @@
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class JUnitRunNotifierResultsListener implements TestSystemListener<WikiTestPage> {
import java.io.IOException;

public class JUnitRunNotifierResultsListener implements TestSystemListener<WikiTestPage>, TestsRunnerListener {

private final Class<?> mainClass;
private final RunNotifier notifier;
private int totalNumberOfTests;
private int completedTests;
private Throwable firstFailure;

public JUnitRunNotifierResultsListener(RunNotifier notifier, Class<?> mainClass) {
this.notifier = notifier;
this.mainClass = mainClass;
}

@Override
public void announceNumberTestsToRun(int testsToRun) {
totalNumberOfTests = testsToRun;
}

@Override
public void unableToStartTestSystem(String testSystemName, Throwable cause) throws IOException {
notifyOfTestSystemException(testSystemName, cause);
}

@Override
public void testStarted(WikiTestPage test) {
firstFailure = null;
Expand All @@ -33,6 +48,7 @@ public void testStarted(WikiTestPage test) {

@Override
public void testComplete(WikiTestPage test, TestSummary testSummary) {
completedTests++;
if (firstFailure != null) {
notifier.fireTestFailure(new Failure(descriptionFor(test), firstFailure));
} else if (test.isTestPage()) {
Expand Down Expand Up @@ -65,6 +81,21 @@ public void testSystemStarted(TestSystem testSystem) {

@Override
public void testSystemStopped(TestSystem testSystem, Throwable cause) {
notifyOfTestSystemException(testSystem.getName(), cause);
}

protected void notifyOfTestSystemException(String testSystemName, Throwable cause) {
Throwable t = cause;
if (completedTests != totalNumberOfTests) {
String msg = String.format(
"Unable to complete suite. Error in test system %s. Completed %s of %s tests.",
testSystemName, completedTests, totalNumberOfTests);
t = new Exception(msg, cause);
}

if (t != null) {
notifier.fireTestFailure(new Failure(Description.createSuiteDescription(mainClass), t));
}
}

private Description descriptionFor(WikiTestPage test) {
Expand Down
44 changes: 44 additions & 0 deletions test/fitnesse/junit/FitNesseRunnerExtensionTest.java
Expand Up @@ -2,9 +2,17 @@

import fitnesse.FitNesseContext;
import fitnesse.components.PluginsClassLoader;
import fitnesse.testrunner.MultipleTestsRunner;
import fitnesse.testrunner.WikiTestPage;
import fitnesse.testsystems.*;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

import java.io.IOException;

import static org.junit.Assert.assertNull;

@RunWith(FitNesseRunnerExtensionTest.SuiteExtension.class)
@FitNesseRunner.FitnesseDir(".")
@FitNesseRunner.OutputDir("../target/fitnesse-results")
Expand All @@ -15,6 +23,11 @@ public SuiteExtension(Class<?> suiteClass) throws InitializationError {
super(suiteClass);
}

@Override
protected void addTestSystemListeners(RunNotifier notifier, MultipleTestsRunner testRunner, Class<?> suiteClass) {
testRunner.addTestSystemListener(new ListenerExtension(notifier, suiteClass));
}

@Override
protected String getSuiteName(Class<?> klass) throws InitializationError {
return "FitNesse.SuiteAcceptanceTests.SuiteSlimTests.TestScriptTable";
Expand All @@ -27,4 +40,35 @@ protected FitNesseContext createContext(Class<?> suiteClass) throws Exception {
return super.createContext(suiteClass);
}
}

public static class ListenerExtension extends JUnitRunNotifierResultsListener {
public ListenerExtension(RunNotifier notifier, Class<?> mainClass) {
super(notifier, mainClass);
}

@Override
public void announceNumberTestsToRun(int testsToRun) {
super.announceNumberTestsToRun(testsToRun);
}

@Override
public void unableToStartTestSystem(String testSystemName, Throwable cause) throws IOException {
super.unableToStartTestSystem(testSystemName, cause);
}

@Override
public void testStarted(WikiTestPage test) {
super.testStarted(test);
}

@Override
public void testComplete(WikiTestPage test, TestSummary testSummary) {
super.testComplete(test, testSummary);
}

@Override
public void testSystemStopped(TestSystem testSystem, Throwable cause) {
super.testSystemStopped(testSystem, cause);
}
}
}

0 comments on commit 4311512

Please sign in to comment.