TestNg依赖 dependsOnGroups

来源:互联网 发布:钓鱼 知乎 编辑:程序博客网 时间:2024/06/08 09:00

第二种依赖方式:dependsOnGroups

强制依赖:如果被依赖的某一个方法发生了异常,那么之后的方法都不会被执行(默认)

顺序依赖:无论被依赖的方法是否出现异常,后续的方法都会被执行,通过alwaysRun=“true”来配置


顺序依赖举例:

代码:
package com.testcase;


import org.testng.annotations.Test;


public class TestngDependOnGroups {
@Test(groups = { "init" })  
   public void serverStartedOk() {  
       System.out.println("serverStartedOk.....");  
   } 
@Test(groups = {"init2"})
public void initEnvironment(){
System.out.println("initEnvironment....."); 
throw new RuntimeException("unexpected fail......"); 
}
@Test(dependsOnGroups = { "init.*" }, alwaysRun = true)  
   public void method1() {  
       System.err.println("I am over here.....");  
   } 
}

testng.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="com.testcase.TestngDependOnGroups"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

运行结果:(所有测试均执行)
[TestNG] Running:
  F:\android\android_work\Test2\src\testng.xml


initEnvironment.....
serverStartedOk.....
I am over here.....


===============================================
Suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================

强制依赖举例:

代码:

package com.testcase;


import org.testng.annotations.Test;


public class TestngDependOnGroups {
@Test(groups = { "init" })  
   public void serverStartedOk() {  
       System.out.println("serverStartedOk.....");  
   } 
@Test(groups = {"init2"})
public void initEnvironment(){
System.out.println("initEnvironment....."); 
throw new RuntimeException("unexpected fail......"); 
}
@Test(dependsOnGroups = { "init.*" })  //说明:相比顺序依赖缺少了 alwaysRun = true
   public void method1() {  
       System.err.println("I am over here.....");  
   } 
}

testng.xml配置与顺序依赖一样

执行结果:

[TestNG] Running:
  F:\android\android_work\Test2\src\testng.xml


initEnvironment.....
serverStartedOk.....


===============================================
Suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================


0 0