Brief introduction
Recently busy decorating blog park, no time to update the article, is finally taking the time to write the last half of the finished article to the new blog style garden, I hope you like it. Today testng continue to introduce relevant knowledge - factory.
Factory allows you to create dynamic test. For example, suppose you need to create a test method, and use it to access a web page multiple times, and each time with different parameters:
@Factory notes from the literal meaning in terms of the method is the use of plants to create test data and with the completion of the test, the main scenario is to deal with: for a test case or a method, we need to enter multiple test data for testing, and these tests the data may be a certain relationship (through code control), this time, we can put multiple test cases because different test data encountered when automated or manual testing combined into a test case to a more convenient and quick test.
To write your own code for automated testing personnel saves a lot of time
Strategy: We will have a general method @Factory notes in class standard in the test call, then TestNg will automatically test class with a method annotated calls @Test
Profile: only need to configure class can be annotated with @Factory
@Factory must be placed in a return to the top of the array of objects, all of these objects contains an instance of the test class, testng will ensure @Factory only called once.
@Factory method is the first to be called, before @Test method and configuration, only when all the @Factory method is called, testng began to perform configuration and test methods.
@Factory allows for dynamic tests at runtime.
On top of that so much is not to say that everyone's foggy, disoriented, followed by specific examples to students and small partners to share.
Examples
Why use it @Factory notes, first look at the following example
The test class Person:
Reference Code:
Package Penalty for hongge; / ** * @author Beijing - Hong Columbia * * the Java automation testing framework -07 - TestNG's Factory chapter * * 6 November 2019 * / Import org.testng.annotations.Parameters; Import org.testng. annotations.Test; public class the Person { String name; int Age; @Parameters ({ "name", "Age" }) public the Person (String name, int Age) { Super (); the this .name = name; the this .age = Age; } @Test () public void say () { of System.out.print ( "I" + name + "" ); IF (Age <18 is ) { System.out.println ( "I Minor" ); } the else IF ( Age> = 18 is && Age <= 45 ) { System.out.println ( "I am young" ); } the else IF (Age> 45 && Age <= 60 ) { System.out.println ( "I am a middle-aged" ); } the else IF (Age> 60 ) { System.out.println ( "I am older."); } } }
Class say () method has four branch judgment, in order to fully test must be performed four times this method, if you do not use @Factory notes, must be so configured in TestNG configuration file:
TestNG configuration file
Reference Code
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test1"> <parameter name="name" value="小明" /> <parameter name="age" value="10" /> <classes> <class name="hongge.Person" /> </classes> </test> <test name="Test2"> <parameter name="name" value="宏哥" /> <parameter name="age" value="20" /> <classes> <class name="hongge.Person" /> </classes> </test> <test name="Test3"> <parameter name="name" value="刘创" /> <parameter name="age" value="50" /> <classes> <class name="hongge.Person" /> </classes> </test> <test name="Test4"> <parameter name="name" value="爷爷" /> <parameter name="age" value="70" /> <classes> <class name="hongge.Person" /> </classes> </test><!-- Test --> </suite> <!-- Suite -->
We can clearly see it from the top: Once the parameters increased, it is difficult to manage, so you should use the factory to do
factory
Factory Notes
If @Factory annotation, relatively simple, and easy to extend, as an example.
Without changes to the original class, add a new class PersonFactory
The new class PersonFactory
Reference Code
Package Penalty for hongge; Import java.util.ArrayList; Import org.testng.annotations.Factory; / ** * @author Beijing - Hong Columbia * * the Java automation testing framework -07 - TestNG's Factory chapter * * November 6, 2019 * / public class PersonFactory { @Factory public Object [] Factory () { the ArrayList <the Person> = testlist new new the ArrayList <> (); the Person TP = new new the Person ( "clearly", 10 ); testList.add (TP); the Person TP2 = new new the Person ( "macro man", 20); testList.add(tp2); Person tp3 = new Person("刘创",50); testList.add(tp3); Person tp4 = new Person("朱爷爷",70); testList.add(tp4); return testList.toArray(); } }
Runtime, in Eclipse, right-click on the factory class (PersonFactory), Run As-> TestNg test execution,
You can also configure the testng.xml, you only need to configure the factory class, the original class do not control.
testng.xml
Reference Code
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test1"> <classes> <class name="hongge.PersonFactory" /> </classes> </test><!-- Test --> </suite> <!-- Suite -->
operation result
You only need to refer to the testng.xml class contains factory methods, and test cases themselves are created at runtime:
<class name="WebTestFactory"/>
Factory methods can take such @Test and @ Before / After marked, and will return Object []. These objects can be returned to any class (not necessarily the same class with factory methods), and they do not even need TestNG annotations (in the example TestNG will be ignored)
The operating principle of using @Factory
1, if not used @Factory, running conventional methods are marked @Test actually constructed TestNG framework called an object class constructor, and then execute the method of the object.
2, the use of @Factory, the method can be seen marked @Factory returned an Object array, each array element of the object to be tested is a class. That is a plurality of test configuration @Factory class objects, each object is then passed to TestNG frame, then the frame is @Test TestNG methods noted in the implementation of these objects respectively.
Through the above description, we know @Factory can help us achieve a simple data-driven testing (based on test data, perform the corresponding procedure).
summary
Well, today will share knowledge about plants so much.