This experiment is the basic application of Junit,hamcrest,eclemma unit test, starting from the Installation. The language chosen here is java,ide for Eclipse (mars.2)
1 ) Install junit, hamcrest and Eclemma
1.1 Installing Juint,hamcrest
Both of these installations I used the same method of importing the jar package, Selecting the Java build path in the "project", "properties", and importing the jar packages that were prepared externally, I am importing JUnit (4.12) and hamcrest-all-1.3. After importing, you can generate test instances and test Them.
1.2 Installing Eclemma
Installing Eclemma I'm Using a method that uses Eclipse to add a Program. With the site http://update.eclemma.org/added, after the successful addition of the Eclemma icon appears and can be overwritten Detection.
2 ) write the program and test
2.1 Writing a program
The procedure below can be used to judge whether a triangle is an equilateral isosceles or a bevel. however, It is not possible to detect whether three edges can form a triangle.
Package mainapp;
public class triangle{
public int a,b,c;//input parameter
public int Result;//store result
public int judge (int a,int b,int C) {
if (a = = B&&b ==c) {
result = 1;
}
Else if (a = = b| | b = = C| | C ==a) {
result = 2;
}
Else
{
result = 3;
}
return result;
}
}
2.2 Generating Test Cases
First create a second source folder with the same name package
Then right-click on Triangle.java to create a new JUnit test case:
Choose to create in the package you just created, and then the program will help us generate a test case named Triangletest in that package, partially modified to get the test case:
Package mainapp;
Import Static org.junit.assert.*;
Import org.junit.Before;
Import org.junit.Test;
public class Triangletest {
Triangle Tri;
int result;
@Before
public void SetUp () throws Exception {
Tri = new Triangle ();
}
@Test
public void Testequilateral () {
result = Tri.judge (2, 2, 2);
Assertequals (1,result);
}
@Test
public void Testisosceles () {
result = Tri.judge (2, 2, 3);
Assertequals (2,result);
}
@Test
public void Testscalene () {
result = Tri.judge (3, 2, 4);
Assertequals (3,result);
}
}
Then right-click the test case, run it, and run it in the JUnit test case. Get the running Result:
There is no overlay test using eclemma, We click on the Eclemma icon and select triangletest, then get the overlay result:
The test case is completely overwritten with detailed information appearing BELOW. This unit test is Complete.
3 ) Insufficient experimental
The test procedure in this experiment has an error in itself and will not be detected by the test Case. At the same time, the test case only uses the assertequal () function, the function itself as if the JUnit interior has been integrated, and does not use the hamcrest other matching device.
Junit,hamcrest,eclemma Unit Test Basics (java)