软件测试实习0623-TestNG

1.介绍

       testNG,也就是testing,Next Generation,下一代测试技术,是一套根据JUnit和NUnit思想而构建的利用注释来强化测试功能的一个测试框架,可以用来做单元测试,也可以做集成测试。

         因为testNG是从junit的思想构建而来,所以testNG具备junit等所不具备的多重功能。而且testng目前的使用比较广防,google的一个delenium自动化项目组即采用的是seleniumrc的Java接口+testNG结合的方式。testNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器)。

         testNG测试过程三个典型的步骤


          编写测试的业务逻辑并在代码中插入TestNG annotation

         将测试信息添加到testng.xml文件或者build.xml
         
运行TestNG

2.对比

       a、testNG是一个设计用来简化广泛测试需求的测试框架,从单元测试到集成测试。这个是testNG设计的出发点,不仅仅是单元测试,而且可以用于集成测试。设计目标的不同,对比junit的只适合用于单元测试

         b、测试的过程的三个典型的步骤,多了一个将测试信息添加到testing.xml文件或者build.xml.测试信息尤其是测试数据不再写死在测试代码中,好处就是修改测试数据是不需要修改代码/编译了,从而有助于将测试人员引入单元测试/集成测试

         c、相比于junit的testCase/testSuit,testNG有suit/test/test method三个级别,也就是把test/test method明确分开了。

3.testNG使用的概念


suite由xml文件描述。它包含一个或多个测试并定义为<suite>标签

         test由<test>描述并包含一个或多个testNG类

         testNG类是包含至少一个 testNGannotation的Java类,由<class>标签描述并包含一个或多个测试方法

         测试方法是源文件中带有@testd注释的Java方法

4.Annotation(注释)

注解

描述

@BeforeSuite

注解的方法将只运行一次,运行此套件的所有测试前。

@AfterSuite

注解的方法将只运行一次此套件中的所有测试都运行之后。

@BeforeClass

注解的方法将只运行一次,被注释的方法将在当前类的第一个测试方法调用前运行

@AfterClass

注解的方法将只运行一次,被注释的方法将在当前类的所有测试方法调用后运行

@BeforeTest

被注释的方法将在测试运行前运行

@AfterTest

被注释的方法将在测试运行后运行

@BeforeGroups

被配置的方法将在列表中的gourp前运行。这个方法保证在第一个属于这些组的测试方法调用前立即执行。

@AfterGroups

被配置的方法将在列表中的gourp后运行。这个方法保证在最后一个属于这些组的测试方法调用后立即执行。

@BeforeMethod

被注释的方法将在每一个测试方法调用前运行

@AfterMethod

被注释的方法将在每一个测试方法调用后运行。

@DataProvider

标记一个方法用于为测试方法提供数据。
被注释的方法必须返回Object[][],其中每个Object[]可以指派为这个测试方法的参数列表。从这个DataProvider接收数据@Test方法需要使用一个和当前注释相同名称的dataProvider名称
name这个DataProvider的名称

@Factory

标记方法作为一个返回对象的工厂,这些对象将被TestNG用于作为测试类。这个方法必须返回Object[]

@Listeners

定义一个测试类的监听器。

@Parameters

描述如何传递参数给@Test方法value用于填充这个方法的参数的变量列表

@Test

标记一个类或方法作为测试的一部分。

         测试一下annotation的结果

package hello;
 
importorg.testng.annotations.Test;
importorg.testng.annotations.BeforeMethod;
importorg.testng.annotations.AfterMethod;
importorg.testng.annotations.AfterSuite;
importorg.testng.annotations.BeforeClass;
importorg.testng.annotations.AfterClass;
importorg.testng.annotations.BeforeTest;
importorg.testng.annotations.AfterTest;
importorg.testng.annotations.BeforeSuite;
 
