用例依赖大致分为 测试方法依赖和测试组依赖
- 测试方法依赖
目录如下:
DependentMethodsTest.java 代码如下:
package com.testng.cn;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class DependentMethodsTest {
@Test
public void testAdd1(){
assertEquals(3+1, 5);
}
@Test(dependsOnMethods = {"testAdd1"})
public void testAdd2(){
assertEquals(3+2, 5);
}
}
dependsOnMethods 来设置用例的依赖, 当 testAdd1() 运行失败时, 则 testAdd2() 不再被执行。
运行结果如下:
- 测试组依赖
目录如下:
DependentGroupsTest.java代码如下:
package com.testng.cn;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class DependentGroupsTest {
@Test(groups={"funtest"})
public void testAdd1(){
assertEquals(3+1, 5);
}
@Test(groups={"funtest"})
public void testAdd2(){
assertEquals(3+2, 5);
}
@Test(dependsOnGroups = {"funtest"})
public void testAdd3(){
assertEquals(3+2, 5);
}
}
dependsOnGroups 来设置组的依赖, testAdd1()和 testAdd2() 同属于于 funtest 组, testAdd3() 依赖于 funtest 组, 该组有中有一条用例运行失败, 则 testAdd3() 不再执行。
运行结果如下: