junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.53k stars 3.29k forks source link

Junit4 RunListener Query #1686

Closed BalasubramanyamEvani closed 3 years ago

BalasubramanyamEvani commented 3 years ago

I'm creating a custom listener to update/send details to another application after a run. As I understand the following methods can be overridden for recording various test events.

testRunStarted, testRunFinished, testStarted, testFinished, testFailure, testAssumptionFailure, testIgnored

For my case, with the help above methods I've managed to capture the respective events, but is there a way to

Thanks for your time !

marcphilipp commented 3 years ago

I'm afraid there's no way to do that using JUnit 4's listener. You can, however, capture the output using a TestExecutionListener when running the tests on the JUnit Platform (a.k.a. JUnit 5) with the JUnit Vintage Engine: https://junit.org/junit5/docs/current/user-guide/#running-tests-capturing-output https://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom

kcooney commented 3 years ago

@BalasubramanyamEvani actually you may be able to capture the log messages JUnit 4. If your tests run serially, then you could update System.out and/or System.err in testStarted() to point to a buffer or temporary file (whether this works depends on how your logging is done and how it is initialized). You won't know whether you are capturing logs of a passing or failing test util you receive testFailure() or testFinished().

If tests can run in parallel then capturing the output of a single test can be challenging (especially if the tests can start threads). I personally think it's simpler to just capture the output of the test run, and possibly print out the test method name in testStarted().

sbabcoc commented 3 years ago

You can get full test lifecycle notifications for parameterized JUnit 4 tests, including access to the parameter values, via the JUnit Foundation library.

sbabcoc commented 3 years ago

Log messages are a bit more involved, but SLF4J includes support for something they call "mapped diagnostic context" that may be of assistance.