有时候,我们需要按顺序来调用测试用例,那么测试用例之间就存在依赖关系。
TestNG支持测试用例之间的依赖:
- 测试案例依赖:@Test(dependsOnMethods = {"dependedMethods"})
- 测试组依赖:@Test(dependsOnGroups = {"dependedGroups"})
测试案例依赖
package me.yezhou;
import org.testng.annotations.Test;
public class DependsOnTest {
@Test
public void onLoad() {
System.out.println("onLoad");
}
@Test(dependsOnMethods = { "onLoad" })
public void onReady() {
System.out.println("onReady");
}
@Test(dependsOnMethods = { "onReady" })
public void onShow() {
System.out.println("onShow");
}
}
测试结果:
onLoad
onReady
onShow
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
测试组依赖
package me.yezhou;
import org.testng.annotations.Test;
public class DependsOnTest {
@Test(groups = { "group1" })
public void testCase1() {
System.out.println("testCase1");
}
@Test(groups = { "group2" })
public void testCase2() {
System.out.println("testCase2");
}
@Test(dependsOnGroups = { "group1" })
public void testCase3() {
System.out.println("testCase3");
}
@Test(dependsOnGroups = { "group.*" })
public void testCase4() {
System.out.println("testCase4");
}
}
测试结果:
testCase1
testCase2
testCase3
testCase4
===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
注:group.* 是通配符