TestNG-分组groups

第一个例子:

java代码:

package com.kdzwy.practice;
import org.testng.annotations.Test;
/*
 *包名:com.kdzwy.practice
 *作者:Adien_cui
 *时间:2017-3-28  下午5:41:26
 *描述:testng分组
 **/
package com.kdzwy.practice;
import org.testng.annotations.Test;
/*
 *包名:com.kdzwy.practice
 *作者:Adien_cui
 *时间:2017-3-28  下午5:41:26
 *描述:testng分组
 **/
public class TestngGroups {
      @Test(groups = { "group1", "group2" })
      public void testMethod1() {
          System.out.println("testMethod1");
      }

      @Test(groups = {"group1", "group2"} )
      public void testMethod2() {
          System.out.println("testMethod2");
      }

      @Test(groups = { "group1" })
      public void testMethod3() {
          System.out.println("testMethod3");
      }
}


testng.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">

  <groups>
            <run>
                <inlude name="group2" />
            </run>
        </groups>
        <classes>
            <class name="com.kdzwy.practice.TestngGroups" />         
        </classes>
    </test>
</suite>


运行结果: 


可以看到在testng.xml的run里只配置了group2,所以只运行了testMethod1和testMethod2。

排除组

testng.xml配置如下

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <include name="group1" />
                <exclude name="group2" />
            </run>
        </groups>
        <classes>
            <class name="com.kdzwy.practice.TestngGroups" />         
        </classes>
    </test>
</suite>


运行结果: 

排除组

testng.xml配置如下

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <include name="group1" />
                <exclude name="group2" />
            </run>
        </groups>
        <classes>
            <class name="com.kdzwy.practice.TestngGroups" />         
        </classes>
    </test>
</suite>

运行结果: 

部分组

可以在类级别定义组,然后在方法级添加组:

 package com.kdzwy.practice;
import org.testng.annotations.Test;
/*
 *包名:com.kdzwy.practice
 *作者:Adien_cui
 *时间:2017-3-28  下午7:56:04
 *描述:部分组
 **/  
@Test(groups = { "class-group" })
public class TestngGroups2 {

      @Test(groups = { "method-group"})
      public void method1() {
          System.out.println("Method1");
      }

      public void method2() { 
          System.out.println("Method2");
      }
}



在这个类中,method2()是组“class-group”的一部分,它在类级别定义,而method1()属于“class-group”和“method-group”两者。
1
testng.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <include name="method-group" />
            </run>
        </groups>

        <classes>

   <class name="com.kdzwy.practice.TestngGroups2" />         
        </classes>
    </test>
</suite>

运行结果: 
这里写图片描述

testng.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <include name="class-group" />
            </run>
        </groups>

        <classes>
            <class name="com.kdzwy.practice.TestngGroups2" />         
        </classes>
    </test>
</suite>

运行结果: 

@BeforeGroups , @AfterGroups 注解使用

@BeforeGroups注解的方法将在本组内任何测试方法执行前被执行一次,可用于执行初始化操作。类似的@AfterGroups 注解的方法将在本组内任何测试方法执行后被执行,可用于关闭资源。 
java示例:

package com.kdzwy.practice;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

/*
 *包名:com.kdzwy.practice
 *作者:Adien_cui
 *时间:2017-3-28  下午8:12:33
 *描述:
 **/
public class TestngGroups3 {
      @BeforeGroups(groups={"group-b"})
      public void setUp(){
          System.out.println("Method---setup");
      }

      @AfterGroups(groups={"group-b"})
      public void tearDown(){
          System.out.println("Method---tearDown");
      }

      @Test(groups = { "group-a" })
      public void aaaMethod() {
          System.out.println("Method---aaa");
      }

      @Test(groups = { "group-b"} )
      public void bbbMethod() {
          System.out.println("Method---bbb");
      }

 @Test(groups = { "group-a","group-b" })
      public void cccMethod() {
          System.out.println("Method---ccc");
      }
}


testng.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="login">
        <groups>
            <run>
                <include name="group-b" />
            </run>
        </groups>

        <classes>
            <class name="com.kdzwy.practice.TestngGroups3" />         
        </classes>
    </test>
</suite>


运行结果: 


因为bbbMethod和cccMethod在组group-b里,所以可以看到setUp在bbbMethod之前运行了,而tearDown在cccMethod之后运行了
--------------------- 
 




作者:灵枢_ 
来源:CSDN 
原文:https://blog.csdn.net/galen2016/article/details/67644426 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值