TestNG中数据参数化的优化方法 [英] Optimized Way for Data Parameterization in TestNG
问题描述
我正在尝试使用硒和TestNG创建一个框架.作为框架的一部分,我正在尝试实现数据参数化.但是我对实现数据参数化的优化方法感到困惑.这是我采用的以下方法.
- 使用数据提供程序(从excel中我正在读取并存储在object [] []中)
- 使用testng.xml
数据提供者问题:
- 让我们说一下,如果我的测试需要处理大量数据(例如15种不同的数据),那么我需要将15个参数传递给它.或者,如果我尝试创建一个TestData类来处理此参数并在其中维护,那么对于每个Test,都会有不同的数据集.因此我的TestData类将充满40多种不同的参数.
例如:在一个Ecom网站上,将存在许多不同的参数,例如帐户,卡,产品,奖励,历史记录,存储位置等,为此,我们可能需要至少40种不同的参数需要在测试数据中声明我认为这不是一个可取的解决方案.其他一些测试有时可能需要10个不同的测试数据,有些可能需要12个.即使在一次测试中的某些时候,一次迭代我也只需要7个参数.
我如何有效地管理它?
与Testng.xml有关的问题
在单个xml文件中维护20个不同的帐户,40个不同的产品详细信息,卡片,历史记录,并配置测试套件(如并行执行),仅配置要执行的特定类等,所有这些都会使testng.xml文件混乱.
因此,请您提出建议,这是在Testing Framework中处理数据的一种优化方法. 如何实时处理数据参数化,具有不同测试数据的迭代
假定每个测试都知道它将要接收哪种测试数据,这就是我建议您执行的操作:
- 让您的testng套件xml文件传递将从中读取数据的文件名到数据提供者.
- 构建数据提供者,使其通过TestNG参数接收要读取的文件名,然后构建通用映射作为测试数据迭代(每个测试都将其参数作为键,值对映射接收),然后与传入的地图.
这样,您将只剩下一个可以真正处理任何事情的数据提供者.通过使数据提供者处理测试方法,然后相应地提供值,可以使它变得更加复杂.
这是我所谈论的内容的基本实现.
public class DataProviderExample {
@Test (dataProvider = "dp")
public void testMethod(Map<String, String> testdata) {
System.err.println("****" + testdata);
}
@DataProvider (name = "dp")
public Object[][] getData(ITestContext ctx) {
//This line retrieves the value of <parameter name="fileName" value="/> from within the
//<test> tag of the suite xml file.
String fileName = ctx.getCurrentXmlTest().getParameter("fileName");
List<Map<String, String>> maps = extractDataFrom(fileName);
Object[][] testData = new Object[maps.size()][1];
for (int i = 0; i < maps.size(); i++) {
testData[i][0] = maps.get(i);
}
return testData;
}
private static List<Map<String, String>> extractDataFrom(String file) {
List<Map<String, String>> maps = Lists.newArrayList();
maps.add(Maps.newHashMap());
maps.add(Maps.newHashMap());
maps.add(Maps.newHashMap());
return maps;
}
}
I am trying to create a framework using selenium and TestNG. As a part of the framework i am trying to implement Data Parameterization. But i am confused about optimized way of implementing Data parameterization. Here is the following approaches i made.
- With Data Providers (from excel i am reading and storing in object[][])
- With testng.xml
Issues with Data Providers:
- Lets say if my Test needs to handle large volumes of data , say 15 different data, then i need to pass 15 parameters to it. Alternative , if i try to create a class TestData to handle this parameters and maintain in it , then for every Test there will be different data sets. so my TestData class will be filled with more than 40 different params.
Eg: In a Ecom Web site , There will be many different params exists like for Accounts , cards , Products , Rewards , History , store Locations etc., for this we may need atleast 40 different params need to declared in Test Data.Which i am thinking not a suggestable solution. Some other tests may need sometimes 10 different test data, some may need 12 . Even some times in a single test one iteration i need only 7 params in other iteration i need 12 params .
How do i manage it effectively?
Issues with Testng.xml
Maintaining 20 different accounts , 40 different product details , cards , history all in a single xml file and configuring test suite like parallel execution , configuring only particular classes to execute etc., all together will mess the testng.xml file
So can you please suggest which is a optimized way to handle data in Testing Framework . How in real time the data parameterization , iterations with different test datas will be handled
Assuming that every test knows what sort of test data it is going to be receiving here's what I would suggest that you do :
- Have your testng suite xml file pass in the file name from which data is to be read to the data provider.
- Build your data provider such that it receives the file name from which to read via TestNG parameters and then builds a generic map as test data iteration (Every test will receive its parameters as a key,value pair map) and then work with the passed in map.
This way you will just one data provider which can literally handle anything. You can make your data provider a bit more sophisticated by having it deal with test methods and then provide the values accordingly.
Here's a skeleton implementation of what I am talking about.
public class DataProviderExample {
@Test (dataProvider = "dp")
public void testMethod(Map<String, String> testdata) {
System.err.println("****" + testdata);
}
@DataProvider (name = "dp")
public Object[][] getData(ITestContext ctx) {
//This line retrieves the value of <parameter name="fileName" value="/> from within the
//<test> tag of the suite xml file.
String fileName = ctx.getCurrentXmlTest().getParameter("fileName");
List<Map<String, String>> maps = extractDataFrom(fileName);
Object[][] testData = new Object[maps.size()][1];
for (int i = 0; i < maps.size(); i++) {
testData[i][0] = maps.get(i);
}
return testData;
}
private static List<Map<String, String>> extractDataFrom(String file) {
List<Map<String, String>> maps = Lists.newArrayList();
maps.add(Maps.newHashMap());
maps.add(Maps.newHashMap());
maps.add(Maps.newHashMap());
return maps;
}
}
这篇关于TestNG中数据参数化的优化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!