2023拉

TestNG编写测试

上一篇 / 下一篇  2014-06-23 15:35:59 / 个人分类:Selenium测试

编写TestNG测试基本上包括以下步骤:
  • 测试和编写业务逻辑,在代码中插入TestNG的注解..

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

  • 运行 TestNG.

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

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

publicclassEmployeeDetails{privateStringname;privatedoublemonthlySalary;privateintage;/**
   * @return the name
   */publicStringgetName(){returnname;}/**
   * @param name the name to set
   */publicvoidsetName(Stringname){this.name=name;}/**
   * @return the monthlySalary
   */publicdoublegetMonthlySalary(){returnmonthlySalary;}/**
   * @param monthlySalary the monthlySalary to set
   */publicvoidsetMonthlySalary(doublemonthlySalary){this.monthlySalary=monthlySalary;}/**
   * @return the age
   */publicintgetAge(){returnage;}/**
   * @param age the age to set
   */publicvoidsetAge(intage){this.age=age;}}

EmployeeDetails 类是用来

  • get/set 员工的名字的值

  • get/set 员工月薪的值

  • get/set员工年龄的值

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

publicclassEmpBusinessLogic{// Calculate the yearly salary of employeepublicdoublecalculateYearlySalary(EmployeeDetailsemployeeDetails){doubleyearlySalary=0;yearlySalary=employeeDetails.getMonthlySalary()*12;returnyearlySalary;}// Calculate the appraisal amount of employeepublicdoublecalculateAppraisal(EmployeeDetailsemployeeDetails){doubleappraisal=0;if(employeeDetails.getMonthlySalary()<10000){appraisal=500;}else{appraisal=1000;}returnappraisal;}}

EmpBusinessLogic 类用于计算

  • 员工的年薪

  • 考核支付予雇员

现在,让我们创建一个TestNG 类名称为 TestEmployeeDetails.java 在 C:\ > TestNG_WORKSPACE. TestNG类是一个Java类,它包含至少一个TestNG的注解。 这个类包含测试用例进行测试。可以配置,@BeforeXXX和@AfterXXX注解了TestNG测试 (在本章,我们会看到这样TestNG - Execution Procedure) 它允许执行一些Java逻辑的目标点之前和之后。

importorg.testng.Assert;importorg.testng.annotations.Test;publicclassTestEmployeeDetails{EmpBusinessLogicempBusinessLogic=newEmpBusinessLogic();EmployeeDetailsemployee=newEmployeeDetails();@TestpublicvoidtestCalculateAppriasal(){employee.setName("Rajeev");employee.setAge(25);employee.setMonthlySalary(8000);doubleappraisal=empBusinessLogic.calculateAppraisal(employee);Assert.assertEquals(500,appraisal,0.0,"500");}// test to check yearly salary@TestpublicvoidtestCalculateYearlySalary(){employee.setName("Rajeev");employee.setAge(25);employee.setMonthlySalary(8000);doublesalary=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" ><suitename="Suite1"><testname="test1"><classes><classname="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,会看到类似下面的图片中的一个页面:


TAG:

 

评分:0

我来说两句

Open Toolbar