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

via:http://www.blogjava.net/aoxj

TestNG官方文档中文版(5)-测试方法/类和组的更多相关文章

  1. TestNG官方文档中文版(2)-annotation(转)

    1. 介绍    TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器). 编写一个测试的 ...

  2. TestNG官方文档中文版(2)-annotation

    TestNG的官方文档的中文翻译版第二章,原文请见 http://testng.org/doc/documentation-main.html 2 - Annotation 这里是TestNG中用到的 ...

  3. TestNG官方文档中文版(1)-介绍

    TestNG的官方文档请见: http://testng.org/doc/documentation-main.html 1. 介绍    TestNG是一个设计用来简化广泛的测试需求的测试框架,从单 ...

  4. TestNG官方文档中文版(3)-testng.xml

    TestNG的官方文档的中文翻译版第3章,原文请见 http://testng.org/doc/documentation-main.html 3 - testng.xml 调用TestNG由几种不同 ...

  5. TestNG官方文档中文版(4)-运行TestNG

    4 - 运行TestNG TestNG可以以不同的方式调用: * Command line     * ant     * Eclipse     * IntelliJ's IDEA 1) 命令行 假 ...

  6. 人工智能系统Google开源的TensorFlow官方文档中文版

    人工智能系统Google开源的TensorFlow官方文档中文版 2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,机器学习作为人工智能的一种类型,可以让软件根据大量的 ...

  7. bootbox.js官方文档中文版

    bootbox.js官方文档中文版简介:Bootbox.js是一个小型的JavaScript库,基于Bootstrap模态框开发,用于创建可编程的对话框. 不像原生的alert等对话框,所有的Boot ...

  8. 2DToolkit官方文档中文版打地鼠教程(三):Sprite Collections 精灵集合

    这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...

  9. 2DToolkit官方文档中文版打地鼠教程(二):设置摄像机

    这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...

随机推荐

  1. js防止客户端多触发

    代码: /***防止多触发**id 必须唯一*fn 回掉函数*wait 延迟多长时间**使用例子:* ToPreventMoreTrigger('id', function () {//注意 id 是 ...

  2. WPF入门教程系列一——基础

    一. 前言   最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料.为了温故而知新把学习过程记录下来,以备后查.这篇主要讲WPF的开发基础,介绍了如何使用V ...

  3. 《PHP Manual》阅读笔记2

    本次笔记截止到 语言参考 流程控制. 1.没有结合的相同优先级的运算符不能连在一起使用,例如 1 < 2 > 1 在PHP是不合法的.但另外一方面表达式 1 <= 1 == 1 是合 ...

  4. 在Ubuntu搭建.NET Core环境

    Ubuntu16.04配置.net core环境   Ubuntu 16.04 desktop下载地址:http://www.ubuntu.com/desktop 本次是用vmware安装该系统.   ...

  5. (转)Tomcat启动报Error listenerStart错误

    今天启动Tomcat启动不了,报以下错: org.apache.catalina.core.StandardContext startInternalSEVERE: Error listenerSta ...

  6. Spring AOP AspectJ Pointcut Expressions With Examples--转

    原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...

  7. 栈的存储结构和常见操作(c 语言实现)

    俗话说得好,线性表(尤其是链表)是一切数据结构和算法的基础,很多复杂甚至是高级的数据结构和算法,细节处,除去数学和计算机程序基础的知识,大量的都在应用线性表. 一.栈 其实本质还是线性表:限定仅在表尾 ...

  8. Spring学习总结(三)——Spring实现AOP的多种方式

    AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是Spring框架中的一个 ...

  9. JS实现弹出层对话框

    点击按钮后,弹出层对话框,可交互,点击关闭后才关闭掉对话框. 效果图: 源码: <!doctype html> <html> <head> <meta cha ...

  10. QQ JS_SDk相关功能接口

    一.实现QQ登录功能 <!DOCTYPE html><html lang="zh-cn">   <head>      <meta htt ...