As mentioned before, @ factory is more suitable for testing the variability of parameters of the same type. If the parameter values do not have a specific rule, we can use the @ factory and @ dataprovider combination to perform the test.
Note: Pay attention to the total number of times the test method will be executed, because @ factory itself is a type of cyclic testing, and @ dataprovider is also a type of overall testing cycle.
Java code:
/***** <P> * Title: testngfactorydataprovider * </P> ** <p> * configuration file: testng-factoryDataProvider.xml ** description: * use @ dataprovider as the data injection method to inject data into the factory and run the test. The tested class is testngfactorytest * </P> ** <p> * company: * </P> ** @ Author: Dragon ** @ Date: July 15, October 22, 2014 */public class testngfactorydataprovider {@ Factory (dataprovider = "datasource ") public object [] createinstances (int A) {system. out. println (a); object [] result = new object [a]; for (INT I = 0; I <A; I ++) {result [I] = new testngfactorytest (I * 10);} return result ;}@ dataprovider (name = "datasource") Public object [] [] getdatasource () {return new object [] [] {new object [] {2}, new object [] {4 }};}}
public class TestngFactoryTest {private int m_numberOfTimes;public TestngFactoryTest(int numberOfTimes) {this.m_numberOfTimes = numberOfTimes;}private static int num;@Testpublic void testServer() {num++;System.out.println("num " + num + " m_numberOfTimes :"+ m_numberOfTimes);}}
Configuration file:
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype suite System "http://testng.org/testng-1.0.dtd"> <! -- The default value of allow-return-values is false, indicates that the returned value will be ignored --> <suite name = "framework_testng" allow-return-values = "true"> <Test verbose = "2" name = "testmethods"> <classes> <class name = "com. dragon. testng. annotation. testngfactorydataprovider "> </class> </classes> </test> </suite>
Test results:
num 1 m_numberOfTimes :20num 2 m_numberOfTimes :10num 3 m_numberOfTimes :0num 4 m_numberOfTimes :30num 5 m_numberOfTimes :10num 6 m_numberOfTimes :0PASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServer=============================================== TestMethods Tests run: 6, Failures: 0, Skips: 0===============================================
If I say yes,
Don't think I am clumsy. Because I understand,
The virtues can carry things and help people be happy.
Testng factory test reference @ dataprovider Data Source-flexible use of factory test