TestNG之注解的生命周期
有必要介紹一下TestNG注解的生命周期,先看一下官網(wǎng)支持的注解有?
| @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. | |
英文看到不是很明白,那么我們從挨個(gè)實(shí)驗(yàn)。
package com.test;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 QiaoJiafei * @version 創(chuàng)建時(shí)間:2016年3月24日 下午9:21:00 * 類說(shuō)明 */ public class TestNG2 {@BeforeSuitepublic void beforesuite() {System.out.println("beforesuite");}@AfterSuitepublic void aftersuite() {System.out.println("aftersuite");}@BeforeTestpublic void beforetest() {System.out.println("beforeTest");}@AfterTestpublic void AfterTest() {System.out.println("aftertest");}@BeforeClasspublic void beforeclass() {System.out.println("beforeclass's TestNG2");}@AfterClasspublic void aftertclass() {System.out.println("afterclass's TestNG2");}@BeforeMethodpublic void beforemethod() {System.out.println("TestNG2's beforemethod");}@AfterMethodpublic void aftertmethod() {System.out.println("TestNG2's aftermethod");}@BeforeGroupspublic void beforegroups() {System.out.println("TestNG2's beforegroups");}@AfterGroupspublic void aftergroups() {System.out.println("TestNG2's aftergroups");}@Testpublic 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");} }運(yùn)行后的結(jié)果:
beforesuite beforeTest 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由此可見(jiàn),testng運(yùn)行時(shí),順序是這樣的:
@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite
其中{}內(nèi)的與多少個(gè)@Test,就循環(huán)執(zhí)行多少次。
我們知道了在一個(gè)類中注解的生命周期,那么這些注解的作用范圍呢,下面我們?cè)俳ㄒ粋€(gè)類
package com.test;import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;/** * @author QiaoJiafei * @version 創(chuàng)建時(shí)間:2016年3月24日 下午9:20:47 * 類說(shuō)明 */ public class TestNG1 {@BeforeClasspublic void beforeclass() {System.out.println("beforeclass's TestNG1");}@AfterClasspublic void afterclass() {System.out.println("afterclass's TestNG1");}@Testpublic 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.test.TestNG1"/><class name="com.test.TestNG2"/></classes><!-- <groups><run><include name="gr" /></run></groups>--></test> <!-- Test --> </suite> <!-- Suite -->運(yùn)行的結(jié)果是:
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看到?jīng)]有,除了@BeforeSuite、@BeforeTest、@AfterTest、@AfterSuite可以對(duì)不同的測(cè)試類生效外,其他的注解的作用范圍只在本類中生效。這樣就可以清晰的知道什么樣的邏輯應(yīng)該放在哪個(gè)注解中,如只想在測(cè)試中只啟動(dòng)、關(guān)閉一次瀏覽器,且再不同的測(cè)試類中共用,那么我們就可以把啟動(dòng)、關(guān)閉瀏覽器的方法放在suite和test中
至于@BeforeGroups和@AfterGroups筆者目前還沒(méi)有發(fā)現(xiàn)怎么生效。
畫(huà)了個(gè)路程圖更直接點(diǎn)。
總結(jié)
以上是生活随笔為你收集整理的TestNG之注解的生命周期的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 更改centos 7 的默认启动为命令界
- 下一篇: 从 github 执行 git clon