是否可以将属性文件值作为参数值传递给 testng.xml? [英] Is it possible to pass property file value to testng.xml as parameter value?

查看:57
本文介绍了是否可以将属性文件值作为参数值传递给 testng.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将属性文件值作为参数值传递给testng.xml?

Is it possible to pass property file value to testng.xml as parameter value?

示例:

test.properties:

test.properties:

browser = FIREFOX

testng.xml:

testng.xml:

parameter name = "browser" value = "${test.browser}"

推荐答案

是的,这可以通过 过滤 testng.xml 测试资源.

Yes, this is possible by filtering the testng.xml test resource.

在您的 POM 中,您需要声明一个指向属性文件的过滤器,并覆盖 以过滤 testng.xml 资源.

Inside your POM, you need to declare a filter poiting to your properties files and override the <testResources> to filter the testng.xml resource.

<build>
  <filters>
    <filter>path/to/test.properties</filter> <!-- relative to location of POM -->
  </filters>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <includes>
        <include>testng.xml</include>
      </includes>
      <filtering>true</filtering>
    </testResource>
    <testResource>
      <directory>src/test/resources</directory>
      <excludes>
        <exclude>testng.xml</exclude>
      </excludes>
    </testResource>
  </testResources>
  <!-- rest of build section -->
</build>

如果您的属性文件包含一个键 test.browser,这将正确地用该键的值替换 plaholder ${test.browser}.所有这些都将在测试启动之前发生,因此 TestNG 将看到替换了值的过滤文件.

If you properties file contains a key test.browser, this will correctly replace the plaholder ${test.browser} by the value of that key. All of this will happen before the tests are launched so TestNG will see the filtered file with the values replaced.

注意上面的配置只过滤testng.xml,避免过滤过多.您可以扩展它以过滤多个文件.

Note that the above configuration only filters testng.xml to avoid filtering too much. You could extend that to filter multiple files.

这篇关于是否可以将属性文件值作为参数值传递给 testng.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