You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Charles Chan <ch...@yahoo.com> on 2003/04/16 22:39:41 UTC

How to reuse test class?

HI, I have classes that are reused across all unit
test cases in multiple projects. I don't want to
package the unit test cases into the production JAR
files.

How do I let other projects know where to find the
common test classes when they compile their unit test
cases?

An example directory layout is shown below:

common/
   test/java/.../BaseTestCase.java

project1/
   test/java/.../Project1Test.java [extends
BaseTestCase]

project2/
   test/java/.../Project2Test.java [extends
BaseTestCase]

Thanks
Charles


__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to reuse test class?

Posted by Rafal Krzewski <Ra...@caltha.pl>.
Charles Chan wrote:
> HI, I have classes that are reused across all unit
> test cases in multiple projects. I don't want to
> package the unit test cases into the production JAR
> files.
> 
> How do I let other projects know where to find the
> common test classes when they compile their unit test
> cases?

One of the possible approaches is creating a sub-project that produces
a jar with the test base classes. It would be needed only for
development, but not for deployment.

global-project
 base-project
 base-tests (compile dep on base-project)
 subproject1 (compile dep on base-project, test dep on base-tests)
 subproject2 (compile dep on base-project, test dep on base-tests)
 ...

The picture becomes less clear if base-tests are actually needed for
testing base-package. In such situation you'll probably need to hack
you maven.xml to package the needed test classes into a jar and
inject it into the local repository, so that your subproject may
test-depend on it. Your base projet will be essentionally generating
two artifacts, and this is not recommended practice in Maven right
now, but I think doing this would be justified in this situation.

R.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: How to reuse test class?

Posted by David Zeleznik <dz...@ilog.com>.
Hi,

We have a similar situation with several test "frameworks". We use 0..n of
these test frameworks in any given project. However, we do not want these
test frameworks to be put in the compile class path for the regular source
code. We only want them in the compile and run path for the unit tests. Each
test-framework is its own project and we have created an optional
project-tests.xml descriptor that specifies the unit test dependencies for a
project. We then have pre and post goals that read the dependencies from
this special pom and add them to the test classpath:

  <goal name="addTestDependencies" >
    <available
      property="testPomPresent"
      file="${basedir}/project-tests.xml" />
    <j:if test="${testPomPresent}">
      <j:set var="testPathX"
value="${pom.getAntProject().getReference('test.path')}X"/>
      <j:if test="${testPathX == 'X'}" >
        <!-- Read the POM that describes test-specific dependencies and then
             verify those dependencies from the repo. -->
        <m:pom projectDescriptor="${basedir}/project-tests.xml"
var="testPom"/>
        <j:set var="dummyVar" value="${testPom.verifyDependencies()}" />
        <path id="test.path" path="${testPom.getDependencyClasspath()}" />
      </j:if>
      <!-- Save Maven's current class path as a string. Because it is a true
           Ant path object, we cannot simply declare a new variable that
           references the same object we are about to change. Also, we
cannot
           simply use the project's dependencyClasspath attribute because
other
           goals, such as clover, also make modifications to Maven's
dependency
           class path that we must preserve. -->
      <j:set
        var="savedPath"

value="${pom.getAntProject().getReference('maven.dependency.classpath').toSt
ring()}" />
      <!-- Add test-specific dependencies to the project's Ant-style
classpath
           structure. Note that this only modifies the Ant path object and
does
           not change the project's dependencyClasspath attribute. -->
      <m:addPath id="maven.dependency.classpath" refid="test.path" />
    </j:if>
  </goal>

  <goal name="removeTestDependencies" >
    <j:if test="${testPomPresent == 'true'}" >
      <!-- Restore Maven's classpath by redefining it. -->
      <path id="maven.dependency.classpath">
        <pathelement path="${savedPath}" />
      </path>
    </j:if>
  </goal>

I hope this gives you some ideas. I have thought a little bit about
generalizing this so that there were tags that could push and pop paths onto
a classpath stack. However, this solution works fine for us now.

--------------------------------------
David Zeleznik
ILOG - Changing the rules of business
mailto:dzeleznik@ilog.com
http://www.ilog.com
--------------------------------------



> -----Original Message-----
> From: Charles Chan [mailto:charles_cynthia@yahoo.com]
> Sent: Wednesday, April 16, 2003 4:40 PM
> To: maven-user
> Subject: How to reuse test class?
>
>
> HI, I have classes that are reused across all unit
> test cases in multiple projects. I don't want to
> package the unit test cases into the production JAR
> files.
>
> How do I let other projects know where to find the
> common test classes when they compile their unit test
> cases?
>
> An example directory layout is shown below:
>
> common/
>    test/java/.../BaseTestCase.java
>
> project1/
>    test/java/.../Project1Test.java [extends
> BaseTestCase]
>
> project2/
>    test/java/.../Project2Test.java [extends
> BaseTestCase]
>
> Thanks
> Charles
>
>
> __________________________________________________
> Do you Yahoo!?
> The New Yahoo! Search - Faster. Easier. Bingo
> http://search.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org