TestNG简单的学习(六)测试工厂注释的使用

TestNG官方网站:

http://testng.org/doc/documentation-main.html

 

官方文档:

5.8 - Factories

Factories allow you to create tests dynamically. For example, imagine you want to create a test method that will access a page on a Web site several times, and you want to invoke it with different values:

TestWebServer.java

 

 
public class TestWebServer {
  @Test(parameters = {
"number-of-times" })
  public void accessPage(int numberOfTimes) {
    while (numberOfTimes-- >
0) {
     //
access the web page
    }
  }
}

 

testng.xml

 

 
<test name="T1">
  <parameter name="number-of-times" value="10"/>
  <class name= "TestWebServer" />
</test>
 
<test name="T2">
  <parameter name="number-of-times" value="20"/>
  <class name= "TestWebServer"/>
</test>
 
<test name="T3">
  <parameter name="number-of-times" value="30"/>
  <class name= "TestWebServer"/>
</test>

This can become quickly impossible to manage, so instead, you should use a factory:

WebTestFactory.java

 

 
public class WebTestFactory {
  @Factory
  public Object[]
createInstances() {
   Object[]
result =
new Object[10];  
   for (int i = 0; i < 10; i++) {
      result[i] = new WebTest(i * 10);
    }
    return result;
  }
}

and the new test class is now:

WebTest.java

 

 
public class WebTest {
  private int m_numberOfTimes;
  public WebTest(int numberOfTimes) {
    m_numberOfTimes = numberOfTimes;
  }
 
  @Test
  public void testServer() {
   for (int i = 0; i < m_numberOfTimes;
i++) {
     //
access the web page
    }
  }
}

 

Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:

 

 
<class name="WebTestFactory" />

 

The factory method can receive parameters just like @Test and @Before/After and it must return Object[]. The objects returned can be of any class (not necessarily the same class as the factory class) and they don't even need to contain TestNG annotations (in which case they will be ignored by TestNG).

 

Factories can also be used with data providers, and you can leverage this functionality by putting the @Factory annotation either on a regular method or on a constructor. Here is an example of a constructor factory:

 

 
@Factory(dataProvider = "dp")
public FactoryDataProviderSampleTest(int n) {
  super(n);
}
@DataProvider
static public Object[][] dp() {
  return new Object[][] {
    new Object[] { 41 },
    new Object[] { 42 },
  };
}

The example will make TestNG create two test classes, on with the constructor invoked with the value 41 and the other with 42.

 

 

 

如果一个测试类的很多方法需要测试,而且这个测试类需要多次测试那么需要使用测试工厂最好。

package com.easyway.testng;

import org.testng.annotations.Test;

/**
 * @author longgangbai
 * 2013-11-19  下午3:02:43
 *
 */
public class WebTest {  

	  private int m_numberOfTimes;  

	  public WebTest(int numberOfTimes) {  

	    m_numberOfTimes = numberOfTimes;  

	  }  

	   

	  @Test 

	  public void testServer() {  

	   for (int i = 0; i < m_numberOfTimes; i++) {  
	    }  

	  }  

	} 

 

制定测试类运行10次:

package com.easyway.testng;

import org.testng.annotations.Factory;

/**
 * @author longgangbai
 * 2013-11-19  下午3:02:21
 *
 */
public class WebTestFactory {  

	  @Factory 
	  public Object[] createInstances() {  

	   Object[] result = new Object[10];    

	   for (int i = 0; i < 10; i++) {  

	      result[i] = new WebTest(i * 10);  

	    }  

	    return result;  

	  }  

	} 

 如果想通过配置制定测试类运行10次:

可以将测试工厂类改写为:

package com.easyway.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;

/**
 * @author longgangbai
 * 2013-11-19  下午3:04:41
 *
 */
public class WebTestConstructFactory {
	@Factory(dataProvider = "dp")  
	public Object[]  createFactory(int n) {  
		   Object[] result = new Object[n];    
		   for (int i = 0; i < n; i++) {  
		      result[i] = new WebTest(i * n);  
		    }  
		   return result;  
	}  
	@DataProvider(name = "dp" ,parallel=true)  
	static public Object[][] dp() {  
	  return new Object[][] {  
	    new Object[] { 41 },  
	    new Object[] { 42 },  
	  };  

	} 

}

 

你可能感兴趣的:(JUnit4,TestNG,不特定执行测试用例,测试工厂)