您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > TestNG
TestNG測(cè)試用例編寫(xiě)和執(zhí)行
作者:菜鳥(niǎo)蟲(chóng)師 發(fā)布時(shí)間:[ 2017/2/9 11:19:44 ] 推薦標(biāo)簽:單元測(cè)試 測(cè)試用例

  編寫(xiě)TestNG用例測(cè)試基本上包括以下步驟:
  · 編寫(xiě)業(yè)務(wù)邏輯
  · 針對(duì)業(yè)務(wù)邏輯中涉及的方法編寫(xiě)測(cè)試類(lèi),在代碼中插入TestNG的注解
  · 直接執(zhí)行測(cè)試類(lèi)或者添加一個(gè)testng.xml文件
  · 運(yùn)行 TestNG.
  下面我們介紹一個(gè)完整的例子來(lái)測(cè)試一個(gè)邏輯類(lèi);
  1.創(chuàng)建一個(gè)pojo類(lèi)EmployeeDetail.java
public class EmployeeDetail {
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;
}
}
  EmployeeDetail用來(lái):
  · get/set 員工的名字的值
  · get/set 員工月薪的值
  · get/set員工年齡的值
  2.創(chuàng)建一個(gè)EmployeeLogic.java
public class EmployeeLogic {
// Calculate the yearly salary of employee
public double calculateYearlySalary(EmployeeDetail employeeDetails){
double yearlySalary=0;
yearlySalary = employeeDetails.getMonthlySalary() * 12;
return yearlySalary;
}
}
  EmployeeLogic.java用來(lái):
  計(jì)算員工年工資
  3.創(chuàng)建一個(gè)測(cè)試類(lèi),為NewTest,包含測(cè)試用例,用來(lái)進(jìn)行測(cè)試;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import com.thunisoft.Employee.EmployeeDetail;
public class NewTest {
EmployeeLogic empBusinessLogic = new EmployeeLogic();
EmployeeDetail employee = new EmployeeDetail();
// 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");
}
}
  NewTest.java類(lèi)作用:測(cè)試員工年薪
  4.測(cè)試執(zhí)行:選中這個(gè)類(lèi)-》右鍵-》run as 選擇TestNG Test

  5.查看執(zhí)行結(jié)果
  控制臺(tái)會(huì)輸出如下:

  可以看到,運(yùn)行了一個(gè)test,成功輸出
  TestNG輸出控制臺(tái)結(jié)果如下:

  我們可以看到運(yùn)行了一testCalculateYearlySalary測(cè)試方法,并且測(cè)試通過(guò)。
  如果我們將測(cè)試代碼中的Assert.assertEquals(96000, salary, 0.0, "8000");改為
  Assert.assertEquals(86000, salary, 0.0, "8000");,則運(yùn)行結(jié)果如下:

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd