laitimes

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

author:Atstudy Online School

1. Introduction to TestNG

In software testing work, automated testing frameworks are indispensable, with Junit and Nunit frameworks before and TestNG later. TestNG not only takes the ideas of the Junit and Nunit frameworks, but also creates more powerful features, which are not only unit testing frameworks, but also integrated automated testing frameworks. TestNG has broken through the limitations of some previous testing frameworks and has become one of the most widely used automation and unit testing frameworks.

1) Follow + reply by private message: "Test", you can get a free 10G software test engineer interview book document. And the corresponding video learning tutorials are free to share!

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

2、TestNG-初步认知

Software testing is the process of checking an application's functionality to ensure that it is performing as required. Unit testing is carried out by developers in coding where sufficient measures are taken to test each entity (class or method) to ensure that the final product meets the requirements. TestNG not only supports unit testing, but is also an excellent automated testing framework.

JUnit pushes developers to understand the usefulness of tests, especially unit tests, compared to any other testing framework. With a fairly simple, practical and rigorous architecture, JUnit is able to "infect" a large number of developers, and JUnit also has some drawbacks:

Originally designed to enable unit testing only and is now used for a variety of tests.

Dependency testing is not possible.

配置控制不佳(setUp/tearDown).

Intrusive (forces you to extend the class and name your method in a certain way).

Static programming model (forcing you to recompile unnecessarily).

Managing different test suites for complex projects can be tricky.

3. TestNG definition

TestNG is a testing framework inspired by JUnit and NUnit, but introduces some new features that make it more powerful and easier to use.

TestNG is an open-source automated testing framework, where NG stands for N ext G ventilation.TestNG is similar to JUnit (especially JUnit 4), but it is not a JUnit extension, it is inspired by JUnit.Its design is superior to JUnit, especially when it comes to automated testing.The creator of TestNG is Cedric Beust.

Removing most of the limitations of older frameworks, TestNG enables developers to write more flexible and robust tests. Since it borrows heavily from JavaAnnotations (introduced with JDK 5.0) to define tests, it can also show you how to use this new feature of the Java language in a real production environment.

4. TestNG function

Support annotation.

· TestNG uses more Java and OO features.

• Support for testing integration classes (e.g., by default, there is no need to create a new instance of a test class for each test method).

• Separate compile-time test code from runtime configuration/data information.

Flexible runtime configuration.

- Introduction: After the test group has compiled the tests, you can let TestNG run all the "front-end" tests, or "fast", "slow", "database" tests, etc.

Support related test methods, parallel testing, load testing and partial failures.

Flexible plugin API.

Support multi-threaded testing.

5. TestNG-Environment and Editing Tools

TestNG is a Java framework, so the first requirement is to install the JDK on your computer.

6. JDK system configuration requirements

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

7. Installation of JDK1.8 and configuration of environment variables

1) https://www.oracle.com/java/technologies/downloads/ 下载JDK安装包

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework
Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

2) Specific installation steps

Click Next:

The C drive has enough space to be installed on the C drive in a foolish way, or you can change the path to the D drive by yourself

Specific steps to change the path:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

3) Copy the name in the red box and create a new folder with its file name in the new Java folder you just created (note: the file name should not have any other characters)

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

4) Next step:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

Configuration of environment variables

Here take win11 system as an example:

1. First of all, win+E open the file manager, find this computer icon, right-click to select properties

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

2. Select Advanced System Settings

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

3. Select the environment variables

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

4. Configure environment variables (Note: Configure system variables instead of user variables are selected here)

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework
Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

5. Enter the variable name and variable value and click OK

Variable name: JAVA_HOME

Variable value: Here is the path where you just installed the JDK, if you follow the installation step by step, you can enter:

D:\Java\jdk1.8.0_131

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

6. Click on path and edit it

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

7. Click New

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

8. Input

%JAVA_HOME%\bin

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

9. Click Confirm

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

Make sure that the JDK is installed and configured

1.win+r input cmd to open the command window and enter javac, if the following prompt appears, it means that the installation and configuration are successful

javac

java-version

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework
Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

8、TestNG-基本注释

The traditional way to indicate a test method in JUnit 3 is to precede the name with test. This is a very efficient way to mark some methods in a class as having a special meaning, but the naming doesn't scale well (what if we want to add more markup for different frameworks?). And rather inflexible (what if we do?) Want to pass additional parameters to the test framework? ).

Annotations were officially added to the Java language in JDK 5, and TestNG has the option to use annotations to annotate test classes.

Here is a list of annotations supported by TestNG:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework
Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework
Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

9. Advantages of using annotations

Here are some of the benefits of using annotations and minus signs:

· TestNG recognizes the method by looking for comments that it is interested in. Therefore, method names are not limited to any schema or format.

We can pass additional parameters to the annotation.

Comments are strongly typed, so the compiler flags any errors immediately.

The test class no longer needs to be extended with anything (e.g. as a TestCase, for JUnit 3).

10. TestNG-Executor case demonstration

TestNgExecutionOrder.java案例代码如下:

