The @ factory annotation of testng literally means that the factory method is used to create test data and complete the test together.
The main response scenario is: for a test case or method, we need to input multiple test data for testing, in addition, the test data can be related to each other (it can be controlled by code ),
In this case, we can combine multiple test cases that are different from the test data during automated or manual testing into one test case for more convenient and quick testing,
Saving a lot of time for developers who write automated test code
Policy: Generally, we call the test class in the method marked with @ factory annotation. At this time, testng automatically calls the method with @ test annotation in the test class.
Java code:
/***** <P> * Title: testngfactory * </P> ** <p> * Description: configuration file: testng-factory.xml ** testngfactory factory class, call the tested class in a method with the @ factory annotation. testng automatically calls the method with the @ test annotation in the executed class. * The tested class is testngfactorytest, * </P> ** <p> * company: * </P> ** @ Author: Dragon ** @ Date: october 22, 2014 */public class testngfactory {@ factorypublic object [] createinstances () {object [] result = new object [10]; for (INT I = 0; I <10; I ++) {result [I] = new testngfactorytest (I * 10);} return result ;}}
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: you only need to configure the class with the @ factory annotation.
<? 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. testngfactory "> </class> </classes> </test> </suite>
Test results:
num 1 m_numberOfTimes :30num 2 m_numberOfTimes :20num 3 m_numberOfTimes :70num 4 m_numberOfTimes :60num 5 m_numberOfTimes :90num 6 m_numberOfTimes :50num 7 m_numberOfTimes :10num 8 m_numberOfTimes :0num 9 m_numberOfTimes :40num 10 m_numberOfTimes :80PASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServerPASSED: testServer=============================================== TestMethods Tests run: 10, Failures: 0, Skips: 0===============================================
Testng factory test reference @ dataprovider Data Source-flexible use of factory test
-- You need to know that there are too many unknown pains behind others' glamorous scenes. people you don't like should smile and bless them silently. For those you like, it is good to reveal the truth and treat each other with sincerity. People are watching things in the sky, and there is a cause and effect in the arrangement, always with a kind of heart, continue to do the right thing. Always remind yourself to change yourself, keep a low profile, and do things in a high profile.
Testng @ factory description ------ how to test parameter value Variability