1. 目前組織test case的實踐
將所有測試方法放在Common Task的類中,然后根據(jù)test case的測試邏輯,創(chuàng)建對應的測試類,然后用TestNG運行這些測試類。
目前實踐的實例代碼如下:
包含所有測試方法CommonTasks文件:
import java.util.Random;
public class CommonTasks {
public int method1(int max) {
System.out.println("Run method1()");
return new Random().nextInt(max);
}
public int method2(int max) {
System.out.println("Run method2()");
return new Random().nextInt(max);
}
}
測試類TestCase1:先執(zhí)行method1,后執(zhí)行method2
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestCase1 {
CommonTasks task;
@BeforeClass
public void setUp() {
task = new CommonTasks();
}
@Test
@Parameters("max")
public void method1(int max) {
Assert.assertEquals(task.method1(max), 0, "Failed");
}
@Test(dependsOnMethods = "method1")
@Parameters("max")
public void method2(int max) {
Assert.assertEquals(task.method2(max), 0, "Failed");
}
}
測試類TestCase2:先執(zhí)行method2,后執(zhí)行method1
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestCase2 {
CommonTasks task;
@BeforeClass
public void setUp() {
task = new CommonTasks();
}
@Test(dependsOnMethods = "method2")
@Parameters("max")
public void method1(int max) {
Assert.assertEquals(task.method1(max), 0, "Failed");
}
@Test
@Parameters("max")
public void method2(int max) {
Assert.assertEquals(task.method2(max), 0, "Failed");
}
}