You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Chip McCormick <CM...@commercehub.com> on 2010/08/23 14:59:14 UTC

junit ant task test listeners

Does anyone know how to add a test listener to an ant junit task, without having to create a custom task?

We are attempting to add a test listener to a junit ant task to do custom logging after each test class in a batch completes.

The only mention in the API of how to do this describes how to enable the test listener events, but does not explain how to add a listener for a specific event:
http://ant.apache.org/manual/Tasks/junit.html#enabletestlistenerevents

It looks like the 'enabletestlistenerevents' feature was added to ant in response to this bug:
https://issues.apache.org/bugzilla/show_bug.cgi?id=31885

Chip McCormick
Software Engineer
CommerceHub


RE: junit ant task test listeners

Posted by Chip McCormick <CM...@commercehub.com>.
-----Original Message-----
From: Stefan Bodewig [mailto:bodewig@apache.org] 
Sent: Monday, August 23, 2010 9:20 AM
To: user@ant.apache.org
Subject: Re: junit ant task test listeners

On 2010-08-23, Chip McCormick wrote:

> Does anyone know how to add a test listener to an ant junit task, 
> without having to create a custom task?

> We are attempting to add a test listener to a junit ant task to do 
> custom logging after each test class in a batch completes.

Ant's formatters are TestListeners and this sounds exactly as if you wanted to attach your own kind of formatter.  Formatters can be added via nested <formatter> elements and a classname attribute - you can specify a classpath to load the listener from as well.

Stefan

That worked perfectly, thanks.

In case anyone finds this discussion and is looking for ways to programmatically fail JUnit tests run via the ant task, a few tips are below.

--Chip

PROGRAMATICALLY FAILING TESTS

1) Add a formatter either within the ant JUnit task, or within a specific test that is a child of that task. E.g.:
<formatter classname="com.commercehub.tools.ant.taskdefs.DatabaseCleanupJUnitListener" extension=".xml"/>

2) The only requirement is that the listener implement JUnitResultFormatter and doing so will give you access to lifecycle events like startSuite (the test class) and startTest (each test method). My goal was to modify the default XML formatting to fail tests programmatically (won't get into why we didn't use a simpler approach here), so I started with the code for XMLJUnitResultFormatter.

3) Check the conditions of interest in a lifecycle method and output the XML or text that indicates a test failed. You should also increment the failureCount. In the case of XML, that required adding an element of type FAILURE that displayed the desired method. I ran this check in the lifecycle method setSystemError, adding the <failure> tag and then detailed output of interest to the stream written within the <system-err> element.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: junit ant task test listeners

Posted by Stefan Bodewig <bo...@apache.org>.
On 2010-08-23, Chip McCormick wrote:

> Does anyone know how to add a test listener to an ant junit task,
> without having to create a custom task?

> We are attempting to add a test listener to a junit ant task to do
> custom logging after each test class in a batch completes.

Ant's formatters are TestListeners and this sounds exactly as if you
wanted to attach your own kind of formatter.  Formatters can be added
via nested <formatter> elements and a classname attribute - you can
specify a classpath to load the listener from as well.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org