簡體   English   中英

如何在Ant JunitLauncher中將系統屬性作為參數提供

[英]How to give System Properties as Parameters in Ant JunitLauncher

我正在嘗試將我的測試套件從Junit4遷移到Junit5。 在Junit4上運行測試的舊目標中有一堆系統屬性作為參數,但現在當我遷移到Junit5時,JunitLauncher不支持此參數。

在Junit4上運行測試的舊目標:

<target name="test">
    <mkdir dir="${junit_reports.dir}" />
    <junit printsummary="${junit.printsummary}" haltonfailure="${junit.haltonfailure}" haltonerror="${junit.haltonerror}" showoutput="${junit.showoutput}" fork="true" forkmode="once" failureProperty="failed">
        <sysproperty key="clover.initstring" value="${clover.dbdir}/${clover.dbfile}" />
        <sysproperty key="rules.location" value="${classes.dir}/rules/impl" />
        <classpath>
            <path refid="classes.classpath" />
            <path refid="test.classpath" />
            <pathelement path="${basedir}/../../.." />
            <pathelement path="${test.classes.dir}" />
            <path location="${basedir}/../common/target/test_classes" />
            <pathelement location="${3rdparty.dir}/prime-server-framework/framework-core-mock.jar" />
        </classpath>
        <formatter type="${unittest.output.type}" />
        <batchtest fork="true" todir="${junit_reports.dir}">
            <fileset dir="${test.classes.dir}" includes="${tests.patternset}" />
        </batchtest>
    </junit>
</target>

在Junit5上運行測試的新目標:

<target name = "sampletest">
    <mkdir dir="${junit_reports.dir}" />
    <junitlauncher>
        <classpath>
            <path refid="classes.classpath" />
            <path refid="test.classpath" />
            <pathelement path="${basedir}/../../.." />
            <pathelement path="${test.classes.dir}" />
            <path location="${basedir}/../common/target/test_classes" />
        </classpath>
        <!--<testclasses outputdir="${junit_reports.dir}">
          <fileset dir="${test.classes.dir}">
              <include name = "**/*Test.class"/>
          </fileset>
        </testclasses>-->
        <test name = "impl.RulesEngineValidationTest"/>
    </junitlauncher>
</target> 

如何在新目標中提供系統屬性?

Ant 1.10.4確實支持JUnit 5.但是,它不支持Ant集成JUnit 4所具有的所有功能。 特別是,它不支持分支junit進程並因此傳遞系統屬性。

我發現了這個問題,因為我試圖做同樣的事情。 我發現了一個解決方法。 您可以在調用junitlauncher之前在代碼中設置系統屬性。

這段代碼是我用來為文件編碼設置單個系統屬性的代碼。 您可以為您的房產做類似的事情。

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('file.encoding', 'ISO8859_1')
  ]]>
</script>

由於您的房產使用其他房產,因此您的情況稍微復雜一些。 您可以從代碼中讀取Ant變量。 (我不知道如何在名稱中讀取一個點,所以我在這個例子中刪除了點)

<property name="cloverdbdir" value="clover-dir-property-value" />
<property name="cloverdbfile" value="clover-db-file-property-value" />

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('clover.initstring', cloverdbdir + '/' + cloverdbfile);
    print(imports.System.getProperty('clover.initstring'));
  ]]>
</script>

如果您使用此技術,有幾點需要注意:

  1. Nashorn已被棄用。 它肯定是在Java 11中。但是,並不保證所有未來的版本。 Ant似乎可能會在當時添加系統屬性功能,所以我並不擔心它。
  2. 系統屬性保持為構建的其余部分設置。 這看起來不像是一個問題。 如果是,則在調用JUnit以使其無效后,您需要另一個腳本塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM