JUnit is a Java Unit Test tool, and in general Eclipse integrates this JUNIT4 test tool
Since it is a test tool, although the development is used more, but as testers also need to have the idea of junit testing, and more technology does not press the body
Here is a brief description of the usage:
1. Create a junit4test project in eclipse
Click "File", "New", "Java project", the following interface appears, fill in the project name Junit4test:
Click Next or Finish to create the Junit4test project
2. Create Com.junit4test Package
Right-click "Junit4test" Project name, select "New" and "package", that is, the configuration interface is as follows:
Fill in the Package name and click Finish to create the Junit4testpackage package
3. Create tested classes and methods
Here we use simple addition to do the subject being tested
Right-click the package name "Junit4testpackage", "New", "Class", the following configuration interface appears:
Configure the class name, then click "Finish" to create the class, and then write an addition method in the class:
Package Com.junit4test;public class Add {public int addtest (int x,int y) {return x + y;}}
4. Create a Test package
Create the same method as above to create a package, take the name "Junit4addtest"
5. Create a test class
Right-click Test Package Name "Com.junit4addtest", "New", "JUnit test Case", the following configuration interface appears:
Write the test class name, select the desired method (if no need to choose) and the class to be tested (because it is not in a package, it is filled with the package name + class name), and then click "Next":
Tick the test method addtest, then select "Finish", this time to create a good use case:
When we see an empty test method, we need to call the test method and make an assertion, the red box on the right should be the following code:
Package Com.junit4addtest;import static Org.junit.assert.*;import Com.junit4test.*;import org.junit.test;public Class Addtest {@Testpublic void testaddtest () {int z = new Add (). addtest; Assertequals (3,z);}}
This is the time to execute the test class and right-click the test case to see that the Java program was executed and is now executing JUnit test:
The results of the implementation are as follows:
The execution result is RUNS:1/1 (a total use case, executes a use case), errors:0 (no error), failures:0 (no failure)
If we change the assertion value to: assertequals (2,z), the result is as follows:
After the assertion has been changed, the left side shows Failures:1 (the failure of a use case), the use case is also shown, and the trace is given below the left.
We copy a Testaddtest method in the test class, one of the wrong one succeeds:
Package Com.junit4addtest;import static Org.junit.assert.*;import Com.junit4test.*;import org.junit.test;public Class Addtest {@Testpublic void testaddtest () {int z = new Add (). addtest; Assertequals (2,z);} @Testpublic void TestAddTest1 () {int z = new Add (). Addtest (2,2); assertequals (4,z);}}
We'll take a look at the results as follows:
Failure is still a token failure
Java Unit Test Tool (Junit)