我正在运行来自 Ant 构建工具的所有 testng 测试用例。所有配置都已完成,我可以通过 ANT 工具运行它们并生成报告。现在问题来了:
在运行所有测试用例时,他们使用 System.getEnv("ABC_HOME")
读取环境变量。现在我的所有测试用例都失败了,或者由于配置失败而被跳过。
我看到了多个帖子,但没有一个对我有用:
设置系统变量:How to read in arguments passed via ant to testng.xml
控制所有测试用例如何从 ANT 工具运行它们。 How do I control which tests to run in testng from ant?
我设置系统变量的配置如下。如果我错了请纠正我:
<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/