TestNG简介

来源:互联网 发布:java怎么选择权限框架 编辑:程序博客网 时间:2024/06/05 03:41
前言
    前几天在跑一个项目的时候发现它的测试框架是TestNG,以前没接触过,不过看起来跟JUnit差不多,后来通过查阅资料发现有个说法:开发人员用Junit写单元测试,测试人员用TestNG写自动化测试。由此可见TestNG比JUnit要更加强大,今天我们就看看测试人员使用的TestNG究竟是啥,比JUnit强在哪里。

正题
    
1.TestNG介绍

    TestNG(Test Next Generation),是基于JUnit和NUnit并进行了改进的测试框架,它的功能很强大,而且简单易用。
    
    下面是官网的解释:
   TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

1)Annotations. 

2)Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per 

3)test class, etc...).

4)Test that your code is multithread safe.

5)Flexible test configuration.

6)Support for data-driven testing (with @DataProvider).

7)Support for parameters.

8)Powerful execution model (no more TestSuite).

9)Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).

10)Embeds BeanShell for further flexibility.

11)Default JDK functions for runtime and logging (no dependencies).

12)Dependent methods for application server testing.

TestNG is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc...

    ps:详情可参考TestNG官网 http://testng.org/doc/index.html
    

2.特点
    上面已经介绍了TestNG的特性,我们可以主要归为几个:
1)更丰富的annotation。

2)使用xml配置文件可以任意组合出需要的各种测试。

3)命令行参数让每一次测试更灵活。


3.Eclipse中安装TestNG
    要想使用TestNG不仅需要相应的jar包还需要对应的插件,安装方式有很多种,对于第一次接触的东西我们可以直接通过Help->Eclipse Marketplace 然后输入tTestNG进行搜索即可。


    安装成功后我们就可以看到了
    

4.简单示例
package com.dan.test;import org.junit.AfterClass;import org.junit.BeforeClass;import org.testng.annotations.Test;public class TestNG1 {    @BeforeClass    public void beforeClass() {        System.out.println("this is before class");    }    @Test    public void TestNgExample() {        System.out.println("this is TestNG test case");    }    @AfterClass    public void afterClass() {        System.out.println("this is after class");    }}


    
1 0
原创粉丝点击