TestNG-初识 Hello World

最近因为工作需要,要了解TestNG并应用到项目的单元测试和集成测试中。准备写点文章,记录一下对TestNG的使用和了解。这篇文章是这个系列的第一篇,简要介绍TestNG是什么,以及如何使用TestNG编写一个Hello World程序。

1. TestNG介绍

TestNG是一个开源的测试框架,灵感来自于JUnit。TestNG跟JUnit4很像,但它并不是JUnit的扩展,它的创建目的是超越Junit。TestNG具有更强大的功能,引入了组测试的概念。TestNG不单纯用来做单元测试,它的作用在于为程序做集成测试。

2. 动手前准备

本文假定你对java和maven有一定的了解。在开始实践之前,需要你准备好以下环境:

  • JDK环境
  • IDE(Eclipse or other)
  • 安装配置好maven

3. 实践

下面将完整地介绍,如何利用eclipse和maven来配置TestNG进行一个简单的Hello World测试。

3.1 Eclipse TestNG插件安装

eclipse工具栏目 : Help -> Install New Software 输入http://beust.com/eclipse在线安装,完成后重启eclipse即可。

3.2 创建maven项目

使用eclipse新建一个quick-start类型的maven项目即可。

pom.xml加入testng依赖:

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

3.3 编写testng测试类

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);

    }

}

3.4 编写testng.xml

src/test/resources里面新建testng.xml,内容如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
  <test name="test1" >
    <classes>
       <class name="com.crazypig.testngdemo.TestNGTest" />
    </classes>
  </test>
</suite>

3.5 Run TestNG

选中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
===============================================

3.6 maven测试插件跑testng

在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.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

就可以用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将会得到一份完成的测试报告:

show_testng_report

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值