Sky's blog

我和我追逐的梦

常用链接

统计

其他链接

友情链接

最新评论

TestNG官方文档中文版(5)-测试方法/类和组

5 - Test methods, Test classes and Test groups
5.1 - Test groups

TestNG容许执行复杂的测试方法分组。不仅可以申明方法属于组,而且可以指定分组包含其他分组。
然后TestNG可以被调用,并被要求包含某些分组和排除其他的分组。
这将提供怎样划分测试的最大弹性,并且如果想运行两个不同的测试装置不需要重新编译。

例如,非常普遍的需要至少两个种类的测试

    * Check-in tests.  这些测试将在提交新代码之前运行. 它们典型的被要求快速而且仅仅确认没有基础功能被破坏。
    * Functional tests.  这些测试将覆盖所有的软件功能,并且必须运行至少1天,尽管理想的是连续运行.

代表性的,check-in测试是功能性测试的子集。TestNG容许用非常直接的方式说明这个。
例如: 可以这样构造测试,申明完整的测试类属于"functest"组,另外两个方法属于组"checkintest":

public class Test1 {
  @Test(groups = { "functest", "checkintest" })
  public void testMethod1() {
  }

  @Test(groups = {"functest", "checkintest"} )
  public void testMethod2() {
  }

  @Test(groups = { "functest" })
  public void testMethod3() {
  }

}

调用TestNG,使用

<test name="Test1">
  <groups>
    <run>
      <include name="functest"/>
    </run>
  </groups>
  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>
将运行在类中的所有测试方法,如果使用checkintest调用则将只运行testMethod1()和testMethod2().

这里由其他例子,这次使用正则表达式。假设某些测试方法可能无法在Linux上运行,测试将是类似如此:
@Test
public class Test1 {
  @Test(groups = { "windows.checkintest" })
  public void testWindowsOnly() {
  }

  @Test(groups = {"linux.checkintest"} )
  public void testLinuxOnly() {
  }

  @Test(groups = { "windows.functest" )
  public void testWindowsToo() {
  }
}

你可以使用下面的testng.xml文件只启动Windows方法:

<test name="Test1">
  <groups>
    <run>
      <include name="windows.*"/>
    </run>
  </groups>

  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>

注意:TestNG使用正则表达,而不是wildmats。注意这个差别。

Method groups
同样可以包含或排除个别方法:

<test name="Test1">
  <classes>
    <class name="example1.Test1">
      <methods>
        <include name=".*enabledTestMethod.*"/>
        <exclude name=".*brokenTestMethod.*"/>
      </methods>
     </class>
  </classes>
</test>

这在需要使莫个单独的方法失效而不想重新编译时非常方便,但是不建议太多的使用这个机制,因为这将可能破坏你的测试框架 如果你开始重构你的java代码(标签中使用的正则表达式可能不再匹配你的方法)
5.2 - Groups of groups

"functest" itself will contain the groups "windows" and "linux" while "checkintest will only contain "windows".  Here is how you would define this in your property file:
组可以包含其他组。这些组被称为"MetaGroups"。例如,你可能想定义一个"all"组,包括"checkintest"和"functest"。"functest"自身将包含组 "windows" 和 "linux",而"checkintest"将包含"windows".

<test name="Regression1">
  <groups>
    <define name="functest">
      <include name="windows"/>
      <include name="linux"/>
    </define>
 
    <define name="all">
      <include name="functest"/>
      <include name="checkintest"/>
    </define>
 
    <run>
      <include name="all"/>
    </run>
  </groups>
 
  <classes>
    <class name="test.sample.Test1"/>
  </classes>
</test>

5.3 - Exclusion groups

TestNG 容许包含组也容许排除组.

例如,当由因为最近的修改而临时破坏的测试而又没有时间去修复它们时非常有用。无论如何,你想要干净的运行功能性测试,因此你想要是这些测试失效,但是记住它们重新被激活。
一个简单的解决这个问题的方法是创建一个称为"broken"的组并让这些测试方法归属它。例如,在上面的例子中,我知道testMethod2() 现在被破坏了,所有我想关闭它:

@Test(groups = {"checkintest", "broken"} )
public void testMethod2() {
}

现在我所想要做的只是在运行中排除这个组:

<test name="Simple example">
  <groups>
    <run>
      <include name="checkintest"/>
      <exclude name="broken"/>
    </run>
  </groups>
 
  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>

用这种方法,我将得到一个干净的测试运行,同时记录了那些被破坏并想要后续修复的测试。
注意:你也可以通过使用在@Test and @Before/After annotations上的"enabled"属性在个体的层面上关闭测试,

5.4 - Partial groups
你可以在类的级别上定义组,然后在方法的层次上添加组:

@Test(groups = { "checkin-test" })
public class All {

  @Test(groups = { "func-test" )
  public void method1() { ... }

  public void method2() { ... }
}

在这个类中,method2() 属于组"checkin-test",在类的级别定义。而method1() 同时属于 "checkin-test" 和 "func-test".

posted on 2008-03-23 13:14 sky ao 阅读(2221) 评论(5)  编辑  收藏 所属分类: software test

评论

# re: TestNG官方文档中文版(5)-测试方法/类和组 2008-03-23 13:25 完美世界私服

http://www.wmsifu.cn  回复  更多评论   

# re: TestNG官方文档中文版(5)-测试方法/类和组 2008-03-23 13:30 飘然

晕倒,私服广告做到这里来了...

大家不要点,无视一楼。  回复  更多评论   

# re: TestNG官方文档中文版(5)-测试方法/类和组 2008-03-24 11:39 阿里

谢谢!
希望楼主翻译完后做个目录。  回复  更多评论   

# re: TestNG官方文档中文版(5)-测试方法/类和组 2008-03-24 21:03 mei

如何把一个excel文件中内容或者.csv file中内容读出,这个是不是叫做data dirive?   回复  更多评论   

# re: TestNG官方文档中文版(5)-测试方法/类和组 2008-12-07 00:14 阿虎

谢谢lz
交个朋友吧  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: