Chapter sixteen, 7-DataProviders

First, when we test the same number of sets of data need to be tested, so we need to use - "DataProviders

. 1  Package testclasses1;
 2  
3  Import org.testng.annotations.DataProvider;
 . 4  Import org.testng.annotations.Test;
 . 5  
. 6  public  class TestNG_DataProviders {
 . 7      
. 8  / ** 
. 9  * scenario: we need 3 sets of data for a test with methods for testing.
10  * Solutions:
 11  * 1, if we write three repeat test. Then it will cause the code redundancy, in order to improve the reusability of code, we can use testng in @DataProvider data provide
 12  * 2, first of all we need to write a method of data can be improved, a two-dimensional array of type when it , you need to return parameters.
13 is  *. 3, the DataProvider @ (name = "Inputs") to make a note of the method, named "Inputs"
 14  *. 4, reference data in the test method in testMethod1, the dataProvider method for providing data and name must be the name of the same notes
 15  * */
16     
17     @DataProvider(name="inputs")
18     public Object[][] getData(){
19         return new Object[][] {
20             {"苹果","红色"},
21             {"西瓜","绿色"},
22             {"桔子","黄色"},
23         };
24     }
25     
26   @Test(dataProvider="inputs")
27   public void testMethod1(String input1,String input2) {
28       System.out.println("Input 1:"+input1);
29       System.out.println("Input 2:"+input2);
30   }
31 }

operation result:

 

 

Second, if there are multiple testing methods and providing data dataprovider one of our class, it will be a bit confusing, so we need then will provide the data and test methods class separation.

1, a new test for storing data such

. 1  Package testclasses1;
 2  
. 3  Import org.testng.annotations.DataProvider;
 . 4  
. 5  public  class the TestData {
 . 6  
. 7      the @dataProvider (name = "Inputs" )
 . 8      public Object [] [] the getData () {
 . 9          return  new new Object [] [ ] {
 10              { "apple", "red" },
 11              { "watermelon", "green" },
 12              { "orange", "yellow" },
 13          };
 14      }
 15 }

2, test class

Package testclasses1; 

Import org.testng.annotations.Test; 

public  class TestNG_DataProviders { 

//     dataProviderClass = TestData.class: storing a reference test data such specialized format: dataProviderClass = test data class name .class 
  @Test (= the dataProvider " Inputs. ", the TestData dataProviderClass = class )
   public  void testMethod1 (String INPUT1, INPUT2 String) { 
      System.out.println ( " the Input. 1: "+ INPUT1); 
      System.out.println ( " the Input 2: "+ INPUT2); 
  } 
}

3, operating results:

 

 

 

 

If you do not understand the small partners can be added to group "555 191 854" I asked, the group is small software industry partners together to learn from each other.

Content with coherence, unlabeled place to see the previous blog, which is a set of automated content on ava selenium +, starting with java foundation.

Welcome attention, please indicate the source.

Guess you like

Origin www.cnblogs.com/luohuasheng/p/11492657.html