TestNG是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有如下特性:html
那么好的测试框架,怎么使用?多线程
这里咱们使用eclipse插件方式 安装详见:http://testng.org/doc/eclipse.html框架
首先了解一下testng 的annotationseclipse
常见的有如下:ide
@BeforeClass: 该annotation在class激活以前执行测试
@BeforeMethod: 该annotation会在每一个执行的方法以前执行ui
@Test ,该annotation 是你要执行测试的方法this
@AfterMethod,该annotation在每一个测试方法执行以后运行spa
@AfterClass 该annotation会在全部测试方法以后运行插件
具体生命周期以下图:
这里是全部的annotation
@BeforeSuite @AfterSuite @BeforeTest @AfterTest @BeforeGroups @AfterGroups @BeforeClass @AfterClass @BeforeMethod @AfterMethod |
Configuration information for a TestNG class: @BeforeSuite: The annotated method will be run before all tests in this suite have run. @AfterSuite: The annotated method will be run after all tests in this suite have run. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod: The annotated method will be run before each test method. @AfterMethod: The annotated method will be run after each test method. |
实例:咱们验证一下testng annotation 执行顺序,这个case里有两个 测试 ,执行顺序为 beforeClass->beforeMethod->test1->afterMethod->beforeMethod->
test2->afterMethod->afterClass.
package com.dbyl.tests; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** * This is to verify testng annotation execute * @author Young * */ public class TestngExample { private int a; @BeforeMethod(alwaysRun=true) public void beforeMethod() { a = 2; System.out.println("This is beforeMethod method. The Value of a is: " + a); } @BeforeClass public void beforeClass() { a = 1; System.out.println("This is beforeClass method .The Value of a is: " + a); } @Test(groups = "TestngExample") public void testExample1() { a = 3; System.out.println("This is Test method1 .The Value of a is: " + a); } @Test(groups = "TestngExample") public void testExample2() { a = 4; System.out.println("This is Test method2 .The Value of a is: " + a); } @AfterClass public void afterClass() { a = 5; System.out.println("This is AfterClass Method .The Value of a is: " + a); } @AfterMethod public void afterMethod() { a = 6; System.out.println("This is AfterMethod Method .The Value of a is: " + a); } }
因此执行结果为:
1
2
3
4
5
6
7
8
9
10
|
This is beforeClass method .The Value of a is: 1
This is beforeMethod method. The Value of a is: 2
This is Test method1 .The Value of a is: 3
This is AfterMethod Method .The Value of a is: 6
This is beforeMethod method. The Value of a is: 2
This is Test method2 .The Value of a is: 4
This is AfterMethod Method .The Value of a is: 6
This is AfterClass Method .The Value of a is: 5
PASSED: testExample1
PASSED: testExample2
|
固然,还有BeforeSuite 等,再也不作深刻研究.
annotation后面能够加一些参数,好比alwaysRun=true/false,dependsOnMethods=""
alwaysRun控制是否每次都执行,dependsOnMethods是一种依赖,依赖某个方法执行
以前有提到testng数据驱动,使用dataProvider,dataProvider能够导入text ,excel等数据,
执行case时会从DataProvider依次拿出数据执行,同一个测试方法,会被执行相应的次数
package com.dbyl.tests; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class Case1 { @DataProvider public Object[][] testData1() { return new Object[][] { { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 }, { -1, 3, 2 } }; } @DataProvider public Object[][] testData2() { return new Object[][] { { 5, 2, 3 }, { 1, 2, 4 }, { 1, -3, 4 }, { 6, 3, 2 } }; } public static int add(int a, int b) { return a + b; } public static int minus(int a, int b) { return a - b; } @BeforeClass public void beforeClass() { System.out.println("This is Before Class"); } @Test(groups = { "add" }, dataProvider = "testData1") public void addTest(int a, int b, int c) { System.out.println("This is test add method. "+a+" + "+ b+" = "+c); Assert.assertEquals(add(a, b), c); } @Test(groups = { "minus" }, dataProvider = "testData2") public void minusTest(int a, int b, int c) { System.out.println("This is test minus method. "+a+" - "+ b+" = "+c); Assert.assertEquals(minus(a, b), c); } @BeforeMethod public void beforeMethod() { System.out.println("This is Before Method"); } @AfterMethod public void afterMethod() { System.out.println("This is After Method"); } @AfterClass public void afterClass() { System.out.println("This is After Class"); } }
执行结果以下: