加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 智能机器人、大数据、CDN、图像分析、语音技术!
当前位置: 首页 > 教程 > 正文

TestNG使用汇总

发布时间:2023-10-28 11:00:26 所属栏目:教程 来源:互联网
导读:    TestNG是诞生于JUnit3和JUnit4之间的一个测试框架,虽然在JUnit4之前诞生,但被认为更先进,NG是Next Generation的意思。TestNG拥有诸多新特性,以及消除了JUnit中的许多限制,更适合
   TestNG是诞生于JUnit3和JUnit4之间的一个测试框架,虽然在JUnit4之前诞生,但被认为更先进,NG是Next Generation的意思。TestNG拥有诸多新特性,以及消除了JUnit中的许多限制,更适合于大型项目的测试。
 
  常用注解:
 
  @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.
 
  @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.
 
  @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.
 
  @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.
 
  @Test A Test Case
 
  注解使用示例:
 
  public class OrderTest {
 
      @BeforeSuite
 
      public void testBeforeSuite(){System.out.println("BeforeSuite");}
 
      @AfterSuite
 
      public void testAfterSuite(){System.out.println("AfterSuite");}
 
      @BeforeTest
 
      public void testBeforeTest(){System.out.println("BeforeTest");}
 
      @AfterTest
 
      public void testAfterTest(){System.out.println("AfterTest");}
 
      @BeforeClass
 
      public void testBeforeClass(){System.out.println("BeforeClass");}
 
      @AfterClass
 
      public void testAfterClass(){System.out.println("AfterClass");}
 
      @BeforeGroups(groups = "data")
 
      public void testBeforeGroups(){System.out.println("BeforeGroups");}
 
      @AfterGroups(groups = "data")
 
      public void testAfterGroups(){System.out.println("AfterGroups");}
 
      @BeforeMethod
 
      public void testBeforeMethod(){System.out.println("BeforeMethod");}
 
      @AfterMethod
 
      public void testAfterMethod(){System.out.println("AfterMethod");}
 
      @Test(groups = "data")
 
      public void testGroup1(){System.out.println("1、Belong to group data");}
 
      @Test(groups = "data")
 
      public void testGroup2(){System.out.println("2、Belong to group data");}
 
  }
 
  执行顺序:
 
      BeforeSuite
 
      BeforeTest
 
      BeforeClass
 
      BeforeGroups
 
      BeforeMethod
 
      1、Belong to group data
 
      AfterMethod
 
      BeforeMethod
 
      2、Belong to group data
 
      AfterMethod
 
      AfterGroups
 
      AfterClass
 
      AfterTest
 
      AfterSuite
 
      
 
  备注:
 
      其中BeforeGroups和AfterGroups会在指定组的所有方法执行之前和之后执行。
 
  复制代码
 
  超时测试:
 
  @Test(timeOut = 1000)
 
  忽略测试:
 
  @Test(enabled = false)
 
  异常测试:
 
  @Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
 
  用例分组:
 
  @Test(groups = {"group_one", "group_two"})
 
  依赖测试:
 
  @Test
 
  public void testOne(){}
 
  @Test(dependsOnMethods = "testOne")
 
  public void testTwo(){}
 
  多次执行以及使用线程池:
 
  @Test(invocationCount = 5, threadPoolSize = 5)
 
  用例执行5次,使用5个线程并行执行
 
  套件测试
 
  需要借助XML文件,运行时需要指定XML才能正确执行
 
  //测试整个NewTest以及OldTest中的testThird方法
 
  <suite name="Method Suit" verbose="1">
 
      <test name="includedGroupsInAPackage">
 
          <classes>
 
              <class name="com.me.hello.NewTest"/>
 
              <class name="com.me.hello.OldTest">
 
                  <methods>
 
                      <include name="testThird"/>
 
                  </methods>
 
              </class>
 
          </classes>
 
      </test>
 
  </suite>
 
  //测试指定两个package里的所有测试类
 
  <test name="tttt">
 
  <packages>
 
      <package name="com.me.hello"/>
 
      <package name="com.me.chat"/>
 
  </packages>
 
  </test>
 
          
 
  //指定package中的分组测试,以及指定class测试。
 
  <suite name="Group Suit" verbose="10">
 
  <test name="tttt">
 
      <groups>
 
          <run>
 
              <include name="slow"/>//分组测试
 
              <include name="fast"/>
 
          </run>
 
      </groups>
 
      <packages>
 
          <!--<package name="com.me.good"/>-->
 
          <!--<package name="com.me.hello"/>-->
 
          <package name="com.me.*"/>
 
      </packages>
 
  </test>
 
  <test name="next test">
 
      <classes>
 
          <class name="com.me.hello.NewTest"/>//指定类
 
      </classes>
 
  </test>
 
  </suite>
 
  备注:
 
     IDEA的套件测试需要在Run-Edit Configurations中配置运行条件以及指定xml文件,或者直接命令行执行。
 
  复制代码
 
  参数化测试:
 
  1、通过xml文件(基本数据类型)
 
  2、代码中使用@DataProvider(复杂数据类型)
 
  @DataProvider(name = "getData")
 
  public Object[][] getDataFromFile(){
 
      return new Object[][]{{1,2},{3,4},{5,6}};
 
  }
 
  @Test(dataProvider = "getData")
 
  public void testCalc(int a, int e){
 
      Assert.assertEquals(a+1, e);
 
  }
 
  1、声明一个方法给定名字,返回数组数据。
 
  2、Test方法根据DataProvider方法返回的数据,设定参数列表。
 
  3、按照参数列表顺序操作参数。
 
  复制代码
 
  断言
 
         TestNG的断言总体和JUnit相似,大体上区别只是把提示消息位置放在后面而已。
 
  总结:
 
        相比JUnit,TestNG的参数化测试,依赖测试,分组测试都更方便使用。另外套件测试使用XML和代码实现数据和测试逻辑的分离,并且方便定制分类逻辑更复杂的测试。
 
 

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章