java测试用例编写_TestNG测试用例编写和执行
編寫TestNG用例測試基本上包括以下步驟:
編寫業(yè)務(wù)邏輯
針對業(yè)務(wù)邏輯中涉及的方法編寫測試類,在代碼中插入TestNG的注解
直接執(zhí)行測試類或者添加一個testng.xml文件
運(yùn)行 TestNG.
下面我們介紹一個完整的例子來測試一個邏輯類;
1.創(chuàng)建一個pojo類EmployeeDetail.java
public classEmployeeDetail {privateString name;private doublemonthlySalary;private intage;/***@returnthe name*/
publicString getName() {returnname;
}/***@paramname the name to set*/
public voidsetName(String name) {this.name =name;
}/***@returnthe monthlySalary*/
public doublegetMonthlySalary() {returnmonthlySalary;
}/***@parammonthlySalary the monthlySalary to set*/
public void setMonthlySalary(doublemonthlySalary) {this.monthlySalary =monthlySalary;
}/***@returnthe age*/
public intgetAge() {returnage;
}/***@paramage the age to set*/
public void setAge(intage) {this.age =age;
}
}
EmployeeDetail用來:
get/set 員工的名字的值
get/set?員工月薪的值
get/set員工年齡的值
2.創(chuàng)建一個EmployeeLogic.java
public classEmployeeLogic {//Calculate the yearly salary of employee
public doublecalculateYearlySalary(EmployeeDetail employeeDetails){double yearlySalary=0;
yearlySalary= employeeDetails.getMonthlySalary() * 12;returnyearlySalary;
}
}
EmployeeLogic.java用來:
計(jì)算員工年工資
3.創(chuàng)建一個測試類,為NewTest,包含測試用例,用來進(jìn)行測試;
importorg.testng.Assert;importorg.testng.annotations.Test;importorg.testng.annotations.BeforeMethod;importorg.testng.annotations.AfterMethod;importorg.testng.annotations.DataProvider;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeTest;importorg.testng.annotations.AfterTest;importorg.testng.annotations.BeforeSuite;importorg.testng.annotations.AfterSuite;importcom.thunisoft.Employee.EmployeeDetail;public classNewTest {
EmployeeLogic empBusinessLogic= newEmployeeLogic();
EmployeeDetail employee= newEmployeeDetail();//test to check yearly salary
@Testpublic voidtestCalculateYearlySalary() {
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);double salary =empBusinessLogic
.calculateYearlySalary(employee);
Assert.assertEquals(96000, salary, 0.0, "8000");
}
}
NewTest.java類作用:測試員工年薪
4.測試執(zhí)行:選中這個類-》右鍵-》run as 選擇TestNG Test
5.查看執(zhí)行結(jié)果
控制臺會輸出如下:
可以看到,運(yùn)行了一個test,成功輸出
TestNG輸出控制臺結(jié)果如下:
我們可以看到運(yùn)行了一testCalculateYearlySalary測試方法,并且測試通過。
如果我們將測試代碼中的Assert.assertEquals(96000, salary, 0.0, "8000");改為
Assert.assertEquals(86000, salary, 0.0, "8000");,則運(yùn)行結(jié)果如下:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java测试用例编写_TestNG测试用例编写和执行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: v8引擎和v12引擎_v8和v12发动机
- 下一篇: 接口有个电池标志_有人说手机快充毁电池,