2012-09-05 40 views
3

作爲JUnit測試(從ANT執行)的一部分,我需要訪問一些公平的屬性,這些屬性不僅依賴於環境,而且還可以從一次運行切換到下一次運行。爲了不影響任何可能使用相同build.xml的其他開發者,我希望在命令行中使用-D標誌定義這些屬性(它們可以使用腳本輕鬆填充)。閱讀關於螞蟻中的<junit>任務,我認爲這些屬性可以輕鬆地複製到使用clonevm標誌運行JUnit測試的虛擬機,但這似乎不起作用。在<junit>呼叫之前的簡單<echo>確認屬性都設置正確,我再與下面的執行JUnit測試:如何將VM屬性複製到ANT中的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");讀取屬性值,但總是空。我假設我必須做錯事情,以便將屬性不發送給JUnit VM,否則我沒有正確讀取它。有任何想法嗎?我想我可以解決這個問題(使用腳本編寫一個臨時屬性文件,然後從測試中讀取),但是如果可能的話,我仍然想知道如何做到這一點。

+0

嘗試使用嵌套的syspropertyset元素。請參閱junit文檔:http://ant.apache.org/manual/Tasks/junit.html –

+0

這聽起來可行。不幸的是,它不是我能夠保留的解決方案(我粘貼的構建腳本的片段不受我控制,我只能設置各種參數),但我當然可以測試它並查看它是否有效。 – Thor84no

+0

@ MarkO'Connor我已經看過它並發現使用''以及'非常有用,因爲它可以輕鬆地在JUnit測試中提供ANT中提供的所有屬性。我可能會將其傳遞給構建團隊,以便稍後使用它。非常感謝。你應該把它作爲答案,所以我可以給你點分數。 – Thor84no

回答

相關問題