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. 24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package zhongqiuzuoye; public class Car { String brand; public void drive() {} } package zhongqiuzuo ...

  2. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  3. Shell最多可以输入多少个参数?

    在脚本编写过程中,通常会涉及到参数的输入.譬如,sh 1.sh 10 20,在执行1.sh这个脚本中,10即为第一个参数,20即为第二个参数.有时,就会有这个疑惑,即shell脚本最多可以支持多少个变 ...

  4. Windows编程中UNICODE和_UNICODE定义问题

    Windows编程中UNICODE和_UNICODE定义问题 先转一篇文章: 初学Windows SDK编程时碰到过这个问题,相信很多初学Windows编程的人也都碰到过,后来慢慢搞明白些了,但有时别 ...

  5. Android学习笔记之短信验证码的获取和读取

    PS:最近很多事情都拖拖拉拉的..都什么办事效率啊!!! 还得吐槽一下移动运营商,验证码超过五次的时候,直接把我的手机号封闭.真是受够了. 学习笔记: 1.Android之如何获取短信验证码. 2.如 ...

  6. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要二

    今天继续分享我的阅读<LEARNING HARD C#学习笔记>知识点总结与摘要二,仍然是基础知识,但可温故而知新. 七.面向对象 三大基本特性: 封装:把客观事物封装成类,并隐藏类的内部 ...

  7. Android Studio快捷键每日一练(4)

    原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-4/ 33.分析数据流到当前位置 苹果/Windows ...

  8. 解决在IE中获取数据的缓存问题,运行环境为node.js

    IE下默认会开启缓存策略,不管是页面还是通过ajax请求的数据都会议一个url,url是uri(统一资源定位符)的实例,url就是资源的标识符. 写一个demo进行验证,测试环境:IE8,node.j ...

  9. C# DllImport用法和路径问题

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息.    DllImport属性应用于方法,要 ...

  10. javascript日期验证:填写的日期大于等于当前日期

    <script> $(function () { var d = new Date(); var strDate = getDateStr(d); $("#beginTime&q ...