`

testNG学习之分组测试和忽略测试

阅读更多
tesng教程:https://www.yiibai.com/testng
1、依靠group分组忽略某些方法执行
       testNG通过group属性实现分组,然后在testng.xml中通过<groups>标签指定哪个组的方法运行,哪个不运行。分为方法分组和类分组,下面用代码示例说明。
       方法分组:
       <code="java">
       package com.test.groups;
       import org.testng.annotations.Test;
       public class GroupsOnMethod {
             @Test(groups = "server")
             public void serverTest1() {
                   System.out.println("这是服务端组测试方法1");
             }

             @Test(groups = "server")
             public void serverTest2() {
                  System.out.println("这是服务端组测试方法2");
             }

             @Test(groups = "client")
             public void clientTest1() {
                  System.out.println("这是客户端组测试方法1111");
             }

            @Test(groups = "client")
            public void clientTest2() {
                  System.out.println("这是客户端组测试方法2222");
            }

            @Test(groups = "thirdParty")
            public void thirdPartyTest1() {
                 System.out.println("这是第三方组测试方法1111");
            }

            @Test(groups = "thirdParty")
            public void thirdPartyTest2() {
                  System.out.println("这是第三方组测试方法2222");
            }
       }
       </code>
       类分组:
       <code="java">
       package com.test.groups;
       import org.testng.annotations.Test;

       @Test(groups = "stu")
       public class GroupsOnClass1 {
             public void stu1() {
                   System.out.println("GroupsOnClass1中的stu1运行!");
             }
             public void stu2() {
                  System.out.println("GroupsOnClass1中的stu2运行!");
             }
       }
       </code>
       <code="java">
       package com.test.groups;
       import org.testng.annotations.Test;

       @Test(groups = "stu")
       public class GroupsOnClass2 {
             public void stu1() {
                   System.out.println("GroupsOnClass2中的stu1运行!");
             }
             public void stu2() {
                  System.out.println("GroupsOnClass2中的stu2运行!");
             }
       }
       </code>
       <code="java">
       package com.test.groups;
       import org.testng.annotations.Test;

       @Test(groups = "teacher")
       public class GroupsOnClass3 {
             public void teacher1() {
                   System.out.println("GroupsOnClass3中的teacher1运行!");
             }
             public void teacher2() {
                  System.out.println("GroupsOnClass3中的teacher2运行!");
             }
       }
       </code>
      在testng.xml中如下配置:
      <code="java">
      <?xml version="1.0" encoding="utf-8" ?>
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      <suite name="groupSuite" preserve-order="true">
          <test name="groupOnMethodTest">
              <groups>
                  <run>
                      <!-- 不包含某个分组 -->
                      <exclude name="client"/>
                 </run>
             </groups>
             <classes>
                 <class name="com.janson.groups.GroupsOnMethod"/>
             </classes>
          </test>
         <test name="groupOnClassTest">
             <groups>
                 <run>
                     <!-- 包含某个分组 -->
                     <include name="teacher"/>
                </run>
            </groups>
            <classes>
                <class name="com.janson.groups.GroupsOnClass1"/>
                <class name="com.janson.groups.GroupsOnClass2"/>
                <class name="com.janson.groups.GroupsOnClass3"/>
            </classes>
         </test>
      </suite>
      </code>

      执行结果如下:
      这是服务端组测试方法1
      这是服务端组测试方法2
      这是第三方组测试方法1111
      这是第三方组测试方法2222
      GroupsOnClass3中的teacher1运行!
      GroupsOnClass3中的teacher2运行!
2、通过enabled参数忽略某些方法
       有时候某些测试方法暂时不需要执行,但是又不想删除代码,可能以后又需要执行,这时怎么办呢,我们可以使用testNG的忽略测试,就是在@Test中设置enabled属性为false,示例代码如下:
       <code="java">
       public class IgnoreTest {
             @Test(enabled = false)
             public void ignore1() {
                   System.out.println("ignore1 运行!");
             }

             @Test //默认是不忽略的
             public void ignore2() {
                   System.out.println("ignore2 运行!");
              }

             @Test(enabled = true)
             public void ignore3() {
                   System.out.println("ignore3 运行!");
             }
       }
       </code>
      执行结果如下:
            ignore2 运行!
            ignore3 运行!

3、通过groups设置依赖关系:
       有时候某个测试方法要等其他某个测试方法执行完才能执行,可以使用dependsOnMethods指定要依赖的测试方法,这个只能用在某个类里面,指定依赖本类中的方法,而当需要依赖其他类的方法时,可以通过dependsOnGroups,代码如下:
<code="java">
package com.propertiesfile;
import org.testng.annotations.Test;
public class WaybillSelect {
@Test(groups= "one")
public void test1() {
System.out.println("this is test1");
}
@Test(groups= "one")
public void tester1() {
System.out.println("this is tester1");
}
@Test
public void test2() {
System.out.println("this is test2");
}

}

package com.moneyselect;
import org.testng.annotations.*;

public class MoneySelectTest {

@Test
public void test1() {
System.out.println("this is moneyselect.test1 method");
}
@Test//(dependsOnGroups= {"one"})
public void beforesuitetest() {
         System.out.println("this is beforesuite test");
}
@Test
public void beforetest() {
System.out.println("this is beforetest  test");
}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="zhanglutest" verbose="2" parallel="classes" >
    <test name="testcase1">
    <classes>
   
    <class name="com.propertiesfile.WaybillSelect"></class>
            <class name="com.moneyselect.MoneySelectTest"></class>
           
        </classes>
    </test>
</suite>
</code>
       运行结果如下,如果不加依赖,因为设置了parallel="classes",所以两个类会在两个线程中按照各自的方法名字典升序排序运行,MoneySelectTest类中的方法顺序应该是beforesuitetest()、beforetest()、test1(),因为现在制定了beforesuitetest()要等待组名为one的组中的方法执行完后再执行,所以执行结果如下。
this is beforetest  test
this is test1
this is tester1
this is moneyselect.test1 method
this is beforesuite test
this is test2
       从上面的结果可以看出,添加了groups依赖后,WaybillSelect中的几个方法不是按照方法名字典顺序执行,而是先执行了设置了groups参数的方法,再执行没有该参数的方法,这个是为什么没有找到原因,难道是和依赖该group的方法有关?另外如果设置了多个groups,各个groups方法的执行顺序是怎样的也有待研究。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics