应用场景
在测试方法2执行前必须执行测试方法1,如果测试方法1执行失败,测试方法2就不执行了;比如我们要执行下单的测试方法,需要依赖登录的测试方法,登录成功了才能继续跑下单的测试方法,否则就不会执行下单的测试方法
实现方法
用到@Test注解的dependsOnMethods属性,格式@Test(dependsOnMethods = "测试方法名称")
如下代码test2依赖test1
package com.course.testng;
import org.testng.annotations.Test;
public class DependTest {
@Test
public void test1() {
System.out.println("test1");
}
@Test(dependsOnMethods = "test1")
public void test2() {
System.out.println("test2");
}
}

右键执行test2方法,执行结果如下:
先执行了test1,后执行了test2

如果test1方法执行失败,看下程序是如何执行的
代码如下:test1方法抛出异常
package com.course.testng;
import org.testng.annotations.Test;
public class DependTest {
@Test
public void test1() {
System.out.println("test1");
throw new RuntimeException();
}
@Test(dependsOnMethods = "test1")
public void test2() {
System.out.println("test2");
}
}
运行test2方法,执行结果如下:test1执行失败、test2被忽略不执行

21

被折叠的 条评论
为什么被折叠?