package com.test;

import org.testng.annotations.*;

/**

*@author Fson

*@date 2024/1/26 14:06

*/

public class TestNgExecutionOrder{

//test case1

@Test

public void testCase1(){

System.out.println("in test case1");

}

//test case2

@Test

public void testCase2(){

System.out.println("in test case2");

}

//test beforeMethod

@BeforeMethod

public void testBeforeMethod(){

System.out.println("in beforeMethod每次执行Test方法前执行");

}

//test afterMethod

@AfterMethod

public void testAfterMethod(){

System.out.println("in afterMethod每次执行test方法后执行");

}

//test beforeClass

@BeforeClass

public void testBeforeClass(){

System.out.println("in beforeClass testcase1执行注释方法只会在调用当前类的第一个测试方法之前运行一次");

}

//test AfterClass

@AfterClass

public void testAferClass(){

System.out.println("in afterClass testcase1执行带注释的方法仅会运行一旦所有当前类的测试方法都已运行.");

}

//test BeforeTest

@BeforeTest

public void testBeforeTest(){

System.out.println("In BeforeTest, execute the suit, execute the BeforeTest, the annotated method will run the lesson LT before any test method that belongs to the method; Experiment > label run.");

}

//test AfterTest

@AfterTest

public void TestAfterTest(){

System.out.println("In afterTestThe annotated method will <test>run after all test methods that belong to the class within. tag has been run.");

}

//test beforeSuite

@BeforeSuite

public void testBeforeSuite(){

System.out.println("in beforeSuite最早执行在此套件中的所有测试运行之前,带注释的方法只运行一次.");

}

//test@AfterSuite

@AfterSuite

public void testAfterSuite(){

System.out.println("In afterSuite The annotated method is only run once after all test runs in this suite have been executed last.");

}

}

Here's what it looks like when you run it directly in the code:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

Based on the above output, the procedure is executed as follows:

First of all, the beforeSuite() method is executed only once.

Finally, the afterSuite() method is executed only once.

·甚至方法beforeTest(),beforeClass(),afterClass()和afterTest()方法也只执行一次.

The beforeMethod() method is executed for each test case, but before the test case is executed.

The afterMethod() method executes each test case after it has been executed.

Between beforeMethod() and afterMethod(), each test case is executed.

Run the program through the testng.xml configuration file

Let's create testng.xml file /work/testng/src where we execute the test case. This file captures your entire test in XML format. The file makes it easy for you to describe all your test suites and their parameters in one file that you can check into your code repository or email to a colleague. It also makes it easy to extract a subset of tests or split multiple runtime configurations (e.g., testngdatabase.xml will only run tests that test your database).

<?xml version="1.0"encoding="UTF-8"?>

<!--@beforeSuite-->

<suite name="tester">

<test name="case1">

<classes>

<class name="com.test.TestNgExecutionOrder"/>

</classes>

</test>

</suite>

右键--->run TestNgExecution.xml

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

Create a testng.xml file

There are two ways to run TestNG test scripts: either directly through the IDE (e.g. using "Run TestNG tests" in eclipse) and from the command line (by using an xml configuration file). When we want to execute a test script for a package or a part of a class, it is very convenient to use the XML configuration file. In the XML configuration file, you can not only select certain test scripts that need to be executed, but also exclude certain test scripts that do not need to be run.

Creating an XML file is easy, you just need to populate it with a few things. You can perform multiple intermediate test methods at the same time.

The first thing to do is to declare the name of a suite, which describes the set of test scripts that will be run, and you can name it as you wish, and finally this name will be seen in the testng test report.

<!DOCTYPE suite SYSTEM"http://testng.org/testng-1.0.dtd">

<suite name="SuiteName"verbose="1">

<test name="TestName">

</test>

</suite>

If you choose a test script that is group-based (using annotations like @Test(groups={"student"})), then you need to declare how you want to use those groups: include or exclude. If certain groups are labeled with the include tag, then only those test scripts that belong to those groups will be run in the selected test scripts. Test scripts that are not selected, or that are selected but do not belong to certain groups, will not be run. It is important to note that a test script can belong to many groups, and as long as one group is marked with the include tag, then it will be run.

If certain groups are labeled with the exclude tag, then only test scripts that do not belong to those groups will be run in the selected scripts. If both include and exclude tags are used, then scripts that have groups marked by include will be run, and scripts that have be marked by include will not be run. The exception to this is if a group is marked with both include and exclude, then the script that owns the group will be run. The include and exclude tags are used in the following ways:

<groups>

<run>

<include name="includedGroupName"/>

<exclude name="excludedGroupName"/>

</run>

</groups>

11、TestNG include

include specifies the method to be executed in the middle of the testng.xml, if only the specified method in the middle of the class is executed, you need to select the method to be executed through the testng.xml configuration file, and the method that is not included will not be executed.

IncludeTest.java案例代码

package com.test;

import org.testng.annotations.Test;

/**

*@author Fson

*@date 2024/2/20 14:20

*include在testng.xml中间指定需要执行的方法

*/

public class IncludeTest{

@Test

public void testRun(){

System.out.println("+++++++++++++++testRun+++++++++++");

}

@Test

public void testNotRun(){

System.out.println("+++++++++++testNotRun+++++++++++++++++++++++");

}

@Test

public void testTwoRun(){

System.out.println("++++++++testTwoRun++++++++++++++");

}

}

testng xml案例配置文件

<?xml version="1.0"encoding="UTF-8"?>

<!--@beforeSuite-->

<suite name="IncludeTestNG">

<test name="IncludeTest">

<classes>

<class name="com.test.IncludeTest"/>

<methods>

<include name="testRun"></include>

<include name="testTwoRun"></include>

</methods>

</classes>

</test>

</suite>

The results of the case are as follows:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

TestNG exclude

The opposite of include is exclude, for example, if everything else is run except the test01 method, use exclude, if you need to exclude a method from not executing, all other methods need to be executed, you need to use this keyword in the middle of the testng.xml configuration file.

ExcludeTest.java案例代码

package com.test;

import org.testng.annotations.Test;

/**

*@author Fson

*@date 2024/2/20 14:42

*exclude all methods except for this method, for example, there are 3 methods in the middle of the class: test01, test02, test03, except for test01, all other methods are executed

*You can't use this keyword

*/

public class ExcludeTest{

@Test

public void test01(){

System.out.println("test01()");

}

@Test

public void test02(){

System.out.println("test02()");

}

@Test

public void test03(){

System.out.println("test03()");

}

}

ExcludeTestNG.xml案例配置文件

<?xml version="1.0"encoding="UTF-8"?>

<!--@beforeSuite-->

<suite name="IncludeTestNG">

<test name="IncludeTest">

<classes>

<class name="com.test.ExcludeTest"/>

<methods>

<exclude name="test01"></exclude>

</methods>

</classes>

</test>

</suite>

Effect of case operation:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

12. Complete cases of TestNG automated test framework and output reports

Testng_before_after_class.java case code

package com.test;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

/**

*@author Fson

*@date 2024/1/16 14:06

*/

/*

Annotated description

TestNG supports a variety of annotations and can be combined in a variety of ways, as explained in the following sections

Note description

@BeforeSuite all tests for that suite are run only once before they run on the annotated method

@AfterSuite all tests for that suite are run only once after the annotated method

@BeforeClass run before the first test method of the current class is called, the annotation method is run only once

@AfterClass runs after the first test method of the current class is called, and the annotation method runs only once

The method @BeforeTest commented will run before all test methods that belong to the class inside the test tag run

The method @AfterTest commented will run after all test methods that belong to the class within the test tag have been run

@BeforeGroups configuration method will run the group list before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is called

@AfterGroups this configuration method will run the group list afterward. The method is guaranteed to run shortly after the last test method that belongs to any of these groups is called

@BeforeMethod annotation method will be run before each test method

@AfterMethod annotation method will run after each test method

@DataProvider label a method to provide data for the test method. The annotation method must return an Object[][], where each Object[] can be assigned to a list of parameters for the test method. The @Test method to receive data from that DataProvider needs to use a dataProvider name equal to the name of this annotation

@Factory mark a method as a factory, returning that TestNG will be used as an object for the test class. The method must return an Object[]

@Listeners define the listener on the test class

@Parameters describes how to pass parameters to @Test methods

@Test mark a class or method as part of a test, and if this flag is placed on a class, all public methods of that class will be used as test methods

Common annotations used:

1.@BeforeClass@AfterClass executes once before or after running the class

Initial data can be written to the @BeforeClass

@AfterClass clear the data

testng_before_after_class.java

public class Testng_before_after_class{

When you run a testng_before_after_class class, it is executed once

@BeforeClass

public void BeforeClass(){

System.out.println("BeforeClass被运行登录");

}

@AfterTest

public void AfterClass(){

System.out.println("AfterClass被运行退出登录");

}

Each test method that is run will be executed

@Test

public void abc(){

System.out.println("这里是abc");

}

@Test

public void abc1(){

System.out.println("这里是abc1");

}

@Test

public void abc2(){

System.out.println("这里是abc2");

}

@Test

public void exportBaogao(){

System.out.println("+++++++++++查看输出报告+++++");

}

}

testng_before.xml case profile

<suite name="tester_before_suite1"parallel="methods"thread-count="2">

<test name="test_before">

<classes>

<class name="com.test.Testng_before_after_class"/>

</classes>

</test>

</suite>

Effect of case operation:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

生成TestNG自带的默认报告Use Default Reportters

设置报告输出路径run-->Edit configuration

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

HTML file storage path:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

You can directly copy the file, open the browser and open the effect, and TestNG will automatically generate the test report as follows:

Essential skills for advanced testing! A detailed explanation of the TestNG automated testing framework

At last:

1) Follow + reply by private message: "Test", you can get a free 10G software test engineer interview book document. And the corresponding video learning tutorials are free to share!

Read on