参数化测试主要解决一次性进行多个测试用例的测试。其主要思想是,将多个测试用例按照,{输入值,输出值}(输入值可以是多个)的列表方式进行测试。
本文使用的是Eclipse+Junit4进行测试。之所以使用Eclipse+Junit4而不是使用Idea+Junit5的环境。是因为Eclipse+junit4环境能够轻易生成ant自动化测试报告。(关于自动化测试报告后面进行讲述)
主要看PeopleTestParameterized.java文件
项目结构
Clothes.java
packagebarry;publicabstractclassClothes{publicabstractdoublegetprice();}
Cap.java
packagebarry;publicclassCapextendsClothes{publicdoubleC_price;publicdoubleC_discout;publicintDiscount;Cap(double price,double discout,intDis){C_price= price;C_discout= discout;Discount=Dis;}@Overridepublicdoublegetprice(){doublePrice;if(Discount==1){Price=C_price*(1-C_discout);}else{Price=C_price;}returnPrice;}}
Jacket.java
packagebarry;publicclassJacketextendsClothes{publicdoubleJ_price;publicdoubleJ_discout;publicintDiscount;Jacket(double price,double discount,intDis){J_price= price;J_discout= discount;Discount=Dis;}@Overridepublicdoublegetprice(){doublePrice;if(Discount==1){Price=J_price*(1-J_discout);}else{Price=J_price;}returnPrice;}}
Trousers.java
packagebarry;publicclassTrousersextendsClothes{publicdoubleT_price;publicdoubleT_discount;publicintDiscount;Trousers(double price,double discount,intDis){T_price= price;T_discount= discount;Discount=Dis;}@Overridepublicdoublegetprice(){doublePrice;if(Discount==1){Price=T_price*(1-T_discount);}else{Price=T_price;}returnPrice;}}
People.java
packagebarry;publicclassPeople{publicClothes cap;publicClothes jacket;publicClothes trousers;People(){}People(Clothes cap,Clothes jacket,Clothes trousers){this.cap = cap;this.jacket = jacket;this.trousers = trousers;}publicdoublegetAllprice(){doublePrice=0;Price+= cap.getprice();Price+= jacket.getprice();Price+= trousers.getprice();returnPrice;}publicvoidclear(){this.cap =null;this.jacket =null;this.trousers =null;}publicvoidtimetest(){for(int i =0; i <100;i++){}}}
PeopleTestParameterized.java
packagebarry;importstaticorg.junit.Assert.*;importjava.util.Arrays;importjava.util.Collection;importstaticorg.junit.jupiter.api.Assertions.assertEquals;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.junit.runners.Parameterized;importorg.junit.runners.Parameterized.Parameters;//表示使用参数化测试的方法@RunWith(Parameterized.class)publicclassPeopleTestParameterized{privatePeople param;privatedouble result;@BeforepublicvoidsetUp(){People people =newPeople();}@AfterpublicvoidtearDown(){}//测试用例使用Collection数据类型返回@ParameterspublicstaticCollectiondata(){Cap cap =newCap(1999,0.1,0);Jacket jacket =newJacket(2999,0.2,1);Trousers trousers =newTrousers(7999,0.1,1);People p_1 =newPeople(cap,jacket,trousers);double re_1 =PeopleTest.cal(cap, jacket, trousers);
cap =newCap(19999,0.1,0);
jacket =newJacket(29999,0.2,1);
trousers =newTrousers(79999,0.1,1);People p_2 =newPeople(cap,jacket,trousers);double re_2 =PeopleTest.cal(cap, jacket, trousers);
cap =newCap(199,0.1,0);
jacket =newJacket(9999,0.2,1);
trousers =newTrousers(799,0.1,1);People p_3 =newPeople(cap,jacket,trousers);double re_3 =PeopleTest.cal(cap, jacket, trousers);
cap =newCap(2199,0.1,0);
jacket =newJacket(49999,0.2,1);
trousers =newTrousers(6799,0.1,1);People p_4 =newPeople(cap,jacket,trousers);double re_4 =PeopleTest.cal(cap, jacket, trousers);
cap =newCap(199,0.1,0);
jacket =newJacket(9999,0.2,1);
trousers =newTrousers(799,0.1,1);People p_5 =newPeople(cap,jacket,trousers);//这里故意写错一个double re_5 =PeopleTest.cal(cap, jacket, trousers)+1;returnArrays.asList(newObject[][]{{p_1,re_1},{p_2,re_2},{p_3,re_3},{p_4,re_4},{p_5,re_5},});}//使用构造函数将列表中的数据复制到private中publicPeopleTestParameterized(People param,double result){this.param = param;this.result = result;}//观察发现,这里的test1也执行了5次。@Testpublicvoidtest1(){System.out.print("sdf");}//进行多个测试用例测试@TestpublicvoidtestGetAllPrice(){double re=0;assertEquals(result, param.getAllprice());}}
运行结果
故意写错的那个用例就是[4]
ant报告,就是测试用例10个,但是有一个错误的那个。
小结
在使用参数化测试方法中。
首先要在测试类前一行写入 @RunWith(Parameterized.class) 表明这个类使用的是参数化测试方法。随后定义两个private变量param与result。前者为测试用例输入,后者为测试用例输出。
其次,要确保Collection数据书写正确,参数化测试方法的数据来源即该Collection数据。并且该方法头部使用@Parameters。
第三步,正确书写构造函数,这里的构造函数的功能是将Collection数据列表中数据赋值给两个private变量,param,result。
最后,编写测试代码即可。
单元测试中掌握参数化测试方式是比较重要的,通过以上简单的示例记录本次学习内容。
版权归原作者 牛右刀薛面 所有, 如有侵权,请联系我们删除。