TestNG入门——注解之Before/After

摘要:
Annotation是java5的一个新函数。它可以在类、方法、变量中使用,testNG包提供的注释函数如下表1所示BeforeSuiteor@AfterSuite注释方法将在整个测试套件之前或之后执行。

注解是java 5新增的功能,可使用于类,方法,变量,testNG包提供的注解功能请见下表

1、@BeforeSuite or @AfterSuite  被注解的方法,将在整个测试套件之前 or 之后执行。

2、@BeforeTest or @AfterTest 被注解的方法,将在测试套件内所有用例执行之前 or 之后执行。

3、@BeforeGroups or @AfterGroups 被注解的方法,将在指定组内任意用例执行之前 or 之后执行。

4、@BeforeClass or @AfterClass 被注解的方法,将在此方法对应的类中的任意其他的,被标注为@Test 的方法执行前 or 执行后执行。

5、@BeforeMethod or@AfterMethod 被注解的方法,将在此方法对应的类中的任意其他的,被标注为@Test的方法执行前 or 执行后执行

6、@DataProvider 被注解的方法,强制返回一个 二维数组Object[ ][ ]作为另外一个@Test方法的数据工厂

7、@Factory 被注解的方法,作为对象工厂,强制返回一个对象数组 Object[ ]

8、@Listeners 定义一个测试类的监听器

9、@Parameters 定义一组参数,在方法运行期间向方法传递参数的值,参数的值在testng.xml中定义

10、@Test  标记方法为测试方法,如果标记的是类,则此类中所有的public方法都为测试方法

首先来看看以下几个注解再testNG中的使用方法与不同点
@BeforeSuite / @AfterSuite
@BeforeTest / @AfterTest
@BeforeGroups / @AfterGroups
@BeforeClass / @AfterClass
@BeforeMethod / @AfterMethod
创建一个java项目,项目结构如下:

TestNG入门——注解之Before/After第1张

TestClass.java文件中增加如下代码

  1.  
    package test.beforafter;
  2.  
    import org.testng.annotations.AfterClass;
  3.  
    import org.testng.annotations.AfterTest;
  4.  
    import org.testng.annotations.AfterGroups;
  5.  
    import org.testng.annotations.AfterSuite;
  6.  
    import org.testng.annotations.AfterMethod;
  7.  
    import org.testng.annotations.BeforeClass;
  8.  
    import org.testng.annotations.BeforeTest;
  9.  
    import org.testng.annotations.BeforeGroups;
  10.  
    import org.testng.annotations.BeforeSuite;
  11.  
    import org.testng.annotations.BeforeMethod;
  12.  
    import org.testng.annotations.Test;
  13.  
     
  14.  
     
  15.  
    public class TestClass {
  16.  
     
  17.  
     
  18.  
    @BeforeSuite
  19.  
    public void beforeSuite(){
  20.  
    System.out.println("Before Suite Method");
  21.  
    }
  22.  
     
  23.  
    @AfterSuite
  24.  
    public void afterSuite(){
  25.  
    System.out.println("After Suite Method");
  26.  
    }
  27.  
     
  28.  
    @BeforeTest
  29.  
    public void beforeTest(){
  30.  
    System.out.println("Before Test Method");
  31.  
    }
  32.  
     
  33.  
    @AfterTest
  34.  
    public void afterTest(){
  35.  
    System.out.println("After Test Method");
  36.  
    }
  37.  
     
  38.  
    @BeforeClass
  39.  
    public void beforeClass(){
  40.  
    System.out.println("Before Class Method");
  41.  
    }
  42.  
     
  43.  
    @AfterClass
  44.  
    public void afterClass(){
  45.  
    System.out.println("After Class Method");
  46.  
    }
  47.  
     
  48.  
    @BeforeGroups(groups={"testOne"})
  49.  
    public void beforeGroupOne(){
  50.  
    System.out.println("Before Group testOne Method");
  51.  
    }
  52.  
     
  53.  
    @AfterGroups(groups={"testOne"})
  54.  
    public void afterGroupOne(){
  55.  
    System.out.println("After group testOne Method");
  56.  
    }
  57.  
     
  58.  
    @BeforeGroups(groups={"testTwo"})
  59.  
    public void beforeGroupTwo(){
  60.  
    System.out.println("Before Group testTwo Method");
  61.  
    }
  62.  
     
  63.  
    @AfterGroups(groups={"testTwo"})
  64.  
    public void afterGroupTwo(){
  65.  
    System.out.println("After Group testTwo Method");
  66.  
    }
  67.  
    @BeforeMethod
  68.  
    public void beforeMethod(){
  69.  
    System.out.println("Before Method");
  70.  
    }
  71.  
     
  72.  
    @AfterMethod
  73.  
    public void afterMethod(){
  74.  
    System.out.println("After Method");
  75.  
    }
  76.  
     
  77.  
    @Test(groups={"testOne"})
  78.  
    public void testOne(){
  79.  
    System.out.print("Test One Method");
  80.  
    }
  81.  
     
  82.  
    @Test(groups={"testTwo"})
  83.  
    public void testTwo(){
  84.  
     
  85.  
    System.out.print("Test Two Method");
  86.  
    }
  87.  
     
  88.  
    @Test
  89.  
    public void testThree(){
  90.  
     
  91.  
    System.out.println("Test Third Method");
  92.  
    }
  93.  
     
  94.  
     
  95.  
    }


