TestNG简单的学习(二)参数化测试并发且多方法测试方法判定

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

 

//This method will provide data to
any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
   {
"Cedric",
new Integer(36) },
   {
"Anne",
new Integer(37)}, 
};
}
 
//This test method declares that
its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1,
Integer n2) {
System.out.println(n1 + "
"
+ n2);
}

will print

Cedric 36
Anne
37

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() {
    return new Object[][] {
      new Object[] { new Integer(42) }
    }
  }
}
 
public class MyTest {
  @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());  // 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) {
}

and will therefore display:

test1
test2

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:

TestNG简单的学习(二)参数化测试并发且多方法测试方法判定

 

       在测试中如果参数数据集被多个测试方法调用并且并发情况下,我们怎么知道是那个方法执行了出现错误呢?那么我们可以采用带参数的数据集测试即可。

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

你可能感兴趣的:(JUnit4,TestNG)