Search code examples
junitantjvm-arguments

Can I pass all the <jvmarg> parameters to an ant junit task from a single file


So I have something like

<junit fork="true" forkmode="perTest">
   <jvmarg value="-Darg.to.pass.our.tests.one=arg1">
   <jvmarg value="-Darg.to.pass.our.tests.two=arg2">
   <jvmarg value="-Darg.to.pass.our.tests.three=arg3">
   ...
   <jvmarg value="-Darg.to.pass.our.tests.enn=argN">
</junit>

I know the ideal is forking once, but that's not possible for our tests for reasons I won't go into.

My question is, can I do something like:

<junit fork="true" forkmode="perTest">
   <jvmargs file="standard_args.properties">
</junit>

I'm looking for the freedom to specify different argument at test execution time.

I know I could do

<junit fork="true" forkmode="perTest">
   <jvmarg value="${arg.one}">
   <jvmarg value="${arg.two}">
   ...
   <jvmarg value="${arg.enn}">
</junit>

and I do use this method but this puts an expectation on the exact number of arguments passed.

Any thoughts?


Solution

  • since all the properties your are passing to your tests are System properties (-Dxxx=yyy) , you should consider using a syspropertyset (c.f. ant documentation for JUnit ):

    <property file="standard_args.properties"/>
    ...
    <junit fork="true" forkmode="perTest">
        <syspropertyset>
            <propertyref prefix="standard.arg"/>
        </syspropertyset>
    </junit>
    

    assuming that your property file standard_args.properties contains properties for your tests and that all those properties start with a specific pattern (i.e. standard.arg in the example above).