1.@Test @BeforeMethod @AfterMethod @BeforeClass @AfterClass @BeforeTest @AfterTest @BeforeSuite @AfterSuite
1 import org.testng.annotations.*; 2 3 public class TestNGDemo { 4 5 //执行顺序 Suite->Test->Class->Method 6 //一个测试套件中包含多个测试项Test,一个Test中可以包含多个类,一个类中可以包含多个测试方法 7 @Test //被测试的方法 8 public void testCase1() { 9 System.out.println("This is TestCase1"); 10 } 11 12 @BeforeMethod //在测试方法执行之前执行(每个测试方法执行前都会执行一次) 13 public void beforeMethod() { 14 System.out.println("Run before the Test Method Everytime!"); 15 } 16 17 @AfterMethod//在测试方法执行之后执行(每个测试方法执行后都会执行一次) 18 public void afterMethod() { 19 System.out.println("Run after the Test Method Everytime!"); 20 } 21 22 @BeforeClass //在测试方法所在类执行之前执行 23 public void beforeClass() { 24 System.out.println("Run before the Class"); 25 } 26 27 @AfterClass //在测试方法所在类执行之后执行 28 public void afterClass() { 29 System.out.println("Run after the Class"); 30 } 31 32 @BeforeSuite //在测试套件执行之前执行 (在类之前) 33 public void beforeSuite() { 34 System.out.println("Run before the Suite"); 35 } 36 37 @AfterSuite //在测试套件执行之后执行 38 public void afterSuite() { 39 System.out.println("Run after the Suite"); 40 } 41 42 @BeforeTest //在套件中的一个测试执行之前执行 43 public void beforeTest() { 44 System.out.println("run before test"); 45 } 46 @AfterTest //在套件中的一个测试执行之后执行 47 public void afterTest() { 48 System.out.println("run after test"); 49 } 50 }
2.忽略测试:@Test(enabled = false) //忽略测试 设置enabled = false,则不会执行
3.方法分组测试
1 import org.testng.annotations.AfterGroups; 2 import org.testng.annotations.BeforeGroups; 3 import org.testng.annotations.Test; 4 5 public class MethodOnGroup { 6 7 @BeforeGroups(groups = "group1")//所在组方法运行之前运行 8 public void beforeGroup() { 9 System.out.println("run before group1"); 10 } 11 @AfterGroups(groups = "group1")//所在组所有方法运行之后运行 12 public void afterGroup() { 13 System.out.println("run after group1"); 14 } 15 @Test(groups = "group1") 16 public void test1() { 17 System.out.println("test1 method on group1"); 18 } 19 20 @Test(groups = "group1") 21 public void test2() { 22 System.out.println("test2 method on group1"); 23 } 24 25 @Test(groups = "group2") 26 public void test3() { 27 System.out.println("test3 method on group2"); 28 } 29 30 @Test(groups = "group2") 31 public void test4() { 32 System.out.println("test4 method on group2"); 33 } 34 35 }
4.类分组测试,可以在类前面添加@Test注解并分组: @Test(groups = "group3")
1 @Test(groups = "group3") 2 public class ClassOnGroup3 { 3 4 public void test1() { 5 System.out.println("test1 method on group3"); 6 } 7 8 public void test2() { 9 System.out.println("test2 method on group3"); 10 } 11 }
可以在testng.xml中指定运行哪些组上的类或方法(只会运行group1上的方法和group4所在类)--(Eclipse中添加testng.xml右键项目-TestNG-Convert to TestNG)
1 <test thread-count="3" name="Login"> 2 <groups> 3 <run> 4 <include name="group1"></include> 5 <include name="group4"></include> 6 </run> 7 </groups> 8 <classes> 9 <class name="com.autotest.testng.suite.SuiteConfig"></class> 10 <class name="com.autotest.testng.suite.LoginTest"/> 11 <class name="com.autotest.testng.MethodOnGroup"></class> 12 <class name="com.autotest.testng.ClassOnGroup3"></class> 13 <class name="com.autotest.testng.ClassOnGroup4"></class> 14 </classes> 15 </test>
5.异常测试,如@Test(expectedExceptions = RuntimeException.class)
1 import org.testng.annotations.Test; 2 3 public class ExpectedException { 4 5 @Test(expectedExceptions = RuntimeException.class) 6 public void test() { 7 System.out.println("Exception test"); 8 throw new RuntimeException(); 9 } 10 }
6.依赖测试:
方法依赖:@Test(dependsOnMethods = {"test2"}) //该方法依赖于test2方法,test2方法执行失败,则该方法无法执行
组依赖:@Test(dependsOnGroups = {"group1"}) //该方法或类依赖于group1上所有类或方法
7.xml文件参数化测试@Parameters({"name","age"})
1 public class ParameterTest { 2 3 @Test 4 @Parameters({"name","age"}) 5 public void parameterTest2(String name,int age) { 6 System.out.println("name:"+name+";age:"+age); 7 } 8 }
1 <class name="com.autotest.testng.ParameterTest"> 2 <methods> 3 <parameter name="name" value="zhangsan"></parameter> 4 <parameter name="age" value="19"></parameter> 5 <include name="parameterTest2"></include> 6 </methods> 7 </class>
8.DataProvider参数化测试@Test(dataProvider = "DataProvider")
import java.lang.reflect.Method; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderTest { @Test(dataProvider = "DataProvider") public void dataProviderTest(String name ,int age) { System.out.println("name:"+name+";age:"+age); } @DataProvider(name = "DataProvider") public Object[][] dataProvider(){ Object[][] obj =new Object[][]{ {"zhangsan",19}, {"lisi",33}, {"wangwu",32} }; return obj; } //根据不同方法提供不同数据 @DataProvider(name = "DataProviderByMethod") public Object[][] MethodDataProvider(Method method){ Object [][] obj=null; if(method.getName().equals("test1")) { obj=new Object[][] { {"lilei",33}, {"hanmeimei",22} }; } if(method.getName().equals("test2")) { obj=new Object[][] { {"David",31}, {"Green",21} }; } return obj; } @Test(dataProvider = "DataProviderByMethod") public void test1(String name,int age) { System.out.println("test1--name:"+name+";age:"+age); } @Test(dataProvider = "DataProviderByMethod") public void test2(String name,int age) { System.out.println("test2--name:"+name+";age:"+age); } }
9.多线程注解测试
1 public class MultiThreadAnnotation { 2 3 //该测试方法可在4个线程中并发执行,共被调用10次,执行超时3秒 4 @Test(invocationCount = 10,threadPoolSize = 4,timeOut = 3000) 5 public void test() { 6 System.out.println(Thread.currentThread().getName()); 7 } 8 }
xml多线程配置
1 <suite name="Suite" parallel="tests" thread-count="4"> 2 <!-- parallel="methods" thread-count="4" 为每个测试方法的执行使用单独的线程,最多并发4个线程--> 3 <!-- parallel="tests" thread-count="4" 为每个测试用例的执行使用单独的线程(该测试用例中的测试方法共享一个线程),最多并发4个线程--> 4 <!-- parallel="classes" thread-count="4" 为每个测试类的执行使用单独的线程(该测试类中的测试方法共享一个线程),最多并发4个线程。--> 5 <!-- parallel="instances" thread-count="4" 为每个测试类实例的执行始终使用唯一的线程(该测试实例中的测试方法共享一个线程),最多并发4个线程。--> 6 </suite>
parallel默认值为"none"