ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chip McCormick <CMcCorm...@commercehub.com>
Subject RE: junit ant task test listeners
Date Wed, 29 Sep 2010 15:39:37 GMT

-----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


Mime
View raw message