TestNG DataProvider 从 testng.xml 配置文件中读取测试数据? [英] TestNG DataProvider reading test data from the testng.xml config file?

查看:30
本文介绍了TestNG DataProvider 从 testng.xml 配置文件中读取测试数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TestNG DataProvider 是否可以从 testng.xml 配置文件中读取测试数据?或者出于某种原因这是不现实的?我希望能够在套件级别和类级别从该文件读取测试数据.

Is it possible for a TestNG DataProvider to read test data from the testng.xml config file? Or is this unrealistic for some reason? I would like to be able to read test data from that file at the suite level and class level.

那么,给定一个这样的 testing.xml 文件(我不确定它是否现实),我该怎么做?我之前使用 XStream(或 Jackson)编写了一个 DataProvider,所以我非常精通我自己的自定义 .xml 格式,但是坚持 testing.xml 的严格格式是我担心的地方.

So, given a testing.xml file like this (which I am unsure is realistic or not), how would I do this? I have written a DataProvider using XStream (or Jackson) before and so I am well versed in my own custom .xml format, but sticking to the strict format of the testing.xml is where I am worried about this.

以下 testing.xml 显然无效,但我只是想展示我想做的事情:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="TestAll"> 
  <parameter name="hubUrl" value="http://localhost:4444/wd/hub"/>
  <parameter name="reportFile" value="CustomReport.html"/>
  <test name="etsy">
    <parameter name="reportFile" value="CustomReport.html"/>
    <classes>
      <class name="qa.examples.suite.TestSearch">
        <parameter name="appUrl"  value="http://etsy.com" type="java.lang.String"/> 
        <parameter name="browser"  value="Firefox" type="java.lang.String"/>    
        <parameter name="testEnabled" value="true" type="java.lang.Boolean"/> 
        <methods>                 
          <include name="testEtsySearch"/>
            <tests>
              <test>
                <parameter name="testNum" value="1" type="java.lang.Integer"/>
                <parameter name="searchTerm" value="cell phone" type="java.lang.String"/>
                <parameter name="searchTerm" value="batteries" type="java.lang.String"/>
              </test>
              <test>
                <parameter name="testNum" value="2" type="java.lang.Integer"/>
                <parameter name="searchTerm" value="buttons" type="java.lang.String"/>
                <parameter name="searchTerm" value="metal" type="java.lang.String"/>
              </test>
            </tests>
          </include>                       
        </methods> 
      </class>
      <class name="qa.examples.suite.TestFilters" />
    </classes>
    </test> 
</suite>

那么,这样的事情可能吗?如果是这样,你会怎么做?

So, is something like this possible? If so, how would you do it?

推荐答案

尝试将 ITestContext 作为数据提供程序参数传递.类似的东西:

Try to pass ITestContext as a data provider parameter. Something like:

@DataProvider(name = "DataProvider")
public static Object[][] Provider(ITestContext context) throws Exception
{
    String dataFile = context.getCurrentXmlTest().getParameter("dataFile");
}

套件xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="suite"> 
  <parameter name="param1" value="val1"/>
  <test name="test">
    <parameter name="param2" value="val2"/>
    <classes>
      <class name="test.TestClass1" />
    </classes>
  </test> 
</suite>

测试类

package test;
import java.util.Map;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClass1 {

@DataProvider(name="Provider")
public Object[][] provider(ITestContext context)
{
 Map<String, String> testParams = context.getCurrentXmlTest().getLocalParameters();
 Map<String, String> suiteParams=context.getCurrentXmlTest().getSuite().getParameters();

 return new Object[][]{{suiteParams.get("param1"), testParams.get("param2")}};
}

@Test(dataProvider="Provider")
public void test1(String param1, String param2)
{
 System.out.println("Param1: " + param1);
 System.out.println("Param2: " + param2);
}

}

输出

[TestNG] Running:
/home/nightmare/workspace/test/suite.xml

Param1: val1
Param2: val2

===============================================
suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

这篇关于TestNG DataProvider 从 testng.xml 配置文件中读取测试数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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