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

标签 java unit-testing ant testng

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

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

我看到了多个帖子,但没有一个对我有用:

  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. 此链接无效: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 变量进行设置,但它们都不起作用。

最佳答案

在代码中,你应该使用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>

关于java - 在 ANT 构建工具中设置系统变量并使用 Syste.getEnv ("ABC_HOME"在 Java 中读取它),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574336/

相关文章:

java - 基于操作系统的不同风格

java - 使用 writer 在 Clojure 中编写 CSV 文件

unit-testing - 如何标记 SUnit 中的预期失败?

Android 总是抛出 ClassCastException

apache-flex - 将 Ant 与 FlexBuilder 一起使用

java - Ant Junit 测试通过 Ant 运行的速度比通过 IDE 慢得多 - 要看什么?

java - 使用Java循环制作图案

java - org.postgresql.util.PSQLException : ERROR : relation does NOT exist PreparedStatement.executeQuery()

unit-testing - 初学者单元测试 - 测试方法的复杂输入

python - 只为单元测试启用 Python 代码?