TestNG编写测试

编写TestNG测试基本上包括如下步骤:html

  • 测试和编写业务逻辑,在代码中插入TestNG的注解..java

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

  • 运行 TestNG.yii

在这里,咱们将看到一个完整的例子了TestNG测试使用POJO类,业务逻辑类,将经过TestNG的运行测试XML。测试

建立 EmployeeDetails.java 在 C:\ > TestNG_WORKSPACE 是一个 POJO 类.ui

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 类是用来this

  • get/set 员工的名字的值spa

  • get/set 员工月薪的值xml

  • get/set员工年龄的值htm

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

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 类用于计算

  • 员工的年薪

  • 考核支付予雇员

如今,让咱们建立一个TestNG 类名称为 TestEmployeeDetails.java 在 C:\ > TestNG_WORKSPACE. TestNG类是一个Java类,它包含至少一个TestNG的注解。 这个类包含测试用例进行测试。能够配置,@BeforeXXX和@AfterXXX注解了TestNG测试 (在本章,咱们会看到这样TestNG - Execution Procedure) 它容许执行一些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>

以上文件的详情以下:

  • 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建立了一个很是漂亮的HTML报告,会自动在当前目录下建立一个文件夹名为test-output 。若是打开​​并加载的index.html,会看到相似下面的图片中的一个页面:

Writing Tests

相关文章
相关标签/搜索