Description
Tests that save files (screenshots, logs, data extracts etc) currently have no way of notifying external programs where these files have been saved, other than possibly by printing a line to a logger or System.out or by writing to a file that other programs are aware of. It would be nice for tests to be able to notify additional events (e.g file saved) to test listeners so that IDEs, CI servers or any other calling program can link to or load these for a user.
Kohsuke Kawaguchi did write a blog post sometime back about this: http://kohsuke.org/?s=junit+attachment which shows the hack created to allow Jenkins to attach files to test reports.
Actually implementing this is a bit of a challenge since tests don't have visibility of listeners. A couple of options I can think of for approaching this:
- Allow tests to have a constructor that takes a
FileListener
parameter for reporting file events to (withFileListener
just being a wrapper round a list ofTestListener
s). - Allow test to declare a
setFileEventListener(FileListener)
method, possibly from a newFileHandlingTest
interface - Create a
FileListenerRule
rule that records a list of saved files then throws aFilesSaved
exception which runners pick up (similar to howErrorCollector
works). - Create a special
FileListenerRule
rule which runners look for whilst constructing the test and inject anyTestListener
s into.
Out of these options, 1 and 2 seem to be reverting to JUnit3 style structures so I'm not convinced they're particularly suitable. Option 3 isn't brilliant since it's using exceptions to handle flow control for non error scenarios, wouldn't work with custom runners (failures/errors would be reports for file saves) and may cause issues with some Rules. Option 4 wouldn't report file events for any custom runners that didn't inject listeners into the rule but wouldn't cause failures in this scenario, although does require runners interact directly with a particular rule type which some people may object to.
I'm happy to create some code for the different options but want to know initial opinions about whether anyone wants this feature, and whether anyone has any strong opinions about how this should (or shouldn't) work.