alltestng.xml文件中增加如下配置

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!-- 只执行某个包 -->
  3.  
    <suite name="Include Package Suite" verbose="1">
  4.  
    <test name="Include Package Test">
  5.  
    <packages>
  6.  
    <package name="test.*">
  7.  
    <include name="test.beforafter" />
  8.  
    </package>
  9.  
    </packages>
  10.  
    </test>
  11.  
    </suite>

执行后的顺序如下:

Before Suite Method
Before Test Method
Before Class Method
Before Group testOne Method
Before Method
Test One Method
After Method
After group testOne Method
Before Method
Test Third Method
After Method
Before Group testTwo Method
Before Method
Test Two Method
After Method
After Group testTwo Method
After Class Method
After Test Method
After Suite Method

免责声明:文章转载自《TestNG入门——注解之Before/After》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇asp.net 自定义控件Mongodb无法访问28107的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

一、Cucumber 环境配置

Cucumber是一个BDD(行为驱动开发)比较火的框架 地址:https://cucumber.io/docs/installation/java/ Maven地址,需要引入2个包: <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java --> <...

这四种对象属性拷贝方式,你都知道吗?

当get/set太繁琐时;当BeanUtils无法拷贝集合时;当。。。可能,你需要好好看看这篇文章,文末附完整示例代码。 在做业务的时候,为了隔离变化,我们会将DAO查询出来的DO和对前端提供的DTO隔离开来。大概90%的时候,它们的结构都是类似的;但是我们很不喜欢写很多冗长的b.setF1(a.getF1())这样的代码,于是我们需要简化对象拷贝方式。...

awk 调用 shell 命令,并传递参数

from:awk 调用 shell 命令的两种方法:system 与 print shell 向awk传递命令,这样使用即可: awk -v  ...  但反过来呢?awk调用外部命令,同时也传参呢?  awk 中使用的 shell 命令,有 2 种方法:一。使用所以 system()awk 程序中我们可以使用 system() 函数去调用 shell...

维护Panel的滚动条ScrollBars位置(C#)

我们常将内容放在Panel中,例如文章,GridView控件等....。当内容超出Panel的高、宽时,可能就需要滚动条来进行控制。 当点击页面中按钮产生PostBack时,滚动条总是会回到最上面的位置,我们现在要解决的主要就是这个问题。 ScrollBar的可选项有:  成员名称 说明   Auto 根据需要,可显示水平滚动条、垂直滚动条或这两种滚动条。...

C# 编写Windows Service(windows服务程序)(摘抄)

  C# 编写Windows Service(windows服务程序)   Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两...

多附件的上传

实现多附件的上传,俗话说:语言不如文字,文字不如图形。先传个页面看看 可以点击“添加”按钮任意添加上传文件的个数,也可以通过“取消”按钮取消多个文件的上传。这样可以一次上传N个附件,而不需要每上传一个附件点击一次上传按钮。达到了一次上传,一次成功的效果。1、JS代码  1function addFileControl() 2         { 3   ...