Using java+TestNG for Interface Regression Testing


TestNG is an open source automated testing framework. TestNG represents the Next Generation. TestNG is similar to Junit (especially JUnit 4), but it is not an extension of JUnit framework. Compared with JUnit, TestNG is more powerful and convenient to use. It is more suitable for testers to conduct integration tests or interface regression tests.

TestNG has the following characteristics:

Use java and object-oriented functions;
The name of a method need not be limited to a certain fixed format, and the method to be executed can be identified through comments.
Some parameters needed in the method can be passed through comments;
Comments are strongly typed, so errors can be reflected at compile time.
Support group testing, dependency testing, parallel testing, load testing, etc.
Supports multi-threaded testing.

TestNG Common Comment Type Comments



























TestNG parametric test

TestNG provides two ways to pass parameters.

The first is testng.xml

The advantage of this method is to separate the code from the test data and facilitate maintenance. The disadvantage is that if the parameter to be passed is not of the basic java type or the required value can only be created at runtime, this method is no longer applicable.


The second type: @DataProvider

This can provide more complex parameters (also known as data-driven testing).

The second type is used in our project.

As shown in the figure, firstly, add a dataProvider with the value of WithdrawBatchQueryLoopData to the test method, and then provide a @DataProvider method with the corresponding name. The concrete implementation in this method is to read data from the configuration file or database.

TestNG grouping test

Grouping test is an innovative function in TestNG. Grouping test enables us to conduct various flexible tests. When we want to parallel multiple groups of different tests, we do not need to recompile.

Grouping can be applied to methods (a method can belong to one or more groups) or to classes. When applied to classes, all public methods in this class become test methods. Even if they are not annotated, they can continue to repeat @Test annotation on methods that need to add attributes. There are many forms of grouping execution test methods:

You can specify the specific grouping to be executed by configuring in testng.xml.

It is also possible to set the grouping to be executed through integration into jenkins and parameterized construction.

You can also set the grouping to be executed by running Run Configurations

One thing to note: the Group tag will cause @BeforeMethod to fail.



Public void beforeMehod()


Public void testCase1()


Public void testCase2()

正常的执行的顺序为:beforeMehod—> testCase1—> beforeMehod—> testCase2



Public void beforeMehod()

@Test(groups = "group1")

Public void testCase1()

@Test(groups = "group1")

Public void testCase2()



1. add @BeforeMethod to group1 as well.

2. set the attribute alwaysRun=true for @BeforeMethod

TestNG dependency test

We often encounter dependencies among multiple interfaces to be tested, that is, the execution of one interface needs to depend on the return result of the previous interface. For example, when executing batch payment query, batch payment needs to be executed first, then TestNG’s dependency test is used. TestNG uses dependsOnMethods and alwaysRun to set the dependencies between test methods and dependsOnGroups to set the dependencies between groups.

Strong Dependency: Before the test method runs, all dependent methods must run and succeed. Even if there is only one failure, the test method will not be called (skip instead of fail)

Soft Dependency (alwaysRun=true): Test methods are always executed after dependent methods run, even if some dependent methods fail to run.

TestNG Expected Exception Test

During testing, sometimes we expect some code to throw exceptions.

TestNG judges the expected exceptions and Error Message through @Test(expectedExceptions)


TestNG test report

After executing the test case, a test report will be generated under the project’s test-output (default directory)

Open the index.html file to see the summary of test results, including: suite name, number of test cases succeeded, number of test cases failed, number of test cases ignored, execution time and testng.xml file.

If all the test cases are successful, the test results will be marked with a green bottom; if the test cases fail, the test results will be marked with a red bottom. TestNG’s own generated test report is not very beautiful and can be beautified with testng-xslt.

Source:Yixin Institute of TechnologyAuthor: Wang Haiyan