前面说了,在同一个类内,TestNG默认是按照方法名首字母升序执行方法。这种方式虽然写起来会方便,个人觉得太过隐晦,对新手也不是很友好。
我们可以在@Test注解的TestNG方法加上dependsOnMethods = {"方法名"}或dependsOnGroups= {"组名"}来添加依赖关系,当然这些方法名和组名也是支持正则表达式的。
下面的代码使用了dependsOnMethods属性来指定了所依赖的方法。依赖组的写法就留给读者自己去实践辣。
public class TestDependency {
@Test(dependsOnMethods = {"firstMethod"})
public void secondMethod(){
System.out.println("我是secondMethod, 虽然我是第一个声明的,但是我依赖于firstMethod,所以我后跑");
}
@Test
public void firstMethod(){
System.out.println("我是firstMethod,尽管我声明的顺序是后面的,但是我被依赖了,所以我先跑");
}
}
运行结果: