51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1494|回复: 0

关于TestNG

[复制链接]

该用户从未签到

发表于 2018-3-20 13:27:06 | 显示全部楼层 |阅读模式
TestNG是一个不错的测试框架,尤其是用于模块测试,以及大范围的测试。相对于JUnit来说,更为灵活。
随着JUnit4的推出,很多功能都与TestNG相似,但相对于JUnit4,TestNG还是有很多部分是有区别的。
      TestNG的IDE支持也不错,对于Eclipse,Idea,Ant都有很好的支持。
      先来看一看怎么使用TestNG,当然首先需要下载TestNG包。目前的版本为5.1,下载地址如下:
      http://testng.org/doc/download.html ,也可以下载相应的Eclipse插件。
      运行TestNG,可以从命令行或者IDE,或者Ant中运行。
      命令行:
      java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
      对于大型的测试,需要定义一个xml文件,一般为testng.xml。
  1.    


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

  3. <suitename="Suite1"    verbose="1" >
  4. <testname="Nopackage" >
  5. <classes>
  6.        <classname="NoPackageTest"  />
  7. </classes>
  8. </test>

  9. <testname="Regression1"   >
  10. <classes>
  11. <classname="test.sample.ParameterSample"  />
  12. <classname="test.sample.ParameterTest" />
  13. </classes>
  14. </test>
  15. </suite>

  16.      java org.testng.TestNG testng.xml
  17.      当然如果使用Eclipse插件,就简单多了。

  18.       下面来看一下,如何来实现测试的,与JUnit4 差不多(怀疑,JUnit4是不是有抄袭TestNG的成分)。
  19.       声明测试方法如下:



  20.     public void testMethod1() {
  21.         System.out.println("in testMethod1");
  22.     }


  23.     public void testMethod2() {
  24.         System.out.println("in testMethod2");
  25.     }

  26.      基本上都是采用java5的注释实现的。
  27.      与JUnit4 不同在于,测试方法可以分组,它可以根据诸如运行时间这样的特征来对测试分类。


  28.     public void testMethod1() {
  29.         System.out.println("in testMethod1");
  30.     }


  31.     public void testMethod2() {
  32.         System.out.println("in testMethod2");
  33.     }

  34.       同JUnit4 一样,同样支持Before,After方法,如同setUp 和tearDown,不过TestNG更为灵活,支持各种签
  35. 名方式,如private,protected。
  36.     @BeforeMethod
  37.     protected void beforeMethod() {
  38.         System.out.println("in beforeMethod");
  39.     }

  40.     @AfterMethod
  41.     protected void afterMethod() {
  42.         System.out.println("in afterMethod");
  43.     }

  44.      同样也支持BeforeClass 和AfterClass,只执行一次的方法,但是可以不需要使用static签名
  45.     @BeforeClass
  46.     protected void beforeClassMethod() {
  47.         System.out.println("in beforeClassMethod");
  48.     }

  49.     @AfterClass
  50.     protected void afterClassMethod() {
  51.         System.out.println("in afterClassMethod");
  52.     }
复制代码

     不同于JUnit4,TestNG提供了以下的特性:
     依赖性测试
     JUnit 框架想达到的一个目标就是测试隔离。它的缺点是:人们很难确定测试用例执行的顺序,而这对于
任何类型的依赖性测试都非常重要。开发者们使用了多种技术来解决这个问题,例如,按字母顺序指定测试
用例,或是更多地依靠 fixture 来适当地解决问题。
      与 JUnit 不同,TestNG 利用 Test 注释的 dependsOnMethods 属性来应对测试的依赖性问题。有了这个
便利的特性,就可以轻松指定依赖方法。如以下定义:testMethod2依赖于testMethod1。


  1.     @Test
  2.     public void testMethod1() {
  3.         System.out.println("in testMethod1");
  4.     }

  5.     @Test(dependsOnMethods="testMethod1")
  6.     public void testMethod2() {
  7.         System.out.println("in testMethod2");
  8.     }
  9. 当然如果testMethod1失败的话,默认testMethod2也不会执行,不过只需要设置alwaysRun = true,则可以
  10. 跳过testMethod1

  11.     @Test
  12.     public void testMethod1() {
  13.         System.out.println("in testMethod1");
  14.         throw new RuntimeException("failed");
  15.     }

  16.     @Test(dependsOnMethods="testMethod1",alwaysRun = true)
  17.     public void testMethod2() {
  18.         System.out.println("in testMethod2");
  19.     }
复制代码


     失败和重运行
     在大型测试套件中,这种重新运行失败测试的能力显得尤为方便。这是 TestNG 独有的一个特性。在
