TestNG参数化测试之支持数据对象

            

Junit4与TestNG对比

 

可以参考的文献有:
  • In pursuit of code quality: JUnit 4 vs. TestNG
http://www.ibm.com/developerworks/java/library/j-cq08296/  
  • JUnit 4 Vs TestNG – Comparison
http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/  
  • TESTNG与JUNIT4对比
http://yingfangming.blog.163.com/blog/static/165802470201092954857138/  


自己已经实践过的TestNG特性有:  
1.TestNG可以使用Group  
2.可以重新运行失败的测试用例  
3.TestNG支持多线程,可以在Xml文件中直接配置  
4.TestNG有dependsOnMethods
 

           

package com.easyway.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
 * TestNG参数化 测试的使用
 * @author longgangbai
 * 
 * 
 * @DataProvider(name = "test1")   采用dataProvider注解,name为数据集的名称
 * @Test(dataProvider = "test1")  的dataProvider的值必须为@DataProvider的name值。
 * 
 * 2013-11-19  下午2:42:29
 *
 */
public class TestNGParameter {
	/**
	 * 采用dataProvider注解,name为数据集的名称
	 * @return
	 */
	@DataProvider(name = "test1")  
	public Object[][] createData1() {  
		 return new Object[][] {  
		   { "Cedric", new Integer(36) },  
		   { "Anne", new Integer(37)},   
		 };  
	}  
    /**
     * 
     * @param n1
     * @param n2
     */
	@Test(dataProvider = "test1")  
	public void verifyData1(String n1, Integer n2) {  
	    System.out.println(n1 + " " + n2);  
	} 
}

 

 执行结果如下:

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--476133712\testng-customsuite.xml

Cedric 36
Anne 37
PASSED: verifyData1("Cedric", 36)
PASSED: verifyData1("Anne", 37)

===============================================
    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.EmailableReporter2@8f57a: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@14e3372: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@de648: 20 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@f9aa66: 10 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1b7a531: 0 ms

 

在testNG中如果没有配置testng.xml将自动生成一个默认的xml如上:

C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--476133712\testng-customsuite.xml

 

 

 

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