Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add logging listener for whole test suite

Tags:

logging

junit

I am using JUnit4 and organized all my tests in a Test suite, annotated with a @RunWith(Suite.class) annotation.

Now I want to register a custom logger to JUnit, so at each beginning and completion of each test a log message is printed.

I already found the RunNotifier and RunListener classes, but don't know how I can add my listener from within my TestSuite definition to JUnit.

Any ideas?

like image 908
Daniel Avatar asked May 30 '11 12:05

Daniel


2 Answers

I think you've already hinted at the solution. Write your own runner and use the @RunWith annotation to specify your runner for your test/suite. That should allow you to see your output even when running in eclipse/ant as junit will use the annotation.

like image 113
papercrane Avatar answered Sep 23 '22 00:09

papercrane


You can instantiate a JUnitCore object and call addListener() before calling run. Not sure if there is a better way with an annotation, but I haven't come across one.

like image 43
Jeanne Boyarsky Avatar answered Sep 23 '22 00:09

Jeanne Boyarsky