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. Triangular Pastures POJ - 1948

    Triangular Pastures POJ - 1948 sum表示木条的总长.a[i]表示第i根木条长度.ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能. 那么a ...

  2. [转]C# 邮箱验证激活

    原文链接 /// <summary> /// 发送邮件 发送激活码 /// </summary> /// <param name="address"& ...

  3. 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...

  4. poj1781In Danger(约瑟夫) 问题

    链接 之前队内赛中的一道题目 当时怎么想也没想到,就一直放到了今天,刚才看另一题的讲解突然看到时拿这个题作为引子来讲的,就仔细看了下. 参考<<具体数学>> p7. Josep ...

  5. mybatis的mapper.xml文件细节

  6. (五)SpringIoc之Bean的作用域

    此文转自 https://blog.csdn.net/icarus_wang/article/details/51586776# 有状态bean和无状态bean请看 https://blog.csdn ...

  7. CF949A/950C Zebras

    思路: 贪心乱搞. 实现: #include <bits/stdc++.h> using namespace std; vector<vector<int>> v; ...

  8. js基础 -----鼠标事件(按下 拖拽)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret。

    如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret. 申请流程如下: 步骤1:登陆腾讯开放平台.链接地址: http://open.qq.com/ . 步骤2:填 ...

  10. 分享div、text、Box Shadow(阴影)演示及代码的页面

    附图: 直接上链接:www.css88.com/tool/css3Preview/Box-Shadow.html