3月11日 实验1 黄元琪3014218150

1 : Build test environment

 

1) : Build a project.Download Junit4.12 and Hamcrest1.3.

2) : Build a new project and two testing class.

3) : Click the button, then select junit-4.12 and hamcrest-core-1.3.

 

4) : As shown in figure:

5): Decompress the eclemma-2.3.3 under the directory eclipse/dropins 

6) : Click help-install new software-add; Then locate the path as follow:

 

2: Write the testing code:

In the first class triangle.java:

package junittest;

public class triangle {
public String triangles (int a, int b, int c){
if(a+b > c && a+c > b && b+c > a){
if (a == b && b == c)
return "equilateral triangle";
else if (a == b || b == c || c == a)
return "isosceles triangle";
else
return "scalene triangle";

}
else
return "not triangle";
}
}

 

In the second class triangleTest.java:

package junittest;

import static org.junit.Assert.*;

import org.junit.Test;

public class triangleTest {
triangle temp1 = new triangle();
triangle temp2 = new triangle();
triangle temp3 = new triangle();
triangle temp4 = new triangle();

@Test
public void testTriangle() {
assertEquals("equilateral triangle",temp1.triangles(1,1,1));
assertEquals("isosceles triangle",temp2.triangles(2,2,3));
assertEquals("not triangle",temp3.triangles(1,2,3));
assertEquals("scalene triangle",temp4.triangles(2,3,4));
}

}

 

3: Run the testing code, we will get the result that the coverage is 100%;

 

posted @ 2017-03-12 00:03  huangyuanqi  阅读(212)  评论(0编辑  收藏  举报