最近因为工作需要,要了解TestNG并应用到项目的单元测试和集成测试中。准备写点文章,记录一下对TestNG的使用和了解。这篇文章是这个系列的第一篇,简要介绍TestNG是什么,以及如何使用TestNG编写一个Hello World程序。
TestNG是一个开源的测试框架,灵感来自于JUnit。TestNG跟JUnit4很像,但它并不是JUnit的扩展,它的创建目的是超越Junit。TestNG具有更强大的功能,引入了组测试的概念。TestNG不单纯用来做单元测试,它的作用在于为程序做集成测试。
本文假定你对java和maven有一定的了解。在开始实践之前,需要你准备好以下环境:
下面将完整地介绍,如何利用eclipse和maven来配置TestNG进行一个简单的Hello World测试。
eclipse工具栏目 : Help -> Install New Software 输入http://beust.com/eclipse在线安装,完成后重启eclipse即可。
使用eclipse新建一个quick-start
类型的maven项目即可。
pom.xml
加入testng依赖:
<dependencies>
<dependency>
<groupId>org.testnggroupId>
<artifactId>testngartifactId>
<version>6.8version>
<scope>testscope>
dependency>
dependencies>
在src/test/java
里面编写测试类:
package com.crazypig.testngdemo;
import static org.testng.Assert.*;
import org.testng.annotations.Test;
public class TestNGTest {
@Test
public void test() {
String actualString = "hello world -TestNG";
String expectedString = "hello world -TestNG";
assertEquals(actualString, expectedString);
assertNotEquals(actualString.toUpperCase(), expectedString);
}
}
在src/test/resources
里面新建testng.xml
,内容如下:
<suite name="Suite1" verbose="1" >
<test name="test1" >
<classes>
<class name="com.crazypig.testngdemo.TestNGTest" />
classes>
test>
suite>
选中testng.xml
文件,鼠标右键选择Run As -> TestNG Suite, 跑完可以在”Result of runing suite”窗口以及常规控制台窗口看到测试报告:
console窗口测试报告如下所示:
[TestNG] Running:
E:\sts-workspace\mycat-workspace\testngdemo\src\test\resources\testng.xml
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
在testng官网上面提到,maven2天生就是支持testng的:
Maven 2 supports TestNG out of the box without the need to download any additional plugins (other than TestNG itself). It is recommended that you use version 2.4 or above of the Surefire plugin (this is the case in all recent versions of Maven).
虽然maven2天生支持testng测试, 但是testng官网还是建议我们使用maven-surefire-plugin
来进行testng测试,并且插件的版本要求为2.4或更高。
因此,我们在pom.xml
里面加入maven-surefire-plugin
的配置,并配置testng测试所用的xml文件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.17version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>./src/test/resources/testng.xmlsuiteXmlFile>
suiteXmlFiles>
configuration>
plugin>
plugins>
build>
配置完成以后,在项目根目录运行:
mvn test
就可以用maven来启动testng测试,而且会生成可读性较高的testng报告(html格式)
mvn test
测试成功的结果如下所示:
E:\sts-workspace\mycat-workspace\testngdemo>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testngdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testngdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testngdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testngdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testngdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ testngdemo ---
[INFO] Surefire report directory: E:\sts-workspace\mycat-workspace\testngdemo\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.755 sec - in TestSuite
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.100 s
[INFO] Finished at: 2016-09-22T10:28:03+08:00
[INFO] Final Memory: 9M/150M
[INFO] ------------------------------------------------------------------------
成功完成测试后可以在${project_dir}/target/surefire-reports/目录下得到测试报告,双击index.html将会得到一份完成的测试报告: