Testng factory test reference @ dataprovider Data Source-flexible use of factory test

Source: Internet
Author: User
Tags testng

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.