|
Unit Test BasicsToday's software testing is very prevalent, I through the project practice and personal experience to talk about unit testing, I have been adhering to the "principle of using Code", but also hope that individuals can give valuable advice, common exploration and common progress, for the Chinese software industry has a greater development common struggle. The first project that our project team developed, write code is always written from the bottom to the performance layer to the JSP, and then the developer in the Web Layer debugging page, almost 98 will be a lot of exception, and then add a breakpoint in the code step by step to find out which layer of code problems. The better approach is to add the main method test in each class, but the overall is not ideal, to the Web layer developers debugging and quality control personnel to bring heavy work pressure; Use unit testing, for each method to do strict checks, greatly reduce the time to debug; and the quality control personnel returned to the bug Less than 60%, now very skilled developers write test cases, and I based on the actual situation of the test cases made a little change (this part is mainly in the following code detailed), bring good results.What benefits does unit testing bring to actual development? (1) (1) in the first place for developers to significantly reduce the time to debug, but also standardized for code security management (we know that those methods can be called), (2) (2) for the entire project, with a complete test to ensure the final delivery of the project The test has a reliable basis, (3) (3) for testers to significantly reduce the feedback of the Bug, (4) (4) for the project manager to achieve a good overall project control; (5) (5) The main complete unit test Post-maintenance personnel bring great convenience. Unit Test benefits may still have a lot, but I can only understand and sentiment so much, hope the viewer to add.Unit Test configuration: I'm going to use Eclipse + Myeclopse to give you a simple configuration of the JUNIT environment; Right click on the project Select "Properties", in the pop-up window to the environment variable to add Junit.jar package, so that the next step we can do unit testing; Use Eclipse Rapid Development Test case :The following figure: Right click on the class you want to test, in the new in the "JUnit test Case", pop-up dialog box, configure the test name and root directory, add comments, etc., and then click "Next" to the following image: Select the method you want to test the class, click Finish. The basic framework of the test class is generated, as in the following code, we take a DAO class test as an example:/**//* * Copyright reserved by Xxxxco. Ltd. * Author:fu Wei Date:2006-9-4 */
Import Junit.framework.TestCase;
/** */ /** * @author Fu Wei */ public class Orgtypedaotest extends TestCase ... { /** */ /** * @param arg0 */ Public Orgtypedaotest (String arg0) ... { Super (ARG0); }
/**/ /* * @see Junit.framework.testcase#setup () */ protected void SetUp () throws Exception ... { Super. SetUp (); }
/**/ /* * @see Junit.framework.testcase#teardown () */ protected void teardown () throws Exception ... { Super. Teardown (); } /** */ /** * Main function * @param args */ public static void Main (string[] args) ... { Testrunner.run (Orgtypedaotest. Class); } /** */ /** * {@link test method for Orgtypedao#getorgtypelist ()}. */ Public final void testgetorgtypelist () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#insertorgtypeinfo (Com.zhjy.mltx.vo.OrgTypeVO)} test method. */ Public final void Testinsertorgtypeinfo () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#deleteorgtypeinfo (java.lang.String)} test method. */ Public final void Testdeleteorgtypeinfo () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#updateorgtypeinfo (Com.zhjy.mltx.vo.OrgTypeVO)} test method. */ Public final void Testupdateorgtypeinfo () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link Orgtypedao#getorgtypeinfobyid (java.lang.String)} test method. */ Public final void Testgetorgtypeinfobyid () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#isrepeatorgtypeinfo (java.lang.String)} test method. */ Public final void testisrepeatorgtypeinfostring () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#isrepeatorgtypeinfo (Com.zhjy.mltx.vo.OrgTypeVO)} test method. */ Public final void Testisrepeatorgtypeinfoorgtypevo () ... { Fail ("not yet implemented"); Todo }
/** */ /** * {@link orgtypedao#getflatorgidbyname (java.lang.String)} test method. */ Public final void Testgetflatorgidbyname () ... { Fail ("not yet implemented"); Todo } } JUnit unit test a few points to note:(1) Import Junit.framework.TestCase and Junit.textui.TestRunner; (2) Inherit junit.framework.TestCase; (3) Add a main method by yourself Testrunner.run (Test class name. Class) is called in the (4) There is a constructor that calls Super (String); all of which are JUnit-bound, and above all, we find a lot of methods that start with test, and these are the methods we're going to test, and the Junti test actually takes the form of assertions, as long as I All the methods at the beginning of test add an assertion method to the data, while providing many methods of assertion,
Common assertion Methods |
assertequals (Failure prompt information, expected data , "test data") |
asserts whether the data is equal to the desired |
assertnotnull ("Failure message", "Test Data") |
assertion get data is not NULL, otherwise prompt error |
assertnull (" |
|