public classTestngAnnotation {
      // test case 1
      @Test
      public void testCase1(){
           System.out.println("in test case 1");
      }
 
      // test case 2
      @Test
      public void testCase2(){
           System.out.println("in test case 2");
      }
 
      @BeforeMethod
      public voidbeforeMethod() {
           System.out.println("in beforeMethod");
      }
 
      @AfterMethod
      public voidafterMethod() {
           System.out.println("in afterMethod");
      }
 
      @BeforeClass
      public voidbeforeClass() {
           System.out.println("in beforeClass");
      }
 
      @AfterClass
      public voidafterClass() {
           System.out.println("in afterClass");
      }
 
      @BeforeTest
      public voidbeforeTest() {
           System.out.println("in beforeTest");
      }
 
      @AfterTest
      public void afterTest(){
           System.out.println("in afterTest");
      }
 
      @BeforeSuite
      public voidbeforeSuite() {
           System.out.println("in beforeSuite");
      }
 
      @AfterSuite
      public voidafterSuite() {
           System.out.println("in afterSuite");
      }
 
}

      根据上面的输出,testNG的annotation的执行过程如下:

    首先所有beforeSuite()方法只执行一次

    最后,afterSuite()方法只执行一次


最后,afterSuite()方法只执行一次

    即使方法beforeTest(), beforeClass(), afterClass() 和afterTest()方法只执行一次

    beforeMethod()方法执行每个测试用例,但在此之前执行的测试用例。

     afterMethod()方法执行每个测试用例,但测试用例执行后

     测试用例在beforeMethod()和afterMethod()之间执行

5.编写测试

创建EmployeeDetails.java

EmployeeDetails类是用来

--get/set员工名字的值

--get/set员工月薪的值

--get/set员工年龄的值

packagehello;
public classEmployeeDetails {
         private String name;
         private double monthlySalary;
         private int age;
         /**
         * @return the name
         */
         public String getName() {
            return name;
         }
         /**
         * @param name thename to set
         */
         public void setName(String name) {
            this.name =name;
         }
         /**
         * @return themonthlySalary
         */
         public double getMonthlySalary() {
            return monthlySalary;
         }
         /**
         * @parammonthlySalary the monthlySalary to set
         */
         public void setMonthlySalary(doublemonthlySalary) {
            this.monthlySalary =monthlySalary;
         }
         /**
         * @return the age
         */
         public int getAge() {
            return age;
         }
         /**
         * @param age theage to set
         */
         public void setAge(intage) {
         this.age =age;
         }
      }
创建一个EmpBusinessLogic.java
EmpBusinessLogic类用于计算
员工的年薪
考核支付雇员
packagehello;
 
public classEmpBusinessLogic {
         //Calculate the yearly salary of employee
         public double calculateYearlySalary(EmployeeDetails employeeDetails){
            double yearlySalary=0;
            yearlySalary = employeeDetails.getMonthlySalary() * 12;
            return yearlySalary;
         }
          
         //Calculate the appraisal amount of employee
         public double calculateAppraisal(EmployeeDetails employeeDetails){
            double appraisal=0;
            if(employeeDetails.getMonthlySalary() < 10000){
               appraisal = 500;
            }else{
               appraisal = 1000;
            }
            return appraisal;
         }
}

现在,让我们创建一个TestNG 类名称为TestEmployeeDetails.java.TestNG类是一个Java类,它包含至少一个TestNG的注解。 这个类包含测试用例进行测试。可以配置,@BeforeXXX和@AfterXXX注解了TestNG测试,它允许执行一些Java逻辑的目标点之前和之后。

TestEmployeeDetails测试方法,用于类EmpBusinessLogic,它

雇员测试的年薪

测试评估员工的金额

packagehello;
 
importorg.testng.Assert;
importorg.testng.annotations.Test;
 
 
public classTestEmployeeDetails {
      EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
      EmployeeDetails employee = newEmployeeDetails();
  @Test
  public voidtestcalCulateAppraisal() {
        employee.setName("Rajeev");
        employee.setAge(25);
        employee.setMonthlySalary(8000);
        double appraisal = empBusinessLogic.calculateAppraisal(employee);
        Assert.assertEquals(500, appraisal,0.0,"8000");
  }
  @Test
  public voidtestCalculateYearSalary(){
        employee.setName("Rajeev");
        employee.setAge(25);
        employee.setMonthlySalary(8000);
        double salary = empBusinessLogic.calculateYearlySalary(employee);
        Assert.assertEquals(96000, salary,0.0,"8000");
       
  }
}



同样我们也可以写testng.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM"http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
  <test name="test1">
    <classes>
       <classname="hello.TestEmployeeDetails"/>
    </classes>
  </test>
</suite>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值