The process of developing using XP, Unit Test is an essential part. As a Unit Test, Junit is the preferred tool. This article is briefly described with a basic usage of JUnit from the use of the Into-use purposes, how to use, and the need to consider in use.
purpose of usage
JUnit is a Framework written in Java. At present, some popular Unit Test tools are mostly extended on JUnit. The current version is JUnit 3.8.1, which can be downloaded from www.junit.org.
usage
1. Basic use steps, JUnit is very simple, its basic use steps:
- Created, from JUnit.Framework.Testcase to derive the west case required for UNIT TEST
- Writing test method, providing test methods similar to the following function signature:
Public void testxxxxx ();
- Compile, write Test Case, compile written TEST CASE class
- Run, start the JUnit Test Runner to run this Test Case.
Junit offers 2 basic Test Runner: Characters and graphical interfaces. The start commands are as follows:
a graphical interface:
Java junit.swingui.teestrunner xxxxx
B character interface:
Java junit.textui.teestrunner xxxxx
2. Example:
Import junit.frmework.testcase;
Public Class Testsample Extends Testcaset {
Public void testmethod1 () {
Asserttrue (TRUE);
}
}
3. Setup and Teardown, these two functions are initialization and reverse initialization of each test method in JUnit Framework. Setup is called before each test method call, responsible for initializing the test environment required to test the test method; Teardown is called after each test method is called, responsible for revoking the test environment. The relationship with the test method can be described as follows:
Test start -> setup -> TestXxxx -> Teardown -> Test end
4. Example:
Import junit.frmework.testcase;
Public Class Testsample Extends Testcaset {
protected void setup () {
//initialization……
}
Public void testmethod1 () {
Asserttrue (TRUE);
}
Potected void teardown () {
// Removal of initialization ...
}
}
5. Distinguish Fail, Exception.
- Fail, expects to appear errors. Cause: Assert Function error (such as Assertfalse (TRUE)); Fail function is generated (such as fail (...)).
- Exception, undesired errors, which belongs to the Unit Test program running. It is a type of Runtime that is thrown during the runtime process.
For a function such as ASSERT, FAIL, see Junit's Javadoc.
6. Use example:
Import junit.frmework.testcase;
Public Class Testsample Extends Testcaset {Protected Void Setup () {
//initialization……
}
Public void testmethod1 () {
......
Try {
Boolean b = ...
Asserttrue (b);
Throw new Exception ("this is a test.");
Fail ("Unable Point."); // It is impossible to arrive
} catch (exception e) {
Fail ("Yes, I catch u"); / / should arrive point
}
......
}
Potected void teardown () {
// Removal of initialization ...
}
}
7. Assemble TestSuite, run more TEST. In Junit, Test, Testcase and TestSuite have formulated ComposiSte Pattern. By assembling your own TestSuite, you can complete all TestCase to add to this TestSuite. And these definition TestSuite can also assemble greater TestSuite, which is also convenient for continuously increased TestCase management and maintenance.
Another benefit of it is that you can start calling from any of this TestCase tree (TestSuite or TestCase) to complete all TestCase of this node below. Improve the flexibility of UNIT TEST.
8. Use example:
Import junit.framework.test;
Import junit.framework.testsuite;
Public class testall {
Public class testall {
// Define a Suite, which can be considered similar to the Java application for the JUnit.
Public static test suite () {
"Running All Tests.") And TestSuite Suite = New Testsuite;
Suite.addTestSuite (Testcase1.class);
Suite.addTestSuite (Testcase2.class);
Return suite;
}
}
It is the same as a TestCase that runs separately, see STEP 1 "Run".
9. Using Ant Junit Task. In addition to using Java to run Junit, we can also run using JUnit Task provided by JUnit to combine with Ant. Several major Ant Tasks involved as follows:
-
-
-
-
-
-
For details, please refer to the relevant documentation.
10. Use example:
claspath>
batchtest>
junit>
fileset>
junitreport>
checklist
JUnit is not very difficult, but it is not easy to write a good TestCase. A bad TestCase is often a waste of time and can't play actual role. Instead, a good TestCase can not only pointed out the problems in the code, but also as a more accurate document as a code, and it is also very important in the process of continuous integration. Several points you need to pay attention to when you write TestCase:
- The independence of the test: Only one object is tested once, it is convenient to position the wrong position. This has 2 dense: a TestCase, only test an object; a TestMethod, only one method in this object.
- give a test method a suitable name.
- Reasons for failed in the Assert function, such as: Asserttrue ("... Should Be true", ...), easy to find out. In this example, if it is not possible to pass Asserttrue, then the given message will be displayed. Each Assert function in JUnit has the first parameter is a function prototype of the message when an error is wrong.
- Test all places that may cause fail, such as: Frequent modified functions in a class. For classes that only contain Getter / setter, if it is generated by IDE (such as Eclipse), it can be unpredictable; if it is manually written, then it is best to test it.
- The code in Setup and Teardown should not be related to the test method, but should be globally related. For example, the code in SETUP and TEARDOWN should be the code required by A and B.
- Test code organization: the same package, different directories. In this way, the test code can access the protected variable / method of the test class, which is convenient for test code. Placing in different directories, it is convenient for the management of the test code and the package and release of the code. An example is as follows: SRC <= Source Code Root Catalog
| --- COM
| --- MOD1
| --- Class1
JUnit <= test code root directory
| --- COM
| --- MOD1
| --- Class1