Unit test Analysis _android in Android application development

Source: Internet
Author: User
Tags assert

This article is mainly to share with you how the Android application development process for unit testing, the individual in the process of doing the project, the unit test is necessary to ensure that we write the correctness of the program. Let's take a look at the unit test and the unit test.
Unit testing (also known as module testing) is a test work for correctness testing of program modules (the smallest unit of software design). The program unit is the smallest testable part of the application. In procedural programming, a unit is a single program, function, process, etc. for object-oriented programming, the smallest unit is the method, including the method in the base class (superclass), abstract class, or derived class (subclass). Unit tests are done by programmers themselves and ultimately by programmers themselves. It can be said that programmers have a responsibility to write functional code, but also have the responsibility for their own code to write unit tests. The unit test is performed to demonstrate that the code behaves as we expect it to.
In fact, we are doing unit tests every day. You write a function, in addition to very simple, always have to perform, to see if the function is normal, and sometimes to find ways to output some data, such as pop-up information window What, this is also unit test, this unit test is called Temporary unit test. Software that only carries out the staging unit tests, testing for code is very incomplete, code coverage is more than 70% difficult, the uncovered code may leave a large number of small errors, these errors will also affect each other, when the bug exposed difficult to debug, significantly improve the late test and maintenance costs, Also reduce the competitiveness of developers. It can be said that adequate unit testing is the only way to improve the quality of software and reduce the cost of development.
For programmers, if you get into the habit of doing unit tests on your own code, not only can you write high quality code, but you can also improve your programming level.
To perform adequate unit testing, you should write the test code specifically and isolate it from the product code. In my opinion, the simpler approach is to create a corresponding test project for the product engineering, set up a corresponding test class for each class, and set up a test function for each function (except very simply).
There are two ways to do unit testing in Android application development, and the following are the methods for each of these two unit tests:

Unit Test Mode one : [Based on the application of the engineering Unit Test]

According to the above program code structure diagram, for example, we want to test the correctness of a method in the Junitservice.java in the package Com.andyidea.demo package. For example, there is an Add method in the object that computes the and of the two integer data

Package Com.andyidea.demo; 
 
public class Junitservice {public 
   
  int ' Add (int a, int b) {return 
    a+b 
  } 
 
} 

Then we write the unit test code in Myjnuittest.java, [Note: The test class must inherit the Androidtestcase class] to verify that the program results are the same as we expect.

Package com.andyidea.jnuit; 
 
Import Junit.framework.Assert; 
Import Android.test.AndroidTestCase; 
 
Import Com.andyidea.demo.JUnitService; 
 
public class Myjnuittest extends Androidtestcase {public 
   
  void Testadd () throws exception{junitservice 
    js = new Junitservice (); 
    int sum = Js.add (1, 2); 
     
    Assert.assertequals (3, sum); 
  } 
 
} 

At the same time we also need to configure in the manifest.xml manifest configuration file: The red flag out where the need to configure.

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.andyidea.demo "android:versioncode=" 1 "android:versionname=" 1.0 "> <span style= "Color: #ff0000;"  
    > <!--representative introduces some of the dependency libraries in the Unit test framework--> &LT;USES-SDK android:minsdkversion= "8"/></span> <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <uses-library android:name= " Android.test.runner "/> <activity android:name=" Com.andyidea.demo.JUnitTestDemoActivity "Android: Label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/ > <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> < /activity> </application> <span style= "color: #ff0000;" 
     The > <!--represents the start-up device for the configuration unit test framework, which has several classes to choose from, which we normally use above. TaThe rgetpackage is the same as the package above, representing the unit test framework and the current application being in the same process--> <instrumentation android:name= "Android.test.Instrumenta Tiontestrunner "android:label=" Test for my App "android:targetpackage=" Com.andyidea.demo "/></span>" 
 ;/manifest>

Finally, open the Outline window in Eclipse, which displays all the methods of the Unit test class and then wants to test which method, the right mouse button on which test method, select Run as, and then select the Android JUnit test and, if it is normal, the following:

If there are exceptions or errors, the following conditions occur:

Unit Test Mode two:
In fact, this approach is simpler, this way is to create a separate unit test engineering to test. That is, create an Android test project, and then select the project that requires unit testing OK, this way to do unit test without the above configuration, in fact, the creation of this project, the default has been configured for us, how to test and the way above, It is not explained in detail here.

The above is for the Android Application Development Unit test in the detailed introduction, I hope to help you learn.

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.