3

作为 JUnit 测试(从 ANT 执行)的一部分,我需要访问相当多的属性,这些属性不仅依赖于环境,而且还可以从一次运行更改为下一次运行。为了不影响可能使用相同 build.xml 的众多其他开发人员中的任何一个,我想在命令行上使用-D标志定义这些属性(它们很容易使用脚本填充)。阅读<junit>ant 中的任务时,我认为这些属性很容易复制到使用clonevm标志运行 JUnit 测试的 VM,但这似乎不起作用。一个简单<echo><junit>调用前确认属性设置正确,然后我执行以下 JUnit 测试:

<junit
    printsummary="on" showoutput="true" timeout="${tests.timeout.value}"
    haltonfailure="false" errorproperty="test.error.occurred"
    haltonerror="false" failureproperty="test.failure.occurred" forkmode="perTest"
    maxmemory="${project.junit.maxmemory}" clonevm="true">
    <jvmarg line="${project.test.vmargs}"/>
    <sysproperty key="java.io.tmpdir" value="${java.io.tmpdir}"/>
    <formatter type="xml" usefile="true"/>
    <classpath>
        <path refid="build.test.classpath"/>
    </classpath>
    <batchtest fork="yes" todir="${test.rpt.dir}">
        <fileset dir="${test.bin.dir}">
            <include name="**/${test.classes.run}.class"/>
            <includesfile name="${test.run.includesfile}"/>
            <!-- ignore inner classes -->
            <exclude name="**/*$*.class"/>
        </fileset>
    </batchtest>
</junit>

然后,我尝试使用System.getProperty("property");但总是获取 null 来读取属性值。我想我一定是做错了什么,所以属性不会发送到 JUnit VM,或者我没有正确读取它。有任何想法吗?我想我可以解决这个问题(使用脚本编写一个临时属性文件,然后从测试中读取它),但如果可能的话,我仍然想知道如何做到这一点。

4

1 回答 1

2

尝试使用嵌套的 syspropertyset 元素。

查看junit文档

于 2012-09-06T18:18:57.550 回答