TestNG之注解的生命周期

发表于:2018-7-16 17:31

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:乔叶叶    来源:博客园

  有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有
  @BeforeSuite
  @AfterSuite
  @BeforeTest
  @AfterTest
  @BeforeGroups
  @AfterGroups
  @BeforeClass
  @AfterClass
  @BeforeMethod
  @AfterMethodConfiguration 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.
  英文看到不是很明白,那么我们从挨个实验。
  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 创建时间:2016年3月24日 下午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");
  }
  }
  运行后的结果:
  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
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号