TestNG - Writing Tests

在TestNG中编写测试基本上涉及以下步骤 :

  • 编写测试的业务逻辑并插入代码中的TestNG注释.

  • 在testng中添加有关测试的信息(例如,类名,要运行的组等). xml文件或在build.xml中.

  • 运行TestNG.

在这里,我们将看到一个完整的TestNG测试示例,它使用POJO类,业务逻辑类和测试xml,它们将由TestNG运行.

创建 EmployeeDetails.java C:\> TestNG_WORKSPACE 的,这是一个POJO类

public class EmployeeDetails {

   private String name;
   private double monthlySalary;
   private int age;
   
   // @return the name

   public String getName() {
      return name;
   }
   
   // @param name the name to set
   
   public void setName(String name) {
      this.name = name;
   }
   
   // @return the monthlySalary

   public double getMonthlySalary() {
      return monthlySalary;
   }
   
   // @param monthlySalary the monthlySalary to set
   
   public void setMonthlySalary(double monthlySalary) {
      this.monthlySalary = monthlySalary;
   }
   
   // @return the age
 
   public int getAge() {
      return age;
   }
   
   // @param age the age to set
 
   public void setAge(int age) {
      this.age = age;
   }
}

mployeeDetails类用于:

  • 获取/设置员工姓名的值。

  • 获取/设置员工月薪的值。

  • 获取/设置员工年龄的值。

在C:\> TestNG_WORKSPACE中创建一个EmpBusinessLogic.java,其中包含业务逻辑。

public class EmpBusinessLogic {

   // 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;
   }
}

EmpBusinessLogic类用于计算:

  • 员工的年薪。

  • 员工的评估金额。

现在,让我们在C:\> TestNG_WORKSPACE中创建一个名为TestEmployeeDetails.java的TestNG类。 TestNG类是Java类,其中包含至少一个TestNG批注。 此类包含要测试的测试用例。 可以通过@BeforeXXX和@AfterXXX批注来配置TestNG测试(我们将在TestNG-执行过程一章中看到),它允许在特定点之前和之后执行一些Java逻辑。

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestEmployeeDetails {
   EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
   EmployeeDetails employee = new EmployeeDetails();

   @Test
   public void testCalculateAppriasal() {
   
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
      
      double appraisal = empBusinessLogic.calculateAppraisal(employee);
      Assert.assertEquals(500, appraisal, 0.0, "500");
   }

   // Test to check yearly salary
   @Test
   public void testCalculateYearlySalary() {
   
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
      
      double salary = empBusinessLogic.calculateYearlySalary(employee);
      Assert.assertEquals(96000, salary, 0.0, "8000");
   }
}

TestEmployeeDetails类用于测试EmpBusinessLogic类的方法。 它执行以下操作:

  • 测试员工的年薪。

  • 测试员工的评估金额。

在运行测试之前,必须使用特殊的XML文件(通常名为testng.xml)配置TestNG。 该文件的语法非常简单,其内容如下所示。 在C:\>TestNG_WORKSPACE中创建此文件。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "TestEmployeeDetails"/>
      </classes>
   </test>
</suite>

以上文件的详细内容如下:

  • 套件由一个XML文件表示。 它可以包含一个或多个测试,并由<suite>标记定义。

  • 标记<test>表示一个测试,并且可以包含一个或多个TestNG类。

  • <class>标签代表一个TestNG类。 它是一个Java类,至少包含一个TestNG批注。 它可以包含一种或多种测试方法。

使用javac编译测试用例类。

C:\TestNG_WORKSPACE>javac EmployeeDetails.java EmpBusinessLogic.java TestEmployeeDetails.java

现在用以下命令TestNG:

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

如果所有步骤都正确完成,则应该在控制台中查看测试结果。 此外,TestNG在名为test-output的文件夹中创建了一个非常漂亮的HTML报告,该文件夹在当前目录中自动创建。 如果您打开它并加载index.html,您将看到与下图类似的页面:

Writing Tests