You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Simon Kitching <sk...@apache.org> on 2005/06/26 08:59:38 UTC

Executing junit tests with differing classpaths

Hi,

At the jakarta-commons-logging project we need to be able to execute
various unit tests with different classpaths set. 

As an example test Test1 may need to have
  parent-classloader: lib1.jar
  child-classloader: lib2.jar, test-classes-dir

The old way of executing unit tests is by using a target which has a
<java> task that executes junit.textui.TestRunner. Currently
commons-logging uses a similar approach, but executes a custom class
that:
* sets up classloaders with custom paths appropriate for the test
* loads class junit.textui.TestRunner into a classloader
* calls TestRunner.main to execute the test
The test class then sees exactly the classloader hierarchy that is
needed for the test.

Unfortunately this means that we need to explicitly execute each
possible testcase via a separate <java> task (specifying our
classloader-tweaking class as the target). TestSuite classes that return
Suite objects containing multiple classes can be defined, but obviously
only testcases that are expecting the same classpath can be combined in
this way (and it's not a very elegant solution anyway).

The <junit> and <batchtest> tasks can make life much easier. But
unfortunately as far as I can see these tasks provide no ability to
insert our custom classloader-configuration step before each test is
run.

And of course because Maven's maven-test-plugin is based on <junit> and
<batchtest> we can't use Maven the standard maven process to run our
unit tests.


Am I right in thinking that the <junit> task provides no way for the
classloader setup in the forked JVM to be tweaked before the test is
executed?

If so, would anyone be interested in extending Ant with a hook to allow
this? I *think* it's just a matter of providing the <junit> task with an
attribute "testRunnerClass" which defaults to 
  org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner
rather than hardwiring that string into the JUnitTask class.


Regards,

Simon


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


Re: Executing junit tests with differing classpaths

Posted by Simon Kitching <sk...@apache.org>.
On Sun, 2005-06-26 at 12:07 +0200, Stefan Bodewig wrote:
> On Sun, 26 Jun 2005, Simon Kitching <sk...@apache.org> wrote:
> 
> > Am I right in thinking that the <junit> task provides no way for the
> > classloader setup in the forked JVM to be tweaked before the test is
> > executed?
> 
> Not sure.  You can pass a <classpath> to the forked VM.  You could
> turn your current classloader-logic into an Ant task that creates a
> Path reference, something like
> 
> <create-classpath forTest="testclass" useId="first-batch"/>
> <junit classpathref="first-batch" ....>
> 
> > If so, would anyone be interested in extending Ant with a hook to
> > allow this? I *think* it's just a matter of providing the <junit>
> > task with an attribute "testRunnerClass" which defaults to
> > org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner rather
> > than hardwiring that string into the JUnitTask class.
> 
> Something like this is way down on my TODO list, so I'd be glad if
> anybody else picked it up.  In my case it is as a way to provide a
> JUnit 4 Testrunner.
> 
> It's not that simple, though.  Ant's TestRunner is responsible for
> routing events to the formatters, for example.  So we can't take an
> arbitrary test-runner, we do have some additional requirements.

Just for everyone's information, I actually decided to take a different
approach to this. By creating a custom TestSuite class, it is possible
to control the classloader hierarchy used for a TestCase's tests without
making changes to junit or ant.

You can find the details here:
http://marc.theaimsgroup.com/?l=jakarta-commons-dev&m=112004597501874&w=2
and an example test case here:
http://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ParentFirstTestCase.java

Regards,

Simon


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


Re: Executing junit tests with differing classpaths

Posted by Stefan Bodewig <bo...@apache.org>.
On Sun, 26 Jun 2005, Simon Kitching <sk...@apache.org> wrote:

> Am I right in thinking that the <junit> task provides no way for the
> classloader setup in the forked JVM to be tweaked before the test is
> executed?

Not sure.  You can pass a <classpath> to the forked VM.  You could
turn your current classloader-logic into an Ant task that creates a
Path reference, something like

<create-classpath forTest="testclass" useId="first-batch"/>
<junit classpathref="first-batch" ....>

> If so, would anyone be interested in extending Ant with a hook to
> allow this? I *think* it's just a matter of providing the <junit>
> task with an attribute "testRunnerClass" which defaults to
> org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner rather
> than hardwiring that string into the JUnitTask class.

Something like this is way down on my TODO list, so I'd be glad if
anybody else picked it up.  In my case it is as a way to provide a
JUnit 4 Testrunner.

It's not that simple, though.  Ant's TestRunner is responsible for
routing events to the formatters, for example.  So we can't take an
arbitrary test-runner, we do have some additional requirements.

Stefan

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