Java automation testing framework -11 - TestNG annotation of concurrent test article (detailed tutorial)

1 Introduction

TestNG used in the annotation of a quick preview of their properties.

2, TestNG basic notes (Notes)

annotation description
@BeforeSuite Annotated method only run once, run all tests before the current suite execution
@AfterSuite Annotated method to run only once, after the current suite execute all test execution
@BeforeClass Annotated method to run only once, before the implementation of all method calls in the current class
@AfterClass After the implementation of the annotated method only run once, all method calls in the current class
@BeforeTest Only run once, run run until all test methods
@AfterTest Only run once, run after run all test methods
@BeforeGroups The list of groups, this configuration will run before. This is to ensure that the operation of any of these groups belong to the first test method is called.
@AfterGroups List Group, this configuration after running. This method is guaranteed to run shortly after the last test method, which belong to any of these groups is called.
@BeforeMethod Prior to running each test method (@Test) run, Example: To reset the execution data to execute use cases, the available ways After the second test.
@AfterMethod Run after each test method (@test) run
@DataProvider
Marks a method provides a test method data. The method must return an annotated Object [] [], the parameter list for each object where [] of the test method can be allocated.
The @Test, and I hope to receive data from a DataProvider this, you need to use a dataProvider name equal to the name of the annotation.
@Factory As a factory object returns TestNG test class is a method for marking. The method must return Object [].
@Listeners Define a test class listeners
@Parameters How to pass parameters to @Test method.
@Test Marking a class or method as part of the test.

 

3, using comments / annotations benefits

Here are some of the benefits of using comments / annotations:

  • TestNG methods to identify it by looking for interesting comments / annotations. Thus, the method is not limited to any pattern name or format.

  • Other parameters can be passed to comment.

  • Comments are strongly typed, the compiler would immediately flag any errors.

  • Extended test class is no longer needed anything (such as TestCase, for JUnit3).

4, Concurrenttesting ( annotation mode ):

Annotations may be achieved by arranging the @Test threadPoolSize concurrency, and threadPoolSize invocationCount are used in combination , the time when invocationCount = 1, threadPoolSize meaningless. It indicates the number of invocationCount method is invoked, if not disposed threadPoolSize, the method will be executed sequentially five times, if the configuration threaPoolSize = 4, the method will be shown below once to four concurrently executing threads, execution time.

The following example is the output process ID, threadPoolSize used to specify the size of the thread pool, which is the number of threads is the number of concurrent

5 call, there are three threads can call

1, the new com.course.testng.multiThread briefcase, MuitiThreadOnAnnotion new class, as follows

2, reference code

Package Penalty for com.course.testng.multiThread; 

Import org.testng.annotations.Test; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automation testing framework -11 - TestNG of annotation and concurrent test papers 
 * 
 * November 2019 11 
 * / 
// multithreaded test, not associated with embodiments to use multiple threads to reduce the execution time of 
public  class MuitiThreadOnAnnotion { 
    @Test (invocationCount =. 5, threadPoolSize =. 3 )
     public  void test () { 
        System.out.println ( . 1 ) ; 
        System.out.printf ( "Thrad Id:%% n-S" ., Thread.currentThread () getId ()); 
    } 
}

3, console page output

4, if the call changed five times, has five threads can call

5, testng.xml profile

TestNG can run all the test with multi-threaded mode, so you can get the maximum running speed, the maximum saving execution time. Of course, there is a price run concurrently, that is, we need to code is thread-safe.

Running concurrently test, then we need to run the specified configuration file, an example is as follows:

<suite name="My suite" parallel="methods" thread-count="4">

Note: During the execution of the current test planning process, uses a separate thread to perform each test method, up to four concurrent threads.

<suite name="My suite" parallel="tests" thread-count="4">

Note: during the current test plan, a separate thread for each execution of test cases (test method the test a shared thread), up to four concurrent threads.

<suite name="My suite" parallel="classes" thread-count="4">

Note: during the current test plan using (Test Method This test class share a thread) as a separate thread of execution for each test class, a maximum of four concurrent threads.

<suite name="My suite" parallel="instances" thread-count="4">

Note: During the execution of the current test plan, the test for each instance of the class to perform always use the unique thread (test method test instance to share a thread), up to four concurrent threads.

Note: parallel here defaults to "none". Once the "true", "false" outdated, not recommended.

1.Parallel = "methods" is meant Motoko unit will TestNG method as concurrency, that is, each method runs in its own thread. If parallel = "tests", it means the unit will test Motoko as concurrency

2.Thread-count = "2" refers to, when running, concurrency 2, there will be two threads running simultaneously.

1, a new test class, for example: Test1

 2, reference code

Package Penalty for com.course.testng.multiThread; 

Import org.testng.annotations.Test; 

/ ** 
 * @author Beijing - Hong Columbia 
 * 
 * the Java automation testing framework -11 - TestNG of annotation and concurrent test papers 
 * 
 * November 2019 12 
 * / 
public  class Test1 { 
    @Test (Groups = { "T8" })  
     public  void aThreadPool () {   
        System.out.println ( "#ThreadA:" + . Thread.currentThread () getId ());   
    }   
    @ the Test (Groups = { "T8" })  
     public  void bThreadPool () {   
        System.out.println ("#ThreadB: " +Thread.currentThread().getId());  
    }  
    @Test(groups = { "t8"})  
    public void cThreadPool() {  
        System.out.println("#ThreadC: " +Thread.currentThread().getId());  
    }  
}

3, testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" parallel="methods" thread-count="2">
    <classes>
      <class name="com.course.testng.multiThread.Test1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

4, the console output:

5, modify testng.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" parallel="tests" thread-count="2">
    <classes>
      <class name="com.course.testng.multiThread.Test1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

6, the console page output (because aThreadPool (), bThreadPool (), cThreadPool () in a test below)

6. Summary

     Well, today on the annotation with TestNG concurrent test articles, sharing it here.

Guess you like

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