Java automation testing framework -03 - TestNG Test Group of articles - our group together Daguai upgrade (detailed tutorial)

Brief introduction

  In fact, group macro brother in this article on the previous mentioned, but it is an example of a band before, so today dedicated an article to explain the Group's knowledge. I hope you enlightened, with further knowledge and understanding of the test group.

A, the Test Group (test group)

  TestNG allows you to sophisticated testing methods are classified into different groups. Not only can declare that a method belongs to a group, but also allows group contain other groups. Such TestNG can call request contains a specific set or group (or regular expressions) to the exclusion of other unwanted groups. Thus, if you intend to test is divided into two, when there is no need to recompile. This feature will give you great flexibility in dividing the group time.

  Group (Group) testng.xml specified in the file, the tag can be found in <test> or <suite>. <Suite> tag is specified in the group applies to all of the following <test> tag. Note that, in the group of these markers are cumulative: if the specified set of "a" in <suite>, specify "b" in <test>, the then comprising "a" and "b".

For example, the test is generally divided into two categories is another common than this:

  Check the test (Check-in test): These tests will be run before you submit the new code. They are generally very soon, and to ensure that no basic function is to make the poor.

  Functional testing (Functional test): These tests cover all your software functions, and run at least once a day, but you might want them to continue operating.

  Typically, the detection test is usually a subset of the functionality test. TestNG allows you to divide groups based on personal feelings. For example, you might want to put all your test classes are classified as "functest" group, and there are a few additional input method "checkintest" group.

TestNG allows you in a very intuitive way to specify the use of a test group. For example, you can explain your entire test category is "functest" group to build a test, and there are some methods belong to the "checkintest" group:

Package Penalty for hongge; 

Import org.testng.annotations.Test;
 Import  static org.testng.Assert.assertEquals;
 Import hongge.TestSum; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automated testing exchange group: 694 280 102 
 * 
 * the Java Automation testing framework -03 - TestNG articles of test Group 
 * 
 * October 22, 2019 
 * / 
public  class Test1 { 
    @Test (Groups = { "functest", "checkintest" })  
       public  void testMethod1 () {   
      }   
      @Test (Groups {= "functest", "checkintest" })  
       public void testMethod2() {  
      }  
      @Test(groups = { "functest" })  
      public void testMethod3() {  
      }  
}

TestNG by calling the following content

<test name="Test1">  
  <groups>  
    <run>  
      <include name="functest"/>  
    </run>  
  </groups>  
  <classes>  
    <class name="example1.Test1"/>  
  </classes>  
</test>  

Or more above that class will run all the tests, when you want to use checkintest call, you just run testMethod1 () and testMethod2 ().

Here is another example. The use of regular expressions. Some test method should not be assumed to run Linux environment, your test will look like:

Package Penalty for hongge; 

Import org.testng.annotations.Test;
 Import  static org.testng.Assert.assertEquals;
 Import hongge.TestSum; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automated testing exchange group: 694 280 102 
 * 
 * the Java Automation testing framework -03 - TestNG articles of test Group 
 * 
 * October 22, 2019 
 * / 
@Test 
public  class Test1 { 
     @Test (Groups = { "windows.checkintest" })   
      public  void testWindowsOnly () {   
     }   
     @Test (Groups {= "linux.checkintest" })  
     public void testLinuxOnly() {  
     }  
     @Test(groups = {"windows.functest"})  
     public void testWindowsToo() {  
     }  
}

Then you can use the following testng.xml to run only under Windows:

<test name="Test1">  
  <groups>  
    <run>  
      <include name="windows.*"/>  
    </run>  
  </groups>  
  <classes>  
    <class name="example1.Test1"/>  
  </classes>  
</test>  

Note: TestNG using regular expressions instead of wildcard characters. Note the difference between these two

For example, "anything" to match "*." - point and asterisk - instead of an asterisk "*."

Two, MetaGroups ( group set )

  Test set can also contain other groups. This group is called "tuple" (MetaGroups). For example, you might want to define a set of all to include other groups, chekcintest and functest. "Functest" itself only contains a set of windows and linux, but "checkintest" contains only windows. You can define the properties file:

<test name="Regression1">  
  <groups>  
    <define name="functest">  
      <include name="windows"/>  
      <include name="linux"/>  
    </define>  
    <define name="all">  
      <include name="functest"/>  
      <include name="checkintest"/>  
    </define>  
    <run>  
      <include name="all"/>  
    </run>  
  </groups>  
  <classes>  
    <class name="test.sample.Test1"/>  
  </classes>  
</test>  

Third, the exclusion of

   TestNG allows you to include groups, of course, can be ruled out.

Instance, because of the recent changes, leading to the current test is interrupted and that you have not time to fix these problems are commonplace. However, you also need to own functional tests can be run properly, therefore, pharmaceutical simply do not need to make these tests fail it. But do not forget that in the future when needed, to re-let it take effect.

   A simple way to solve this problem is to create one called "broken" group, and then make these test methods belong to that group. Such as the example above, suppose I know testMethod2 () is interrupted, so I hope it can be disabled:

Package Penalty for hongge; 

Import org.testng.annotations.Test;
 Import  static org.testng.Assert.assertEquals;
 Import hongge.TestSum; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automated testing exchange group: 694 280 102 
 * 
 * the Java Automation testing framework -03 - TestNG articles of test Group 
 * 
 * October 22, 2019 
 * / 
@Test (Groups = { "checkintest", "Broken" })  
 public  void testMethod2 () {}  

And all I need to do is to exclude the group from running:

<test name="Simple example">  
  <groups>  
    <run>  
      <include name="checkintest"/>  
      <exclude name="broken"/>  
    </run>  
  </groups>  
  <classes>  
    <class name="example1.Test1"/>  
  </classes>  
</test>  

In this way, we can both get clean test run, but also be able to track that needs to be corrected later interrupted test.

Note: You can be accomplished by using the "enabled" attribute, which applies to @Test and @ Before / After annotation.

Fourth, the local group

Group may be defined at the class level, but also after the stage in the process can define groups:

Package Penalty for hongge; 

Import org.testng.annotations.Test;
 Import  static org.testng.Assert.assertEquals;
 Import hongge.TestSum; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automated testing exchange group: 694 280 102 
 * 
 * the Java Automation testing framework -03 - TestNG articles of test Group 
 * 
 * October 22, 2019 
 * / 
@Test (Groups = { "CHECKIN-test" })  
 public  class All {   
@Test (Groups = { "FUNC-test" )  
 public  void the method1 () {...}  
 public  void method2 () {...}   
}  

In this class, method2 () class-level part of a group "checkin-test", and the method1 () that is part of "checkin-test" belong "func-test" group.

Fifth, the method group

You can exclude or include a single method

<test name="Test1">

  <classes>

    <class name="example1.Test1">

      <methods>

        <include name=".*enabledTestMethod.*"/>

        <exclude name=".*brokenTestMethod.*"/>

      </methods>

     </class>

  </classes>

</test>

This can come in to disable individual method without having to recompile anything, but I do not recommend using this technique too much, because if you start refactoring Java code (using regular expressions in regular expressions), it make your testing framework crash. Label may no longer be consistent with your approach).

summary

  Hey! Share here today. The next plan is to test method (Test Method), want little friends and children's shoes are like and continue to focus on the macro brother!

There are problems to join java automated testing exchange group: 694 280 102

 

No individual public                                                              micro-channel group  (group of micro-channel 100 is full, you can add micro Nobuhiro brother pull you into the group, please note: into the group)          

                                                                 

I am sure that your progress is. If you feel good, you are encouraged to ask about it! Remember dimples  recommended  Oh! ! ! (Click on the right side of the ball you can! (^ __ ^) hee hee ......)

Guess you like

Origin www.cnblogs.com/du-hong/p/11696589.html