TestNG官方网站:
http://testng.org/doc/documentation-main.html
官方文档:
5.6.2 - Parameters with DataProviders
Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with @DataProvider:
Java
@DataProvider (name = "test1" )
|
public Object[][] createData1() {
|
{
"Cedric" ,
new Integer( 36 ) },
|
{
"Anne" ,
new Integer( 37 )},
|
@Test (dataProvider = "test1" )
|
public void verifyData1(String n1, Integer n2) {
|
System.out.println(n1 + " " + n2);
|
will print
A @Test method specifies its Data Provider with the dataProvider attribute. This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.
By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method and you specify the class where it can be found in the dataProviderClass attribute:
StaticProvider.java
public class StaticProvider {
|
@DataProvider (name =
"create" )
|
public static Object[][] createData() {
|
new Object[] { new Integer( 42 ) }
|
@Test (dataProvider =
"create" , dataProviderClass = StaticProvider. class )
|
public void test(Integer n) {
|
The Data Provider method can return one of the following two types:
- An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the cast illustrated by the example above.
- An Iterator<Object[]>. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.
Here is an example of this feature:
@DataProvider (name = "test1" )
|
public Iterator<Object[]> createData() {
|
return new MyIterator(DATA);
|
If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for.
For example, the following code prints the name of the test method inside its @DataProvider:
@DataProvider (name = "dp" )
|
public Object[][] createData(Method m) {
|
System.out.println(m.getName());
|
return new Object[][] { new Object[] { "Cedric" }};
|
@Test (dataProvider = "dp" )
|
public void test1(String s) {
|
@Test (dataProvider = "dp" )
|
public void test2(String s) {
|
and will therefore display:
Data providers can run in parallel with the attribute parallel:
@DataProvider (parallel = true )
|
Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in the <suite> tag of your XML file:
< suite name = "Suite1" data-provider-thread-count = "20" >
|
If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.
5.6.3 - Parameters in reports
Parameters used to invoke your test methods are shown in the HTML reports generated by TestNG. Here is an example:
在测试中如果参数数据集被多个测试方法调用并且并发情况下,我们怎么知道是那个方法执行了出现错误呢?那么我们可以采用带参数的数据集测试即可。
package com.easyway.testng;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
*
* 参数化测试,并发且指定方法的Method用于在测试时候判断执行的方法的名称。
*
* @author longgangbai
* 2013-11-19 下午2:49:25
*
*/
public class DynamicDataTest {
@DataProvider(name = "dp",parallel=true)
public Object[][] createData(Method m) {
//用于在数据集合在多个测试方法使用时候知道那个方法调用。
System.out.println(m.getName()); // print test method name
return new Object[][] { new Object[] { "Cedric" }};
}
@Test(dataProvider = "dp")
public void test1(String s) {
}
@Test(dataProvider = "dp")
public void test2(String s) {
}
}
测试结果:
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1274939779\testng-customsuite.xml
test1
test2
PASSED: test1("Cedric")
PASSED: test2("Cedric")
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@33a6b8: 10 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@a14c53: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@17035c6: 20 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@773a1: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@94837a: 0 ms
相关推荐
仅仅用于初学者交流,意在与自动化测试初学者进行相互学习交流,简单的实现了java+selenium+testng参数化打开百度搜索
Abbot是一种JUnit扩展框架,主要目的是使GUI组件实现编程隔离,它还提供了一种验证GUI行为的简易方法。TestNG-Abbot是一种测试框架,它为GUI组件的测试带来了新的活力。本文介绍使用TestNG-Abbot测试GUI过程中难度...
集成 Jenkins 和 TestNG 实现自助式自动化测试平台,测试可行,。
主要对举例对国家气象局接口自动化测试进行讲解(Get请求及结果断言),以达到自动化测试入门目的,需要有一定的JAVA知识(HTTP相关)。
java+selenium+maven+testng自动化测试框架实例(实际项目)
1、testng测试报告模板BeautifulReport 2、testng.xml文件
Jenkins和TestNG搭建一个自助式的自动化测试平台。在软件业十分成熟的今天,敏捷(Agile)开发在业界日益流行,而面临的挑战也日益增多,不断变化的用户需求、缩短的开发周期、频繁的部署上线、复杂的产品架构和团队...
ant+selenium+testng自动化测试方案
Selenium自动化测试框架视频-Testng使用介绍(自动化测试)
适用于想了解TestNG测试的同学
全栈自动化测试实战 基于testng,httpclient,selenium.appium 书本上的东西
《Java测试新技术:TestNG和高级概念》通过针对有效测试Java应用程序以及围绕可测试性来设计应用程序和组件展示了这些有效的测试技术,并给出了每种测试方法的优点和不足,展示了解决常见问题的不同选择。《Java测试...
开源自动化测试框架testng包,用于appium+java+android测试
自己整理的使用Appium+TestNG实现的Android自动化测试。包括安装配置、真机测试说明和一个简单的Android App源码、一个针对该App的测试
E:\最全面的Java接口自动化测试实战\第13章 TestNg+MyBatis实现数据校验 E:\最全面的Java接口自动化测试实战\第14章 持续集成 E:\最全面的Java接口自动化测试实战\第15章 课程总结 E:\最全面的Java接口自动化测试...
spring集成TestNG与Mockito框架单元测试方法,方便学习者能快速上手如何单测,对自己的代码能够有质量保证。
TestNG系列教程:并行执行测试 目录 1. 并行执行测试的优势 2. 如何并行地执行测试方法 3. 如何并行地执行测试类 4. 如何并行地执行同一测试套件内的各个测试组件 5. 如何配置需要在多线程环境中执行的测试方法
《Java测试新技术TestNG和高级概念》通过针对有效测试Java应用程序以及围绕可测试性来设计应用程序和组件展示了这些有效的测试技术,并给出了每种测试方法的优点和不足,展示了解决常见问题的不同选择。《Java测试新...
Selenium+TestNG自动化测试
万事开头难,其实开过头后也不容易。测试方面的技术好多啊,多的像天上的繁星,一.俩.仨.四个…在有限的时间里,想做最多的了解,只能从最实用的着手,去除教条式的金科玉律。...测试方法可以传入参数; 测试类不