- TestNG 教程
- TestNG - 首页
- TestNG - 概述
- TestNG - 环境
- TestNG -编写测试
- TestNG - 基本注解
- TestNG - 执行流程
- TestNG - 执行测试
- TestNG - 套件测试
- TestNG - 忽略测试
- TestNG - 组测试
- TestNG - 异常测试
- TestNG - 依赖测试
- TestNG - 参数化测试
- TestNG - 运行 JUnit 测试
- TestNG - 测试结果
- TestNG - 注解转换器
- TestNG - 断言
- TestNG - 并行执行
- TestNG - 与 ANT 集成
- TestNG - 与 Eclipse 集成
- TestNG - TestNG 与 JUnit 的比较
- TestNG 有用资源
- TestNG - 快速指南
- TestNG - 有用资源
- TestNG - 讨论
TestNG -编写测试
在 TestNG 中编写测试基本上包括以下步骤:
编写测试的业务逻辑并在代码中插入 TestNG 注解。
在 testng.xml 文件或 build.xml 文件中添加有关测试的信息(例如,类名、要运行的组等)。
运行 TestNG。
在这里,我们将看到一个使用 POJO 类、业务逻辑类和测试 XML 的完整 TestNG 测试示例,TestNG 将运行该示例。
在 /work/testng/src 中创建EmployeeDetails.java,这是一个 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;
}
}
EmployeeDetails 类用于:
- 获取/设置员工姓名的值。
- 获取/设置员工月薪的值。
- 获取/设置员工年龄的值。
在 /work/testng/src 中创建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 类用于计算:
- 员工的年薪。
- 员工的考核金额。
现在,让我们在 /work/testng/src 中创建一个名为 TestEmployeeDetails.java 的 TestNG 类。TestNG 类是一个包含至少一个 TestNG 注解的 Java 类。此类包含要测试的测试用例。TestNG 测试可以通过 @BeforeXXX 和 @AfterXXX 注解进行配置(我们将在章节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。此文件的语法非常简单,其内容如下所示。在 /work/testng/src 中创建此文件。
<?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 类。它是一个包含至少一个 TestNG 注解的 Java 类。它可以包含一个或多个测试方法。
使用 javac 编译测试用例类。
/work/testng/src$ javac EmployeeDetails.java EmpBusinessLogic.java TestEmployeeDetails.java
现在使用以下命令运行 TestNG:
/work/testng/src$ java org.testng.TestNG testng.xml
如果所有操作都正确完成,您应该在控制台中看到测试结果。此外,TestNG 在当前目录中自动创建的名为 test-output 的文件夹中创建一个非常好的 HTML 报告。如果您打开它并加载 index.html,您将看到类似于下图中的页面: