Testng

Table of Contents

1. Testng

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

参考:
TestNG documentation: http://testng.org/doc/documentation-main.html

1.1. 集成到 maven 工程中

在 maven 工程中使用 Testng,只需要把下面依赖加入到 pom.xml 文件中:

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8.8</version>
      <scope>test</scope>
    </dependency>

2. 第一个 Testng 例子

2.1. 准备测试程序

工程目录结构为:

./pom.xml
./src/test
./src/test/java
./src/test/java/com
./src/test/java/com/mycompany
./src/test/java/com/mycompany/app
./src/test/java/com/mycompany/app/App1Test.java
./src/test/java/com/mycompany/app/App2Test.java

其中,pom.xml 为:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8.8</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

两个测试程序 App1Test.java 和 App2Test.java 分别为:

package com.mycompany.app;

import org.testng.annotations.Test;

public class App1Test {
    @Test
    public void exampleOfTestNg() {
        System.out.println("This is TestNG Example");
    }
}

和:

package com.mycompany.app;

import org.testng.annotations.Test;

public class App2Test {
    @Test
    public void anotherExampleOfTestNg() {
        System.out.println("This is another TestNG Example");
    }
}

2.2. 执行测试程序

运行 mvn test 即可执行测试程序。如:

$ mvn test
......
[INFO] Surefire report directory: /Users/cig01/test/my-app/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@368239c8
This is TestNG Example
This is another TestNG Example
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.473 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
......

2.3. 执行指定的测试程序

可以通过命令行参数来指定想在运行的测试程序,如:

$ mvn -Dtest=com.mycompany.app.App1Test test                              # 只执行第一个测试程序
$ mvn -Dtest=com.mycompany.app.App1Test,com.mycompany.app.App2Test test   # 执行两个测试程序

2.4. 通过 testng.xml 执行测试程序

新建文件 src/test/resources/testng.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestNgSuite" parallel="false">
  <test name="TestNgTest">
    <classes>
      <class name="com.mycompany.app.App1Test"/>
      <class name="com.mycompany.app.App2Test"/>
    </classes>
  </test>
</suite>

在 pom.xml 中增加如下配置:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>

运行 mvn test 后,会执行 testng.xml 中定义的测试程序。如:

$ mvn test
......
This is TestNG Example
This is another TestNG Example
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.036 s - in TestNgSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
......

打开文件 target/surefire-reports/index.html,可以看到测试报告。

3. Tips

3.1. 配置多个 testng.xml 文件

pom.xml 中可以指定多个 testng.xml 文件。如:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng1.xml</suiteXmlFile>
            <suiteXmlFile>src/test/resources/testng2.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>

默认这些 xml 文件中定义的 case 都会执行,也可以通过在命令行中定义 surefire.suiteXmlFiles 来只执行某个(或某几个)xml 中定义的 case。如:

$ mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng1.xml
$ mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng1.xml,src/test/resources/testng2.xml

Author: cig01

Created: <2017-03-12 Sun>

Last updated: <2017-12-13 Wed>

Creator: Emacs 27.1 (Org mode 9.4)