@Test(groups = {""})

  在执行测试用例的时候,往往一个功能依赖多个测试用例,比如流程的测试,那么这个时候就可以用到组测试,把流程涉及到测试用例都分到同一组里,按组执行即可。

  testNG的组通过@Test的groups属性来指定的,一个方法可以属于一个组(@Test(groups = {"checkintest"})),也可以属于多个组(@Test(groups = {"functest","checkintest"}))。

  假设现在有3个java代码,common.java、functionA.java、functionB.java,测试流程涉及到common.java中 login() 和 quit() 方法、functionA.java中的 testMethod1() 方法、functionB.java中的 testMethod3() 方法,那么可以将这4个方法分到同一组里functest,代码如下:

common.java:

import org.testng.annotations.Test;

public class common {
@Test(groups = {"functest","checkintest"})
public void login() {
System.out.println("login");
} @Test(groups = {"functest","checkintest"})
public void quit() {
System.out.println("quit");
} @Test(groups = {"checkintest"})
public void init() {
System.out.println("init");
}
}

functionA.java:

import org.testng.annotations.Test;

public class functionA {
@Test(groups = {"functest"})
public void testMethod1() {
System.out.println("this is testMethod1");
} @Test(groups = {"functest2"})
public void testMethod2() {
System.out.println("this is testMethod2");
}
}

functionB.java:

import org.testng.annotations.Test;

public class functionB {
@Test(groups = {"functest"})
public void testMethod3() {
System.out.println("this is testMethod3");
} @Test(groups = {"functest2"})
public void testMethod4() {
System.out.println("this is testMethod4");
}
}

testng.xml:

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

<suite name="Suite1" verbose="1" >
<test name="Regression1" preserve-order="true">
<groups>
<run>
<include name = "functest" />
</run>
</groups>
<classes>
<class name="common"></class>
<class name="functionA"></class>
<class name="functionB"></class>
</classes>
</test>
</suite>

注意:testng.xml需要指定要测试的组(<groups>……</groups>)和组所在的class(<classes>……</classes>)

运行testng.xml,执行结果如下:

login
quit
this is testMethod1
this is testMethod3

@Test(priority = )

一般quit都是在流程的最后才执行,如何控制组里方法执行的顺序呢?可以通过@Test的priority属性,testNG按照priority从小到大的顺序执行

修改common.java,在@Test中添加属性priority = 1和priority = 4:

import org.testng.annotations.Test;

public class common {
@Test(groups = {"functest","checkintest"},priority = 1)
public void login() {
System.out.println("login");
} @Test(groups = {"functest","checkintest"},priority = 4)
public void quit() {
System.out.println("quit");
} @Test(groups = {"checkintest"})
public void init() {
System.out.println("init");
}
}

修改functionA.java,在@Test中添加属性priority = 2:

import org.testng.annotations.Test;

public class functionA {
@Test(groups = {"functest"},priority = 2)
public void testMethod1() {
System.out.println("this is testMethod1");
} @Test(groups = {"functest2"})
public void testMethod2() {
System.out.println("this is testMethod2");
}
}

修改functionB.java,在@Test中添加属性priority = 3:

import org.testng.annotations.Test;

public class functionB {
@Test(groups = {"functest"}, priority = 3)
public void testMethod3() {
System.out.println("this is testMethod3");
} @Test(groups = {"functest2"})
public void testMethod4() {
System.out.println("this is testMethod4");
}
}

再次运行testng.xml,执行结果如下:

login
this is testMethod1
this is testMethod3
quit

如何只执行组里 testMethod1() 和 testMethod3() 方法,而不执行 login() 和 quit() 方法呢?

修改testng.xml,添加<exclude name="checkintest"></exclude>,排除在checkintest组里的方法

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

<suite name="Suite1" verbose="1" >
<test name="Regression1" preserve-order="true">
<groups>
<run>
<include name = "functest" />
<exclude name="checkintest"></exclude>
</run>
</groups>
<classes>
<class name="common"></class>
<class name="functionA"></class>
<class name="functionB"></class>
</classes>
</test>
</suite>

再次运行testng.xml,执行结果如下:

this is testMethod1
this is testMethod3

testNG之组测试的更多相关文章

  1. TestNG的组测试和组中组测试

    在编写测试的过程中,我们经常遇到只想执行个别或者某一部分/某一类型的测试用例,这时我们可以使用TestNG的分组测试方法 分组测试在配置时,TestNG执行的原则是:只保留最小集合进行执行 看代码: ...

  2. TestNg 4.组测试中的方法分组测试

    看以下代码: package com.course.testng.groups; import org.testng.annotations.AfterGroups; import org.testn ...

  3. testng入门教程7 TestNG组测试

    在TestNG中组测试是一个新的创新功能,它不存在于JUnit框架,它允许调度到适当的部分方法和瓶坯复杂的测试方法分组.您不仅可以声明属于群体的那些方法,但你也可以指定一组包含其他组.然后,TestN ...

  4. 学习使用TestNG进行数据驱动测试

    转自: https://mp.weixin.qq.com/s/8Bd8LEhiC2pu2VMcyNMGlQ 学习使用TestNG进行数据驱动测试 赵吃饭 51Testing软件测试网 前天   学习使 ...

  5. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  6. testng多线程并行执行测试

    testng多线程并行执行测试 testng多线程并行执行测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力.TestNG允许我们以 ...

  7. testNG之异常测试

    @Test(expectedExceptions = ) 在测试的时候,某些用例的输入条件,预期结果是代码抛出异常,那么这个时候就需要testNG的异常测试,先看一段会抛出异常的代码 exceptio ...

  8. TestNG 组测试

    方法分组测试 1. 给@Test注解后面加groups参数,如 @Test(groups = "groupa") 2. 可以添加@BeforeGroups和@AfterGroups ...

  9. TestNG(七)组测试

    package com.course.testng.groups; import org.testng.annotations.AfterGroups; import org.testng.annot ...

随机推荐

  1. list的过滤操作

    假设 l = ['abc', 'mn', 'aq', 'liuming'] 我要过滤出以a开头的元素,方法有以下两种 方法1: l = ['abc', 'mn', 'aq', 'liuming'] l ...

  2. 多线程模拟生产者消费者示例之Lock

    public class Test { public static void main(String[] args) { List<String> list = new ArrayList ...

  3. LDD3 第10章 中断处理

    各种硬件和处理器打交道的周期不同,并且总是比处理器慢.必须有一种可以让设备在产生某个事件时通知处理器----中断. 中断仅仅是一个信号,如果硬件需要,就可以发送这个信号.Linux处理中断方式和用户空 ...

  4. 【LeetCode 76】最小覆盖子串

    题目链接 [题解] 尺取法. 用l和r代表一个合法的覆盖子串. 我们不断地扩大右指针. 直到l..r包含T中的所有字母为止(重复的就要两次以上.) 然后我们可以尝试的让l++. 看看新的l..r是不是 ...

  5. 【Flutter学习】基本组件之弹窗和提示(SnackBar、BottomSheet、Dialog)

    一,概述 Flutter中的操作提示主要有这么几种 SnackBar.BottomSheet.Dialog,因为 Dialog样式比较多,放最后讲好了 二,介绍 SnackBar SnackBar的源 ...

  6. ThinkPHP整合datatables实现服务端分页

    最近做东西有一个需求,因为数据量很大,在这里我决定使用datatables的服务端分页,同时还需要传递查询条件到服务端.在网上搜索的大部分文章都感觉有些误差,于是自己封装了一下,主要配置/工具为: 服 ...

  7. mysql审计插件

    Audit Plugin安装使用 原文: https://www.cnblogs.com/waynechou/p/mysql_audit.html#_label0   #有卸载方法 下载地址: htt ...

  8. 6 November in 614

    Contest A. greet map,完了. B. gift map,完了. C. [Usaco2008 Nov Gold] 安慰奶牛 最小生成树.新边权设为原边权的两倍,再加上两端点的点权.完了 ...

  9. JS 替换

    JS 字符串有replace() 方法.但这个方法只会对匹配到的第一个字串替换. 如下例: var str = "wordwordwordword"; var strNew = s ...

  10. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalabl ...