java文件

package selniumhomework;

import org.testng.annotations.Test;

public class Test1 {
@Test(groups = {"regression"})
public void method1() {
System.out.println("Method 1 printed");
}
@Test(groups = {"regression"})
public void method2() {
System.out.println("Method 2 printed");
}
@Test(groups = {"smoke"})
public void method3() {
System.out.println("Method 3 printed");
}
@Test(groups = {"regression"})
public void method4() {
System.out.println("Method 4 printed");
}
@Test(groups = {"smoke"})
public void method5() {
System.out.println("Method 5 printed");
}
}

case1:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<include name = "regression"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1">
<methods>
   <include name = "method1"/>
   <include name = "method2"/>
   <include name = "method3"/>
   <include name = "method4"/>
   <include name = "method5"/>
</methods>
</class>
</classes>
</test>
</suite>

结果: 当class 包含有methods时,无论method是否被标注为regression,都会被执行。所以5个方法都被打印。当method少了 include method1 时 则不会打印method1(即使它被标注为regression的groups)。所以结论是:

当 class 包含有 methods时,则运行 method include 的方法,不会运行 分组(groups,包括 inlcude和 exclude 都不会运行)里的方法。

----------------------------------------------------------------------------------------------------------------------

case2:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<include name = "regression"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1" />
</classes>
</test>
</suite>

结果: 该配置打印那些被标注为 groups=“regression” 的方法。

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

多增加一个java文件:

package selniumhomework;

import org.testng.annotations.Test;

public class Test2 {
@Test(groups = {"smoke"})
public void method1() {
System.out.println("F2: method1");
} @Test(groups = {"regression"})
public void method2() {
System.out.println("F2: method2 ");
} @Test(groups = {"regression", "smoke"}) //注意别写成:groups={"regression,smoke"}
public void method3() {
System.out.println("F2: method3");
} @Test(groups = {"smoke"})
public void method4() {
System.out.println("F2: method4");
}
}

-----------------------------------------------------------------------------------

case1

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<include name = "regression"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1" />
<class name="selniumhomework.Test2" />
</classes>
</test>
</suite>

结果: 打印两个java文件中所有被标注为 groups={"regression"}的方法:

Method 1 printed

Method 2 printed

Method 4 printed

F2: method2

F2: method3

-----------------------------------------------------------------------------------

case2

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<exclude name = "regression"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1" />
<class name="selniumhomework.Test2" />
</classes>
</test>
</suite>

结果: 打印两个java文件中所有被标注为 groups={"regression"}除外的其他方法 (exclude :不运行的方法):

Method 3 printed

Method 5 printed

F2: method1

F2: method4

-----------------------------------------------------------------------------------

case3

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<exclude name = "regression"/>
<include name = "smoke"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1" />
<class name="selniumhomework.Test2" />
</classes>
</test>
</suite>

结果:当某个方法既属于include的组,又属于exclude的组,那么exclude的组优先。所以该配置打印两个java文件中:

1). 排除所有被regression的分组;

2). 属于smoke的分组; 如果一个方法既属于regression的分组又属于smoke的分组,则不打印,如Test2的 method3就没有打印出来。

Method 3 printed

Method 5 printed

F2: method1

F2: method4

-------------------------------------------------------------------------------------

case4

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
  <define name = "chrome">
<include name = "regression"/>
<include name = "smoke"/>
</define>
<define name = "firefox">
<include name = "regression"/>
</define>
<run>
<include name = "chrome"/>
</run>
</groups>
<classes>
<class name="selniumhomework.Test1"/>
<class name="selniumhomework.Test2"/>
</classes>
</test>
</suite>

这里自定义了两个组,分别针对chrome 和 firefox 的测试策略。chrome分组包含了regression和smoke两组分组;firefox分组则只包含了regression的分组。这样形成了分组包含分组,在设计分组的层次关系时,定义新组能够带来灵活性:可以在代码中使用粒度非常小的分组,然后在运行时刻将这些小分组合并成大分组。这里既可以运行chrome的分组或firefox的分组,还可以运行regression或smoke 的分组,灵活性很高。

Method 1 printed

Method 2 printed

Method 3 printed

Method 4 printed

Method 5 printed

F2: method1

F2: method2

F2: method3

F2: method4

-------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------

Note:

1). 可以配置两个.xml文件来驱动case的运行,譬如test1.xml 运行 regression 的cases; test2.xml 运行 smoke 的cases.

