Software Testing Lab1 (软件测试实验一) —— junit hamcrest and eclemma安装及入门
LAB 1 REPORT
Firstly, install junit, hamcrest and eclemma.
Junit is a unit testing framework for the Java programming language.
Hamcrest is a framework that assists writing software tests in the Java programming language.
----Wikipedia
Thus, before we install Junit and hamcrest, Eclipse is needed. There will be some difficulties while configuring it. Blog is a good way to figure out the steps and solutions.
Download juint.jar and hamcrest.jar from the official website. Then, click right button and select "Build path"->"Configure Build path"->"Libraries"->"Add external JARs". Finally, find out the jar files that have been downloaded.

The way to install eclemma is similar to last one. But, paying more attention, the path to add eclemma is "Help"->"Install New Software"->"Add"->"local". When it is finished, there will be a signal on the tool column.

Secondly, write a program for the triangle problem. There is the description of that problem.
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.
There is the code.
package cn.tjuscs.st.danning; public class triangle { public int triangleJudge(int a,int b,int c){ int kindOfTrg; //The number is right if(a > 0 && b > 0 && c > 0){ //equilateral if(a == b && a == c){ return kindOfTrg = 2; } //scalene else if(a != b && a != c && b != c){ return kindOfTrg= 0; } //isosceles else{ return kindOfTrg = 1; } } //Can not be a triangle else { return kindOfTrg = -1; } } }
Thirdly, test the program with Junit. This is the frame of my program.

Code the testing part. It has to coverage every way of the program.
package cn.tjuscs.st.danning; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.After; 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 triangleTest { private int a; private int b; private int c; private int expected; private static triangle triTest; public triangleTest(int a,int b,int c,int expected){ this.a = a; this.b = b; this.c = c; this.expected = expected; } @Before public void setUp() throws Exception { triTest = new triangle(); } @After public void tearDown() throws Exception { } @Parameters public static Collection<Object[]> getData(){ return Arrays.asList(new Object[][]{ {0,2,3,-1}, {2,2,3,1}, {2,2,2,2}, {5,4,3,0} }); } @Test public void test(){ assertEquals(this.expected,triTest.triangleJudge(this.a,this.b,this.c)); } }
Test the program with junit and eclemma.


浙公网安备 33010602011771号