zoukankan      html  css  js  c++  java
  • TestNG 随笔

     背景

    TestNG是一类用于做测试的框架。由JUnit和NUnit启发而来,同时又引入了一些新的功能,可以使得TestNG在容易上手的同时,更加高效的进行开发。引入的新功能有:

    • 注解
    • 所有的测试都运行在一个有很多策略可以配置的线程池中(所有的方法在他们独自的线程中,每个测试类一个线程等)
    • 多线程安全
    • 丰富的测试配置
    • 支持数据驱动测试(@DataProvider)
    • 支持参数化测试
    • Powerful execution model (no more TestSuite).
    • 多工具以及插件支持
    • 使嵌入式BeanShell更加灵活
    • 支持依赖测试方法,并行测试,负载测试,局部故障
    • 独立的编译时测试代码和运行时配置/数据信息

    Concepts in TestNG

    • suite由xml文件描述,它包含一个或多个测试并被定义为<suite>标签。
    • test由<test>描述并包含一个或者多个TestNG类
    • TestNG类是包含至少一个TestNG annotation的java类,由<class>标签描述并包含一个或多个测试方法(test method)
    • 测试方法(test method)是源文件中带有@Test注释的java方法

    Annotations

    Here is a quick overview of the annotations available in TestNG along with their attributes.

    @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.

    Behaviour of annotations in superclass of a TestNG class

    The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.

    In that case, TestNG guarantees that the "@Before" methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the "@After" methods in reverse order (going up the inheritance chain).

      alwaysRun For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to. 
    For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
      dependsOnGroups The list of groups this method depends on.
      dependsOnMethods The list of methods this method depends on.
      enabled Whether methods on this class/method are enabled.
      groups The list of groups this class/method belongs to.
      inheritGroups If true, this method will belong to groups specified in the @Test annotation at the class level.
     
    @DataProvider Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
      name The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
      parallel If set to true, tests generated using this data provider are run in parallel. Default value is false.
     
    @Factory Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
     
    @Listeners Defines listeners on a test class.
      value An array of classes that extend org.testng.ITestNGListener.
     
    @Parameters Describes how to pass parameters to a @Test method.
      value The list of variables used to fill the parameters of this method.
     
    @Test Marks a class or a method as part of the test.
      alwaysRun If set to true, this test method will always be run even if it depends on a method that failed.
      dataProvider The name of the data provider for this test method.
      dataProviderClass The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
      dependsOnGroups The list of groups this method depends on.
      dependsOnMethods The list of methods this method depends on.
      description The description for this method.
      enabled Whether methods on this class/method are enabled.
      expectedExceptions The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
      groups The list of groups this class/method belongs to.
      invocationCount The number of times this method should be invoked.
      invocationTimeOut The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
      priority The priority for this test method. Lower priorities will be scheduled first.
      successPercentage The percentage of success expected from this method
      singleThreaded If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods". This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
      timeOut The maximum number of milliseconds this test should take.
      threadPoolSize The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount. 
    Note: this attribute is ignored if invocationCount is not specified
  • 相关阅读:
    使用 git 提交报错:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large 的解决办法
    vue3使用 svg, 不是 svg 图标,是 svg 大图片。可动态修改参数
    Unix信号列表
    Linux CentOS系统安装 node 版本管理工具 nvm
    《能源监测与评价》——产品能耗的节能监测
    脚本
    Parallels Desktop 安装 Centos 虚拟机
    Nuxt3 学习笔记
    电力行业中的一些基本概念
    管理成熟度和管理者成熟度
  • 原文地址:https://www.cnblogs.com/huangshijie/p/8028682.html
Copyright © 2011-2022 走看看