Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG DataProvider reading test data from the testng.xml config file?

Tags:

testing

testng

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.

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.

The following testing.xml is obvious invalid but I am just trying to show the kind of thing I would like to do:

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

like image 513
djangofan Avatar asked Jan 22 '14 17:01

djangofan


People also ask

How is DataProvider different from passing parameters from TestNG XML file?

DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG.

How do u fetch data in DataProvider?

Step 1: First create a method to read excel data and return string array. Step 2: Create before class and after class methods which helps in getting the browser and closing them when done. Step 3: Create a data provider which actually gets the values by reading our excel sheet.

How does DataProvider work in TestNG?

Note: TestNG comes up with DataProvider to automate the process of providing test-cases for execution. DataProvider helps with data-driven test cases that carry the same methods but can be run multiple times with different data sets. It also helps in providing complex parameters to the test methods.


1 Answers

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");
}

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

test class

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);
}

}

Output

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

Param1: val1
Param2: val2

===============================================
suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
like image 101
user1058106 Avatar answered Oct 01 '22 00:10

user1058106