How to Use JUnit for software testing in Android, Android junit

Source: Internet
Author: User

How to Use JUnit for software testing in Android, Android junit
Software testing, as a necessary skill for programmers, is the key to determining the length of the software development cycle and the success or failure of software operation. It can be said that good software is not decided by writing well but by effective testing. This article describes how to use JUnit for unit testing during eclipse development in android.

 

1. Classification of tests (only some of the methods are used) [Do you know the code based on the test]

1. Black box testing (the specific code is not known during testing): The tested software is regarded as a black box. We don't care about the structure in the box, but only about the input data and output results of the software. It only checks whether the program functions are normally used according to the requirements specification, and whether the program can properly receive input data to generate correct output information. Black box testing focuses on the external structure of the program, without considering the internal logic structure. It mainly tests the software interface and software functions.

2. White-box testing (specific code is required during testing): Refers to opening the lid of the box to study the source code and program results. Tests are conducted according to the internal structure test procedures of the program to check whether the internal actions of the product are normal in accordance with the provisions of the design specification, and whether each path in the program can work correctly according to the predefined requirements.

3. gray box test (the gray box test is between the black box test and the white box test ):It can be understood in this way that the gray box test focuses on the correctness of the output for the input and the internal performance, but this attention is not as detailed and complete as the white box, it is only through some characterization phenomena, events, signs to determine the internal running status, sometimes the output is correct, but the internal is actually wrong, this situation is very many, if you use a white box test every time, the efficiency will be very low. Therefore, you need to use such a gray box method.

[Based on test granularity]

1. function test): Verify the functions of the module.

2. unit test): Verify the accuracy of the program with the lowest function/parameter, for example, test the correctness of a function.

3. intergration test): Verify the functions of several interdependent modules.

[Based on the number of tests]

1. smoke test): It refers to whether testers can carry out a large number of clicks or functional tests on the software at the same time, and whether the testing software can withstand such pressure, the key lies in a large number of repeated tests on the software within a very short period of time for the same user.

2. Stress test): It refers to the ability of a software or website to be accessed by a large number of users within the same period of time. It highlights the ability to withstand pressure when the software or website is accessed by a large number of customers and is generally used for testing on large websites.

 

2. Give an example to explain the JUnit test.Here we want to perform a unit test on the randomArray () method in the AppService class of the android application.

Note: It is invalid to try to use java's JUnit directly. It should be executed on the java Virtual Machine (JVM) during java applications, while the android program runs on the terminal's Dalvik virtual machine, therefore, an error is reported when you perform the JUnit test directly. Therefore, you can use the following methods to perform the test.

1. Create a package and create a test class (TestService) under the package to test the method. The Code is as follows:
The randomArray method of AppService: 
1 package com. app. wolf; 2 3 public class AppService {4/** 5 * N non-repeated numbers in the random specified range are randomly generated in the initialized non-repeated array to be selected and placed into the result, 6 * replace the random number of the array to be selected with the number corresponding to the subscript of the array to be selected (len-1) and then randomly generate the next random number from the len-2, similarly, 7*8 * @ param max 9 * specifies the maximum range of 10 * @ param min11 * specifies the minimum range of 12 * @ param n13 * Number of random numbers 14 * @ return int [] Random Number result set 15 */16 public static int [] randomArray (int min, int max, int n) {17 int len = max-min + 1; 18 19 if (max <min | n> len) {20 Return null; 21} 22 23 // initialize the 24 int [] source = new int [len]; 25 for (int I = min; I <min + len; I ++) {26 source [I-min] = I; 27} 28 29 int [] result = new int [n]; 30 Random rd = new Random (); 31 int index = 0; 32 // The algorithm 666, the steps I understand should be like this: 33 // 1. First Initialize an array source. The length of this array is the number of people the user chooses to start the game, and then the elements in the array are 0 ~ Array length-1; 34 // 2. Then borrow a random variable index. The random number range of this variable is 0 ~ Array length-1; 35 // 3. Finally, the source content corresponding to each index is mapped to the final returned result array; 36 // The most amazing thing is to first use the index to retrieve the content in the source array. At this time, the length of the array is reduced by 1, and then replaced by the last element of the array, 37 // in this way, the elements in the array will not be reused. 38 for (int I = 0; I <result. length; I ++) {39 // The array to be selected 0 to (len-2) random subscript 40 index = Math. abs (rd. nextInt () % len --); 41 // Add the random number to the result set 42 result [I] = source [index]; 43 // replace 44 source [index] = source [len] with the number of random numbers in the to-be-selected array (len-1) subscript; 45} 46 for (int I: result) {47 System. out. print (I + "\ t"); 48} 49 return result; 50} 51 52}
  
TestService class:
1 package com. app. wolf. testService; 2 3 import com. app. wolf. appService; 4 5 import android. r. integer; 6 import android. test. androidTestCase; 7 8 public class TestService extends AndroidTestCase {9 10/** 11 * use JUnit to test randomArray Method 12 * @ throws Exception13 */14 public void testRandomArray () throws Exception {15 AppService service = new AppService (); 16 int [] resultArray = service. randomArray (2, 7, 6); 17 for (int result: resultArray) {18 System. out. print (result + "\ t"); 19} 20} 21 22}
2. If you right-click the testRandomArray () method in outline and perform Android JUnit Test, the following exception is displayed:

"WolfApp does not specify a android. test. InstrumentationTestRunner instrumentation or does not declare uses-library android. test. runner in its AndroidManifest. xml"

This is because the InstrumentationTestRunner and uses-library are not configured in AndroidManifest. xml. 3. For the preceding error, add the following code to the AndroidManifest. xml file:
<! -- Use this line to configure the instrumentation, but it is worth noting that the targetPackage should select the package where the method you want to test is located --> <instrumentation android: name = "android. test. instrumentationTestRunner "android: targetPackage =" com. app. wolf "> </instrumentation> <! -- Use the modified Code to configure uses-library, but must be placed under the application node --> <uses-library android: name = "android. test. runner"/>
4. After configuring AndroidManifest. xml, return to Step 1 and perform Android JUnit Test on the method.

 

Iii. Summary

During the test, the assert should be used properly to test the program, which is useful for testing the program running and checking the errors, however, in this test, because the output is an array, if you want to determine whether the elements in the array are required elements, it will be troublesome to test, therefore, I chose the print output method for testing.

 

2016-04-04

BOB

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.