JUnit 4 中,如果测试套件包括 1000 项测试,其中 3 项失败,很可能就会迫使您重新运行整个测试套件
(修改错误以后)。不用说,这样的工作可能会耗费几个小时。
一旦 TestNG 中出现失败,它就会创建一个 XML 配置文件,对失败的测试加以说明。如果利用这个文件执
行 TestNG 运行程序,TestNG 就只 运行失败的测试。所以,在前面的例子里,您只需重新运行那三个失败
的测试,而不是整个测试套件。可以看到以下的失败文件,一般命名为testng-failed.xml,以后只需要运行
此文件就可以了。

  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
  2. <suite thread-count="5" verbose="1" name="Failed suite [testng]" parallel="false" annotations="JDK5">
  3.   <test name="demo.testng.Test2(failed)" junit="false" parallel="false" annotations="JDK5">
  4.     <classes>
  5.       <class name="demo.testng.Test2">
  6.         <methods>
  7.           <include name="testMethod1"/>
  8.           <include name="testMethod2"/>
  9.           <include name="beforeClassMethod"/>
  10.           <include name="afterClassMethod"/>
  11.           <include name="beforeMethod"/>
  12.           <include name="afterMethod"/>
  13.         </methods>
  14.       </class>
  15.     </classes>
  16.   </test>
  17. </suite>
复制代码


    参数化测试
    TestNG 中另一个有趣的特性是参数化测试。在 JUnit 中,如果您想改变某个受测方法的参数组,就只能
给每个 不同的参数组编写一个测试用例。多数情况下,这不会带来太多麻烦。然而,我们有时会碰到一些
情况,对其中的业务逻辑,需要运行的测试数目变化范围很大。
    在这样的情况下,使用 JUnit 的测试人员往往会转而使用 FIT 这样的框架,因为这样就可以用表格数据
驱动测试。但是 TestNG 提供了开箱即用的类似特性。通过在 TestNG 的 XML 配置文件中放入参数化数据,
就可以对不同的数据集重用同一个测试用例,甚至有可能会得到不同的结果。这种技术完美地避免了只能
假定一切正常的测试,或是没有对边界进行有效验证的情况。


  1.    @Parameters( { "first-name"
  2.     })
  3.     @Test(groups = { "param"
  4.     })
  5.     public void testParm(String firstName) {
  6.         System.out.println("invoked testString:" + firstName);
  7.         assertEquals(firstName, "Test");
  8.     }
复制代码


在xml中设置相应的参数值,可以放入suite下面或者test下面,如果同名,一般test下面的定义覆盖suite定义。

<parameter name="first-name" value="Test"/>

    高级参数化测试
    尽管从一个 XML 文件中抽取数据会很方便,但偶尔会有些测试需要有复杂类型,这些类型无法用 String
或原语值来表示。TestNG 可以通过它的 @DataProvider 注释处理这样的情况。@DataProvider 注释可以方
便地把复杂参数类型映射到某个测试方法。例如,清单 7 中的 verifyHierarchy 测试中,我采用了重载的
buildHierarchy 方法,它可接收一个 Class 类型的数据, 它断言(asserting)Hierarchy 的 getHierarchyClass
Names() 方法应该返回一个适当的字符串数组:
  1. package test.com.acme.da.ng;

  2. import java.util.Vector;

  3. import static org.testng.Assert.assertEquals;
  4. import org.testng.annotations.DataProvider;
  5. import org.testng.annotations.Test;

  6. import com.acme.da.hierarchy.Hierarchy;
  7. import com.acme.da.hierarchy.HierarchyBuilder;

  8. public class HierarchyTest {


  9. public Object[][] dataValues(){
  10.   return new Object[][]{
  11.    {Vector.class, new String[] {"java.util.AbstractList",
  12.      "java.util.AbstractCollection"}},
  13.    {String.class, new String[] {}}
  14.   };
  15. }


  16. public void verifyHierarchy(Class clzz, String[] names)
  17.   throws Exception{
  18.     Hierarchy hier = HierarchyBuilder.buildHierarchy(clzz);
  19.     assertEquals(hier.getHierarchyClassNames(), names,
  20.           "values were not equal");               
  21. }
  22. }
复制代码


     当然还有一些其他的特性,就不一一详细说明了,有兴趣可以参考相应的testNG文档。
      JUnit 4 和 TestNG 在表面上是相似的。然而,设计 JUnit 的目的是为了分析代码单元,而 TestNG 的预期
用途则针对高级测试。对于大型测试套件,我们不希望在某一项测试失败时就得重新运行数千项测试,Test
NG 的灵活性在这里尤为有用。这两个框架都有自己的优势,您可以随意同时使用它们。

回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 16:21 , Processed in 0.069191 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表