有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单元测试在ANT构建工具中设置系统变量,并使用Syste在Java中读取。getEnv(“ABC_主页”)

我正在运行来自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变量进行设置,但它们都不起作用


共 (1) 个答案

  1. # 1 楼答案

    在代码中,应该使用^{}

    如果无法更改代码,则可以执行以下操作:

    <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>