而Automatin team 也正是这样做的:在src/test/resources 下的一个文件CISute 中包含两个xml文件,coreRegressionTest.xml

和smokeTest.xml.

2). You can define groups at the class
level and then add groups at the method level:

@Test(groups = { "smoke" })

public class All {

   @Test(groups = { "regression" )

    public void method1() { ... }

    public void method2() { ... }

}

In this class, method2() is part of the group "smoke", which is defined at the class level, while method1() belongs to both "smoke" and "regression".

TestNG 练习的更多相关文章

  1. TestNG 入门教程

    原文出处:http://www.cnblogs.com/TankXiao/p/3888070.html 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装T ...

  2. JUnit 4 与 TestNG 对比

    原文出处: 付学良的网志 原文出处2: http://www.importnew.com/16270.html -------------------------------------------- ...

  3. JAVA+Maven+TestNG搭建接口测试框架及实例

    1.配置JDK 见另一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.html 2.安装Eclipse以及TestNG Eclipse下载地址:ht ...

  4. Idea+TestNg配置test-output输出

    说明:testNG的工程我是使用eclipse创建的,直接导入到idea中,运行test时不会生产test-output,只能在idea的控制台中查看运行结果,然后到处报告,经过不懈的百度终于找到怎么 ...

  5. testng 失败自动截图

    testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...

  6. 两种方式testng dataprovider结合csv做测试驱动

    方式一: 第一.读取csv数据源码 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream ...

  7. java分享第十九天(TestNg的IReporter接口的使用)

     IReporter接口是干嘛的?就是让用户自定义报告的,很多人想要自定义报告,于是乎找各种插件,比如什么testng-xslt啊,reportng啊,各种配置,最后出来的结果,还不能定制化,但为什么 ...

  8. java分享第十八天-02( java结合testng,利用XML做数据源的数据驱动)

    testng的功能很强大,利用@DataProvider可以做数据驱动,数据源文件可以是EXCEL,XML,YAML,甚至可以是TXT文本.在这以XML为例:备注:@DataProvider的返回值类 ...

  9. java分享第十四天(TestNG Assert详解)

     TestNG Assert 详解org.testng.Assert 用来校验接口测试的结果,那么它提供哪些方法呢? 中心为Assert测试类,一级节点为方法例如assertEquals,二级结点为参 ...

  10. TestNG Assert 详解

    org.testng.Assert 用来校验接口测试的结果,那么它提供哪些方法呢? 中心为Assert测试类,一级节点为方法例如assertEquals,二级结点为参数类型及参数个数,double 3 ...

随机推荐

  1. 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

    题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...

  2. django中实现websocket

    一.Websockets介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信 ...

  3. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  4. Python基础第一天

    诞生时间:1991年,创造者Guido van Rossum 优点: 1.简单  Python是一种代表简单注意思想的语言 2.易学  Python是及其容易上手,因为Python有极其简单的说明文档 ...

  5. Webform 三级联动例子

    首先分别做三个下拉列表 <body> <form id="form1" runat="server"> <asp:DropDown ...

  6. 开发一个 Web App 必须了解的那些事

    在过去的一年里,我在从头开始开发我的第一个重要的Web应用.经验教会了很多以前不知道的东西,特别是在安全性和用户体验方面. 值得一提的是,我上一次尝试构建的任何合理复杂性是在2005年.所以,在安全防 ...

  7. 平衡图片负载,提升web站点访问体验

    最近给分公司做官方网站,内网测试一切ok,发布至云端后,体验惊人——公司外网网速渣渣(十几k~几十k),更加要命的是,网站的高清图,根本就加载不出来,几秒,十几秒过去了,仍然在转圈圈,如下图... 于 ...

  8. Struts2------拦截器和标签库和注解开发

    一.解析Struts2源码中拦截器的执行 客户端请求Action,执行前端控制器,在前端控制器内部创建了Action的代理类,调用代理类的execute方法,在execute方法内部执行ActionI ...

  9. 解决webstromm标签高亮问题

      2017/2016版  

  10. TCP/UDP套接字 java socket编程实例

    网络协议七层结构: 什么是Socket? socket(套接字)是两个程序之间通过双向信道进行数据交换的端,可以理解为接口.使用socket编程也称为网络编程,socket只是接口并不是网络通信协议. ...