在 ANT 构建工具中设置系统变量并使用 Syste.getEnv("ABC_HOME") 在 Java 中读取它

Setting System variable in ANT build tool and read it in Java using Syste.getEnv("ABC_HOME")

我运行正在使用 Ant 构建工具中的所有 testng 测试用例。所有配置都已完成,我可以通过 ANT 工具 运行 它们并可以生成报告。现在问题来了:

在 运行 宁所有测试用例时,他们使用 System.getEnv("ABC_HOME") 读取环境变量。现在我所有的测试用例都失败了,或者因为配置失败而被跳过。

我看到了多个 post,但其中 none 适合我:

  1. 如何配置:http://testng.org/doc/ant.html

  2. 设置系统变量:How to read in arguments passed via ant to testng.xml

  3. 控制所有测试用例如何从 ANT 工具 运行 它们。 How do I control which tests to run in testng from ant?

  4. 这个link不起作用:How to set an env variable in Ant build.xml

我设置系统变量的配置如下。如有错误请指正:

   <taskdef resource="testngtasks" classpath="${unitTest.lib.dir}/testng.jar"/>


    <target name="runUnittests" depends="unitTestCompile">
      <property name="ABC_HOME" value="${base.dir}"/>
      <testng delegatecommandsystemproperties="true" classpathref="unitTest.classpath"
        outputDir="${testng.report.dir}" workingDir="${unitTest.src.dir}" haltOnfailure="true">
        <sysproperty key="property" value="${BLUEOPTIMA_HOME}"/>
        <xmlfileset dir="${unitTest.suites.dir}" includes="testng.xml"/>
      </testng>
    </target>

每次我的 Java 代码查找变量 ABC_HOME 时,它都找不到该环境变量,并且所有测试用例都失败或被跳过。使用 ANT 工具中的 env 变量进行设置,但其中 none 正在运行。

在代码中,您应该使用 System.getProperty("ABC_HOME") 代替。

如果您不能更改代码,那么您可以这样做:

<target name="runUnittests" depends="unitTestCompile">
  <property name="ABC_HOME" value="${base.dir}"/>
  <testng fork="yes" delegatecommandsystemproperties="true" classpathref="unitTest.classpath"
    outputDir="${testng.report.dir}" workingDir="${unitTest.src.dir}" haltOnfailure="true">
    <env key="property" value="${BLUEOPTIMA_HOME}"/>
    <xmlfileset dir="${unitTest.suites.dir}" includes="testng.xml"/>
  </testng>
</target>