How to use JUnit for software testing under Android

Source: Internet
Author: User

software Testing as a programmer must be a skill is to determine the length of the software development cycle and the success of the software is the key, you can say that the software is not written code good but effective test decision. This article describes how to use JUnit for unit testing when developing with eclipse under Android.

I. Classification of tests (examples of some of these methods only)"Depending on whether the test knows the code"

1, black box test (do not know the specific code when testing): Refers to the software being tested as a black box, we do not care about the structure inside the box is what it looks like, only concerned about the software input data and output results. It only checks whether the program function is normal use according to the requirements specification, whether the program can receive input data properly and produce the correct output information. Black box testing focuses on the external structure of the program, regardless of the internal logical structure, mainly for the software interface and software function testing.

2, White box test (need to know the specific code when testing): Refers to the box lid open, to study the source code and program results. is according to the procedure internal structure test procedure, passes the test to detect whether the product internal action according to the design specification instruction normal carries on, the examination procedure each path can be able to work according to the predetermined request correctly.

3, gray box test (gray box Test between the black box test and white Box test): It can be understood that the gray box test is concerned about the correctness of output for input, but also focus on internal performance, but this concern is not as detailed and complete as the white box, but through some representational phenomena, events, Flag to determine the internal operating state, sometimes the output is correct, but the internal is actually wrong, this situation is very much, if each time through the white box test to operate, the efficiency will be very low, so need to take such a gray box method.

"According to the granularity of the test"

1, Method Test (function test): Verify the function of the module.

2. Unit Test: Verify the accuracy of the program on the lowest function/parameter, such as testing the correctness of a function.

3. Integration Testing (Intergration test): Verify the functionality of several modules that have dependencies on each other.

"Based on the number of tests"

1, smoke test (smoke test): refers to the tester at the same time on the software to carry out a large number of click or function testing, testing software under such pressure can be carried, the key is the same user in a very short period of time to the software for a large number of repeated tests.

2, the pressure test (pressure test): Refers to the software or website at the same time by a large number of users access, highlighting the software or website is a large number of customer access to the anti-pressure ability, generally used in large-scale website testing.

Ii. Examples of the JUnit tests in which they are explainedhere we want to unit test the Randomarray () method in the Appservice class in the Android application.

Note: JUnit that tries to use Java directly is not valid, should be run in Java Virtual Machine (JVM) When Java application, and the Android program is run on the terminal's Dalvik virtual machine, so the JUnit test directly to it will error, so we can use the following method to test.

1. Create a package and a new test class (Testservice) under the package to test the method, with the following code:
Appservice method of Randomarray:
1  PackageCom.app.wolf;2 3  Public classAppservice {4 /**5 * Random number of n non-repeating in the specified range randomly produces a number in the initialized non-repeating array to be placed in the result,6 * The number of random arrays to be selected, replaced with the corresponding number of subscript of the array (len-1) to be selected and then randomly generated the next random number from the len-2, and so on.7      * 8      * @paramMax9 * Specify the maximum value of the rangeTen      * @parammin One * Specify range minimum value A      * @paramN - * A random number of numbers -      * @returnint[] Random number result set the      */ -      Public Static int[] Randomarray (intMinintMaxintN) { -         intLen = max-min + 1; -  +         if(Max < min | | n >Len) { -             return NULL; +         } A  at         //initializes the array to be selected for the given range -         int[] Source =New int[Len]; -          for(inti = min; I < min + len; i++) { -Source[i-min] =i; -         } -  in         int[] result =New int[n]; -Random rd =NewRandom (); to         intindex = 0; +         //This algorithm 666, I understand the steps should be like this -         //1, first initialize an array of source, the length of the array is the number of users to choose to start the game, and then the elements in the array is the 0~ array length-1; the         //2, then borrow a random variable index, the variable generated by the random number range is 0~ array length-1; *         //3. Finally, the content of the source corresponding to each index corresponds to the result array returned in the last one; $         //The most powerful thing is to use the index index to get the contents of the source array, the length of the array is reduced by 1, and the last element of the array is used instead .Panax Notoginseng         //This will not occur if the elements in the array are reused.  -          for(inti = 0; i < result.length; i++) { the             //Optional Array 0 to (len-2) random subscript +index = Math.Abs (rd.nextint ()% len--); A             //put a random number into the result set theResult[i] =Source[index]; +             //Replaces the number of random numbers in the selected array with the number of subscripts for the array (len-1) you want to select -Source[index] =Source[len]; $         } $          for(intI:result) { -System.out.print (i+ "\ T"); -         } the         returnresult; -     }Wuyi  the}
  
Testservice class:
1  PackageCom.app.wolf.testService;2 3 ImportCom.app.wolf.AppService;4 5 ImportAndroid. R.integer;6 Importandroid.test.AndroidTestCase;7 8  Public classTestserviceextendsAndroidtestcase {9 Ten     /** One * Using JUnit to test the Randomarray method A      * @throwsException -      */ -      Public voidTestrandomarray ()throwsException { theAppservice service=NewAppservice (); -         int[] Resultarray=service.randomarray (2, 7, 6); -          for(intResult:resultarray) { -System.out.print (result+ "\ T"); +         } -     } +  A}
2. If you right-click on the Testrandomarray () method directly in outline to perform the Android JUnit Test, the following exception will be exposed:

"Wolfapp does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library Androi D.test.runner in its androidmanifest.xml "

this is because Instrumentationtestrunner and uses-library are not configured in Androidmanifest.xml. 3, for the above error, we can add the following code in the Androidmanifest.xml file configuration:
<!--Use this line statement to configure instrumentation, but it's worth noting that Targetpackage should choose the package that contains the method you want to test -    <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.app.wolf" >    </Instrumentation>        <!--The uses-library can be configured with a row code, but must be placed under the application node -        <uses-libraryAndroid:name= "Android.test.runner" />
4. After configuring the Androidmanifest.xml, go back to the 2nd step and the Android JUnit test for this method will be executed successfully.

Iii. Summary

In the process of testing should be appropriate to use assert assert to test the program, which is useful for verifying the operation of the program and check the error place, but this test experiment because the output of the array, to determine whether the elements in the arrays are required elements, so that the test will be more troublesome, So the author chooses the way of printing output to carry on the examination.

2016-04-04

BOB

How to use JUnit for software testing under Android

Related Article

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.