利用MyEclipse结合TestNG测试框架进行单元测试
测试环境
jdk1.8.0_121
myeclipse-10.0-offline-installer-windows.exe
TestNG插件org.testng.eclipse_6.8.6.20130607_0745
代码组织结构
代码示例
Humman.java存放了需要进行测试的类
package mypackage;
public class Humman {
Stringname ="";
Stringsex ="";
StringfoodForEat ="";
public Humman(String name, String sex){
this.name = name;
this.sex = sex;
}
public void eat(String food){
if(food ==""){
System.out.println("啥都没得吃");
this.foodForEat ="";
}else if(food =="apple"){
System.out.println("在吃苹果");
this.foodForEat ="apple";
}
}
public String getFoodForEat(){
return this.foodForEat;
}
//public static void main(Stringargs[]){
//Humman shouke = newHumman("shouke", "M");
//shouke.eat("apple");
//}
}
HummanTest.java 存放TestNG测试类
代码1:
package unittests;
import mypackage.Humman;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
public class HummanTest {
@Test
public void testGetFoodForEat() {
Humman shouke =new Humman("shouke","M");
String FoodForEat = shouke.getFoodForEat();
assertEquals("", FoodForEat);
}
@Test(dependsOnMethods = {"testGetFoodForEat" })
public void testEatWithFoodEqualsApple() {
Humman shouke =new Humman("shouke","M");
shouke.eat("apple");
String FoodForEat = shouke.getFoodForEat();
assertEquals("apple", FoodForEat);
}
@Test(dependsOnMethods = {"testGetFoodForEat" })
public void testEatWithFoodEmpty() {
Humman shouke =new Humman("shouke","M");
shouke.eat("");
String FoodForEat = shouke.getFoodForEat();
assertEquals("", FoodForEat);
}
}
代码2:
package unittests;
import mypackage.Humman;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
public class HummanTest {
Hummanshouke;
public HummanTest(){
this.shouke =new Humman("shouke","M");
}
@Test
public void testGetFoodForEat() {
String FoodForEat =this.shouke.getFoodForEat();
assertEquals("", FoodForEat);
}
@Test(dependsOnMethods = {"testGetFoodForEat" })
public void testEatWithFoodEqualsApple() {
this.shouke.eat("apple");
String FoodForEat =this.shouke.getFoodForEat();
assertEquals("apple", FoodForEat);
}
@Test(dependsOnMethods = {"testGetFoodForEat" })
public void testEatWithFoodEmpty() {
this.shouke.eat("");
String FoodForEat =this.shouke.getFoodForEat();
assertEquals("", FoodForEat);
}
}
运行结果
个人理解
1、撇开对TestNG之类的测试框架进行封装、自行开发框架不谈,如果以类方法、函数为单元的话,个人觉得对单元测试主要就是构造对象(如果有必要的话),对类方法、函数进行传参调用,然后检测结果。
2、单元测试的核心是用例设计,这块网上查找白盒测试用例设计相关资料
3、思考了下,如果基于TestNG进行二次开发,只写一个测试类和一个测试方法,估计有点困难,因为被测试类的构造函数不同,并且每个方法的输入参数和输出结果也都不一样,所以,即便是用反射机制也似乎很不好处理。不过我们可以做些参数化处理,这样还可以减少测试方法的编写
package unittests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
import mypackage.Humman;
public class HummanTest {
Hummanshouke;
public HummanTest(){
this.shouke =new Humman("shouke","M");
}
@Test
public void testGetFoodForEat() {
String FoodForEat =this.shouke.getFoodForEat();
assertEquals("", FoodForEat);
}
@Test(dependsOnMethods = {"testGetFoodForEat" }, dataProvider ="dataprovider")
public void testEat(Object... args) {
this.shouke.eat((String)args[0]);
String FoodForEat =this.shouke.getFoodForEat();
assertEquals(args[1], FoodForEat);
}
@Test
@DataProvider(name ="dataprovider")
public Object[][] providFood(){
return new Object[][]{
{"",""},
{"apple","apple"},
};
}
}
运行结果: