logo资料库

西北工业大学软件与微电子学院软件测试课程实验一.docx

第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
资料共10页,剩余部分请下载后查看
Lab1 1, Junit Test cases: package junitexample; import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; @RunWith(Parameterized.class) public class TriangleTest { private static Triangle t=new Triangle(); private String param1; private String param2; private String param3; private String result; @Parameters public static Collection data(){ return Arrays.asList(new Object[][]{ 0!\n"}, 0!\n"}, form a valid triangle!\n"}, triangle is too big\n"}, {"1","1","1","Equilateral"}, {"2","2","1","Isosceles"}, {"-1","3","3","At least one length is less than {"a","1","2","Side 1 is not a number!\n"}, {"1","a","1","Side 2 is not a number!\n"}, {"1","1","a","Side 3 is not a number!\n"}, {"1a","1","1","Side 1 is not a number!\n"}, {"1","1","2","The lengths of the triangles do not {"1000000","1000000","1000000","I feel your {"3","4","5","Scalene"}, {"0","1","1","At least one length is less than {"1a","1a","1a","Side 1 is not a number!\n"}, {"0.9","1.2","2","Scalene"}, {"0.1","0.1","0.1","Equilateral"}, }); } public TriangleTest(String param1,String param2,String param3,String result){ this.param1=param1; this.param2=param2; this.param3=param3; this.result=result; } @Before public void setUp() throws Exception { }
@Test public void testDetermineTriangleType() { t=new Triangle(param1,param2,param3); assertEquals(this.result,t.determineTriangleType()); } } 经测试发现,14 个测试用例只有 5 个测试用例通过测试,发现发生此种情况的原因有: 1 在发生错误后并没有跳出函数,而是继续向下执行 如测试用例 3,4,5,6,7,11,12 2.parseInt 处理函数无法处浮点数 如测试用例 13,14 2.目标代码转换为 java 代码 package junitexample; import java.util.Scanner; public class Lab2 { private int a; private int b; private int c; public Lab2(int params1,int params2,int params3){ this.a=params1; this.b=params2; this.c=params3; } public String isTriangle(){ int count=1; boolean isTri; //System.out.println("Enter the parameters,max and count:"); //Scanner sc = new Scanner(System.in); // count=sc.nextInt(); //max=sc.nextInt(); while(count>0){ String type; //System.out.println("please enter parameters:"); //a=sc.nextInt(); //b=sc.nextInt(); //c=sc.nextInt(); System.out.println("side a is"+a+"side b is"+b+"side c is"+c); if((a
if(isTri){ if((a==b)||(a==c)||(b==c)&&!((a==b)||(a==c))){ type="Isosceles"; return type; } if(a==b&&a==c){ type="Equilateral"; return type; } if((a!=b)&&(a!=c)&&(b!=c)){ type="Scalene"; return type; type="Not a Triangle"; return type; } } else{ } break; } return null; } } 编写测试代码,发现语句覆盖,分支覆盖,路径覆盖测试语句可以相同。测试代码如 下 package junitexample; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class Lab2Test { private int a; private int b; private int c; private String value; private static Lab2 t=new Lab2(0, 0, 0); @Before public void setUp() throws Exception { } public Lab2Test(int params1,int params2,int params3,String value){ this.a=params1; this.b=params2; this.c=params3;
this.value=value; } @Test public void testIsTriangle() { t=new Lab2(a,b,c); assertEquals(this.value,t.isTriangle()); } @Parameters public static Collection data(){ return Arrays.asList(new Object[][]{ {1,2,3,"Not a Triangle"}, {2,3,4,"Scalene"}, {2,2,2,"Equilateral"},//statement coverage {1,2,3,"Not a Triangle"}, {2,3,4,"Scalene"}, {2,2,2,"Equilateral"},//branch coverage {1,2,3,"Not a Triangle"}, {2,3,4,"Scalene"}, {2,2,2,"Equilateral"},//path coverage }); } } 共九个测试用例,经测试发现123456789中369三个测试用例中共通过124578等六个 测试用例,369三个测试用例未通过,经分析是语句判断出现问题。 画出路径图:
三个覆盖的测试用例相同,所以经历路径相同,为
3首先测试代码: package junitexample; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FormulasTest { public static Formulas f=new Formulas(0,0,0); public FormulasTest(int a,int b,int c,int max,int min,int value){ private int a; private int b; private int c; private int max; private int min; private int value; this.a=a; this.b=b; this.c=c; this.max=max; this.min=min; this.value=value; } @Before public void setUp() throws Exception { f=new Formulas(a,b,c); } @Parameters public static Collection data(){ return Arrays.asList(new Object[][]{ {101,-101,99,100,-100,100}, {1,2,3,100,-100,3}, {1,3,2,100,-100,3}, {2,1,3,100,-100,3}, {3,2,1,100,-100,3}, }); } @Test public void testMax() { a=f.clip(min,max,a); b=f.clip(min,max,b); c=f.clip(min,max,c); f=new Formulas(a,b,c); assertEquals(this.value,f.Max(a,b,c)); } }
分享到:
收藏