JUnit test Program

Source: Internet
Author: User
Program JUnit test Program writing specification








First, program naming code





1. The name
of the test class



The naming rule for the
test class is: The class name of the class being tested +test





such as a class called Irgsrhdelegate, then its test class is named Irgsrhdelegatetest











2. Name
for test Cases



The naming rule for
test cases is: test+ use case method name





such as the method to test is called UpdateData, then the name of the test case is Testupdatedata





(Description: "Use case method" means the method contained in the class being tested, and "test case" means the method contained in the test Class)





such as Irgsrhdelegate has a method called Findbyirgfindparam, then the corresponding test case name in Irgsrhdelegatetest is Testfindbyirgfindparam.











3. Other naming conventions





other naming conventions not described in this specification refer to the JAVA Language encoding specification (Eno-w063-java coding rule.doc).











second, the test program's Package name definition specification





in order to maintain the independence and stability of the test program, please organize the test program in the following way:





if the package name of the test class is com.wistrons.util, then the package name of the test class is test.com.wistrons.util. That is, add "test." to the package name of the test class, which is the package name for the test class.











third, test data preparation plan




There are three options to
when preparing test data.





1. Write the test data directly in the program





can be used in this way when there are not many data items to enter











2. Use the Inputdatautil tool class in the Junitpack package





(To use this tool, add import Junitpack to your test program.) Inputdatautil)





This method requires that the test data be written in an XML file, and the format of the XML is as follows:





<?xml version= "1.0" encoding= UTF-8 "?>"





<inputs>





<input>





<IrgCd>h0001</IrgCd>





<IrgName></IrgName>





<IrgKname></IrgKname>





</input>





<input>





...





</input>





...





</inputs>





The root node in this XML file is inputs, and there can be multiple input nodes under the root node. Each input node represents all the data needed in a case.





Use this tool class for the following steps:





1) Gets the storage path of the XML.





put the written XML in the same directory as the test class, and then you can get the XML storage path as follows:





String XML = IrgSrhDelegateTest.class.getResource ("."). ToString () + "Test.xml";











2) To create an instance of Inputdatautil.





inputdatautil inpututil = new Inputdatautil ();











3 Sets the class name of the accepted data in the Inputdatautil instance, Inputdatautil data binding to the XML.





inpututil.setclassname ("Jp.co.liondor.common.fz25IrgSrh"). Seekirgsrhopt ");











4) calls the Inputdatautil.parse () method to collect data from XML





java.util.Vector Vector = (java.util.Vector) inpututil.parse (XML);











5) takes an instance of the bound class from the vector





for (int i = 0; i < vector.size (); i++) {





seekirgsrhopt opt = (seekirgsrhopt) vector.get (i);





...





}











now describes the working principle of inputdatautil. Inputdatautil sets the corresponding set method in the bound class according to the sub-section name under the input node, and then sets the data in the XML to the bound class. For example, in the previous example XML, there are three child nodes under the input node: IRGCD, Irgname, Irgkname. Then, when the Inputdatautil.parse () method is invoked, Inputdatautil invokes the SETIRGCD (), Setirgname (), Setirgkname () method of the Seekirgsrhopt class, respectively, Sets the data to the Seekirgsrhopt instance class and returns the Vector object that contains the instance classes.











3. Using Digester





(To use this tool, add import Org.apache.commons.digester.Digester to the test program)





Digester is a tool class provided by Apache, and the above inputdatautil is also inherited from the Digester class. When the use of Inputdatautil is temporarily unresolved, you can use Digester directly.




The steps for
using Digester are as follows:





1) Gets the storage path of the XML.





are obtained in the same way as using Inputdatautil.











2) To create an instance of Digester.





digester dig = new Digester ();











3 set the class bound to the inputs node as vector





dig.addobjectcreate ("Inputs", "java.util.Vector");











4 Sets the class
that binds to the input node




dig.addobjectcreate ("Inputs/input",





"Jp.co.liondor.common.fz25IrgSrh. Seekirgsrhopt ");





5) According to the sub nodes under the input node, set the corresponding set method sequentially





Dig.addcallmethod ("INPUTS/INPUT/IRGCD", "SETIRGCD", 1);





Dig.addcallparam ("INPUTS/INPUT/IRGCD", 0);











6) Set the method of adding data to the vector





dig.addsetnext ("Inputs/input", "add");











7) calls the Digester.parse () method to collect data from XML





java.util.Vector Vector = (java.util.Vector) dig.parse (XML);











8) takes an instance of the bound class from the vector





for (int i = 0; i < vector.size (); i++) {





seekirgsrhopt opt = (seekirgsrhopt) vector.get (i);





...





}










The use of
digester is very flexible and can organize very complex data.





refer to http://jakarta.apache.org/commons/digester/For detailed usage of digester.

















Four, the principles of UI testing





unit Testing of the UI must not involve business logic operations (such as database operations, interaction with the server). Otherwise, the UI is not designed to be reasonable. Unit testing of the UI should be very simple, just testing whether the interface's actions meet the design requirements.











v. Coverage of test data




The test data prepared during the
test will overwrite all possible case occurrences in the program.











six, Test records





record the process and results of the test, use the log4j tool.











Seven, test the granularity of





the principle of choosing the granularity of test:





1) All public and protected methods in the tested class are measured.





2 does not need to be tested for simple set and get methods.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.