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

support multiple junit --run-listener #86 #98

Merged
merged 3 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions src/main/java/com/novocode/junit/JUnitRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sbt.testing.TaskDef;

import java.util.*;

import java.util.stream.Collectors;

final class JUnitRunner implements Runner {
private final String[] args;
Expand All @@ -16,7 +16,7 @@ final class JUnitRunner implements Runner {
private volatile boolean used = false;

final ClassLoader testClassLoader;
final RunListener runListener;
final List<RunListener> runListeners;
final RunStatistics runStatistics;

JUnitRunner(String[] args, String[] remoteArgs, ClassLoader testClassLoader) {
Expand All @@ -35,7 +35,7 @@ final class JUnitRunner implements Runner {

String testFilter = "";
String ignoreRunners = "org.junit.runners.Suite";
String runListener = null;
final List<String> runListeners = new ArrayList<>();
for(String s : args) {
if("-q".equals(s)) quiet = true;
else if("-v".equals(s)) verbosity = RunSettings.Verbosity.STARTED;
Expand All @@ -48,7 +48,7 @@ final class JUnitRunner implements Runner {
else if("-c".equals(s)) logExceptionClass = false;
else if(s.startsWith("--tests=")) testFilter = s.substring(8);
else if(s.startsWith("--ignore-runners=")) ignoreRunners = s.substring(17);
else if(s.startsWith("--run-listener=")) runListener = s.substring(15);
else if(s.startsWith("--run-listener=")) runListeners.add(s.substring(15));
else if(s.startsWith("--include-categories=")) includeCategories.addAll(Arrays.asList(s.substring(21).split(",")));
else if(s.startsWith("--exclude-categories=")) excludeCategories.addAll(Arrays.asList(s.substring(21).split(",")));
else if(s.startsWith("-D") && s.contains("=")) {
Expand All @@ -68,7 +68,9 @@ else if(s.startsWith("-D") && s.contains("=")) {
new RunSettings(!nocolor, decodeScalaNames, quiet, verbosity, summary, logAssert, ignoreRunners, logExceptionClass,
sysprops, globPatterns, includeCategories, excludeCategories,
testFilter);
this.runListener = createRunListener(runListener);
this.runListeners = runListeners.stream()
.map(this::createRunListener)
.collect(Collectors.toList());
this.runStatistics = new RunStatistics(settings);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/novocode/junit/JUnitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public Task[] execute(EventHandler eventHandler, Logger[] loggers) {
EventDispatcher ed = new EventDispatcher(logger, eventHandler, settings, fingerprint, taskDescription, runner.runStatistics);
JUnitCore ju = new JUnitCore();
ju.addListener(ed);
if (runner.runListener != null) ju.addListener(runner.runListener);

runner.runListeners.forEach(ju::addListener);

Map<String, Object> oldprops = settings.overrideSystemProperties();
try {
Expand Down
47 changes: 47 additions & 0 deletions src/sbt-test/simple/test-listener-multiple/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name := "test-project"

scalaVersion := "2.10.7"

libraryDependencies += "com.novocode" % "junit-interface" % sys.props("plugin.version") % "test"
eed3si9n marked this conversation as resolved.
Show resolved Hide resolved

testOptions += Tests.Argument(
TestFrameworks.JUnit,
"-v", "-n",
"--run-listener=test.JUnitListener1",
"--run-listener=test.JUnitListener2"
)

val listenerFile = settingKey[File]("location of the listener output")

listenerFile := target.value / "listener.txt"

javaOptions in Test += "-Djunit.output.file=" + listenerFile.value.getAbsolutePath

fork in Test := true

val checkRunListenerFile = taskKey[Unit]("Tests that the file is correct")

checkRunListenerFile := {
val expectedContent =
"""testStarted_1 testFail(TestFoo)
|testStarted_2 testFail(TestFoo)
|testFailure_1 testFail(TestFoo)
|testFailure_2 testFail(TestFoo)
|testFinished_1 testFail(TestFoo)
|testFinished_2 testFail(TestFoo)
|testStarted_1 testPass(TestFoo)
|testStarted_2 testPass(TestFoo)
|testFinished_1 testPass(TestFoo)
|testFinished_2 testPass(TestFoo)
|testRunFinished_1
|testRunFinished_2""".stripMargin.replace("\r", "")

val actualContent = sbt.IO.readLines(listenerFile.value).mkString("\n")
assert(
expectedContent == actualContent,
s"""Expecting content:
|$expectedContent
|Actual content:
|$actualContent""".stripMargin
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

public class JUnitListener1 extends JUnitListenerBase {
public JUnitListener1() {
super("1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

public class JUnitListener2 extends JUnitListenerBase {
public JUnitListener2() {
super("2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package test;

import org.junit.*;
import java.io.*;
import org.junit.runner.*;
import org.junit.runner.notification.*;

public abstract class JUnitListenerBase extends RunListener {
private PrintWriter pw;
private String outputFile = System.getProperty("junit.output.file");

private final String id;

public JUnitListenerBase(String id) {
this.id = id;
}

public void testRunStarted(Description description) throws Exception {
File file = new File(outputFile);
pw = new PrintWriter(new FileWriter(outputFile, /*append=*/true));
}
public void testRunFinished(Result result) throws Exception {
pw.println("testRunFinished_" + id);
pw.close();
}
public void testStarted(Description description) throws Exception {
pw.println("testStarted_" + id + " " + description.getDisplayName());
pw.flush();
}
public void testFinished(Description description) throws Exception {
pw.println("testFinished_" + id + " " + description.getDisplayName());
pw.flush();
}
public void testFailure(Failure failure) throws Exception {
pw.println("testFailure_" + id + " " + failure.getDescription().getDisplayName());
pw.flush();
}
public void testAssumptionFailure(Failure failure) {
pw.print("ASSUMPTION FAILURE");
pw.flush();
}
public void testIgnored(Description description) throws Exception {
pw.println("testIgnored_" + id + " " + description.getDisplayName());
pw.flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.junit._

class TestFoo {

@Test
def testPass(): Unit = {
Thread.sleep(2000)
import Assert._
assertEquals("Test should pass", true, true)
}

@Test
def testFail(): Unit = {
Thread.sleep(2000)
import Assert._
assertEquals("Test should fail", fail, true)
}
}
7 changes: 7 additions & 0 deletions src/sbt-test/simple/test-listener-multiple/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# test that the run listener option works.
# one test passes, other fails
-> test
# Check that file has been created and is correct
> checkRunListenerFile
# Debugging.
> show definedTests