Using java+TestNG for Interface Regression Testing

  test

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

描述
@Test

将类或是方法标记为测试的一部分

@BeforeSuite

在该套件的所有测试都运行在注释的方法之前,仅运行一次。

@AfterSuite

在该套件的所有测试都运行在注释的方法之后,仅运行一次

@BeforeClass

在调用当前类的所有测试方法之前执行,注释方法仅运行一次

@AfterClass

在调用当前类的所有测试方法之后执行,注释方法仅运行一次

@BeforeTest

注释的方法将在属于<test>标签内的类的所有测试方法运行之前运行

@AfterTest

注释的方法将在属于<test>标签内的类的所有测试方法运行之后运行。

@BeforeGroups

此方法是保证在运行属于任何这些组的第一个测试方法之前,该方法被调用

@AfterGroups

此方法是保证运行属于任何这些组的所有测试方法执行之后,该方法被调用

@BeforeMethod

被注释的方法将在每个测试方法之前执行

@AfterMethod

被注释的方法将在每个测试方法之后执行

@DataProvider

被注释的方法的作用是提供测试数据,如果某个测试方法希望从这个DataProvider接收数据,就必须使用一个名字等于这个注解名字的DataProvider

@Parameters

介绍如何将参数传递给测试方法

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.

使用java+TestNG进行接口回归测试
使用java+TestNG进行接口回归测试

The second type: @DataProvider

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

The second type is used in our project.

使用java+TestNG进行接口回归测试
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.

使用java+TestNG进行接口回归测试
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.

使用java+TestNG进行接口回归测试
It is also possible to set the grouping to be executed through integration into jenkins and parameterized construction.

使用java+TestNG进行接口回归测试
You can also set the grouping to be executed by running Run Configurations

使用java+TestNG进行接口回归测试
One thing to note: the Group tag will cause @BeforeMethod to fail.

@BeforeMethod的作用是标明所注解的方法在每一个测试方法运行之前会执行一次。例如:

@BeforeMethod

Public void beforeMehod()

@Test

Public void testCase1()

@Test

Public void testCase2()

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

但是,在将testCase放入某一个Group之后,@BeforeMethod就失效了

@BeforeMethod

Public void beforeMehod()

@Test(groups = "group1")

Public void testCase1()

@Test(groups = "group1")

Public void testCase2()

再次执行脚本,执行顺序变为testCase1—>testCase2

Resolution:

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.

使用java+TestNG进行接口回归测试
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)

使用java+TestNG进行接口回归测试

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