TestNG的基本注解:
@Test:最基本的注解,用来把方法标记为测试的一部分
@BeforeSuite:对于套件测试,在此套件中的所有测试执行之前运行,仅运行一次
@AfterSuite:对于套件测试,在此套件中的所有测试执行之后运行,仅运行一次
@BeforeTest:对于套件测试,在属于<test>标签内的所有类的测试方法执行之前运行
@AfterTest:对于套件测试,在属于<test>标签内的所有类的测试方法都已执行完之后运行
@BeforeClass:在调用当前类之前运行
@AfterClass:在调用当前类之后运行
@BeforeMethod:在每个测试方法执行之前都会运行
@AfterMethod:在每个测试方法执行之后都会运行
@BeforeGroups:在调用属于该组的第一个测试方法之前运行
@AfterGroups:在调用属于该组的最后一个测试方法执行之后运行
英文解释:
@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
@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
@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
举例:
import org.testng.annotations.*; public class AnnotationsTest { //test 这是最基本的注解,可以放在方法或类的前面 @Test public void test1Demo() { System.out.println("这是测试方法1"); } @Test public void test2Demo() { System.out.println("这是测试方法2"); } @BeforeTest public void beforeTest() { System.out.println("beforeTest运行!!!"); } @AfterTest public void afterTest() { System.out.println("afterTest运行!!!"); } @BeforeSuite public void beforeSuite() { System.out.println("beforeSuite运行!!!"); } @AfterSuite public void afterSuite() { System.out.println("afterSuite运行!!!"); } @BeforeMethod public void beforeMethod() { System.out.println("beforeMethod运行!!!"); } @AfterMethod public void afterMethod() { System.out.println("afterMethod运行!!!"); } @BeforeClass public void beforeClass() { System.out.println("beforeClass运行!!!"); } @AfterClass public void afterClass() { System.out.println("afterClass运行!!!"); } }
运行结果:
beforeSuite运行!!!
beforeTest运行!!!
beforeClass运行!!!
beforeMethod运行!!!
这是测试方法1
afterMethod运行!!!
beforeMethod运行!!!
这是测试方法2
afterMethod运行!!!
afterClass运行!!!
afterTest运行!!!
afterSuite运行!!!
由此可见,testng运行时,顺序是这样的:
@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite
其中{}内的有多少个@Test,就循环执行多少次。
我们知道了在一个类中注解的生命周期,那么这些注解的作用范围呢?
我们新建下面两个类来测试:
package com.janson; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; /** * @author janson * @version 创建时间:2018年11月22日 下午9:21:00 * 类说明 */ public class TestNG2 { @BeforeSuite public void beforesuite() { System.out.println("beforesuite"); } @AfterSuite public void aftersuite() { System.out.println("aftersuite"); } @BeforeTest public void beforetest() { System.out.println("beforeTest"); } @AfterTest public void AfterTest() { System.out.println("aftertest"); } @BeforeClass public void beforeclass() { System.out.println("beforeclass's TestNG2"); } @AfterClass public void aftertclass() { System.out.println("afterclass's TestNG2"); } @BeforeMethod public void beforemethod() { System.out.println("TestNG2's beforemethod"); } @AfterMethod public void aftertmethod() { System.out.println("TestNG2's aftermethod"); } @BeforeGroups public void beforegroups() { System.out.println("TestNG2's beforegroups"); } @AfterGroups public void aftergroups() { System.out.println("TestNG2's aftergroups"); } @Test public void test1() { System.out.println("TestNG2's testt1"); } @Test(groups="gr") public void test2() { System.out.println("TestNG2's testt2"); } public void ff() { System.out.println("nothing"); } }
package com.janson; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; /** * @author janson * @version 创建时间:2018年11月22日 下午9:20:47 * 类说明 */ public class TestNG1 { @BeforeClass public void beforeclass() { System.out.println("beforeclass's TestNG1"); } @AfterClass public void afterclass() { System.out.println("afterclass's TestNG1"); } @Test public void test3() { System.out.println("TestNG1's test3"); } @Test(groups="haha") public void test4() { System.out.println("TestNG1's test4"); } }
配置xml:
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="Test"> <classes> <class name="com.janson.TestNG1"/> <class name="com.janson.TestNG2"/> </classes> </test> </suite>
运行结果:
beforesuite beforeTest beforeclass's TestNG1 TestNG1's test3 TestNG1's test4 afterclass's TestNG1 beforeclass's TestNG2 TestNG2's beforemethod TestNG2's testt1 TestNG2's aftermethod TestNG2's beforemethod TestNG2's testt2 TestNG2's aftermethod afterclass's TestNG2 aftertest aftersuite
所以,除了@BeforeSuite、@BeforeTest、@AfterTest、@AfterSuite可以对不同的测试类生效外,其他的注解的作用范围只在本类中生效。
这样就可以清晰的知道什么样的逻辑应该放在哪个注解中,如只想在测试中只启动、关闭一次浏览器,且在不同的测试类中共用,那么我们就可以把启动、关闭浏览器的方法放在suite和test中