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 ) {
|
testng.xml
<parameter name= "number-of-times" value= "10" />
|
< class name= "TestWebServer" />
|
<parameter name= "number-of-times" value= "20" />
|
< class name= "TestWebServer" />
|
<parameter name= "number-of-times" value= "30" />
|
< class name= "TestWebServer" />
|
This can become quickly impossible to manage, so instead, you should use a factory:
WebTestFactory.java
public class WebTestFactory {
|
public Object[] createInstances() {
|
Object[] result = new Object[ 10 ];
|
for ( int i = 0 ; i < 10 ; i++) {
|
result[i] = new WebTest(i * 10 );
|
and the new test class is now:
WebTest.java
private int m_numberOfTimes;
|
public WebTest( int numberOfTimes) {
|
m_numberOfTimes = numberOfTimes;
|
public void testServer() {
|
for ( int i = 0 ; i < m_numberOfTimes; i++) {
|
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) {
|
static public Object[][] dp() {
|
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 },
};
}
}
相关推荐
1、testng测试报告模板BeautifulReport 2、testng.xml文件
讲解如何使用Selenium 和 TestNG 进行编程式测试
适用于想了解TestNG测试的同学
TestNG-Abbot源自于两个...本文介绍使用TestNG-Abbot测试GUI过程中难度最大的部分,即理解用户场景的实现过程。一旦理解了它,您会发现将GUI组件隔离并使用框架所含的极其方便的fixture对象对其进行验证是多么地简单。
java+selenium+maven+testng自动化测试框架实例(实际项目)
开源自动化测试框架testng包,用于appium+java+android测试
testng下一代测试框架
ant+selenium+testng自动化测试方案
《Java测试新技术:TestNG和高级概念》介绍了Java测试的新技术,主要内容包括:基本概念、测试设计模式、企业级测试、Java EE测试、集成和扩展TestNG等。《Java测试新技术:TestNG和高级概念》通过针对有效测试Java...
《Java测试新技术TestNG和高级概念》介绍了Java测试的新技术,主要内容包括:基本概念、测试设计模式、企业级测试、Java EE测试、集成和扩展TestNG等。《Java测试新技术TestNG和高级概念》通过针对有效测试Java应用...
无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。 目录: 第1章 起步 1.1 超越JUnit 3 1.2 JUnit 4 1.3 针对可测试性而...
本书主要内容包括: 展示了与测试有关的折衷考虑,这样您就能在测试...无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。
TestNG中文手册及学习笔记,包括:环境配置、详细使用说明及实例。
httpclient testgn 接口测试
集成 Jenkins 和 TestNG 实现自助式自动化测试平台,测试可行,。
Java + IDEA +TestNG 集成 Allure生成一套可视化测试报告 关于 Allure2: 1. 支持多语言多框架集成:包括java的testng、junit单元测试框架,和python的pytest等框架集成,满足当前行业里主流测试开发工程师手撕的...
TestNG是一个测试框架,其灵感来自JUnit和NUnit,但引入了一些新的功能,使其功能更强大,使用更方便。TestNG是一个开源自动化测试框架;TestNG表示下一代(Next Generation的首字母)。 TestNG类似于JUnit(特别是JUnit...
本书主要内容包括: 展示了与测试有关的折衷考虑,这样您就能在测试...无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。
配合先前的代码demo,进行了一个系统的学习总结
Selenium自动化测试框架视频-Testng使用介绍(自动化测试)