Example analysis of unit test for Android programming _android

Source: Internet
Author: User
Tags assert

This example describes the unit test usage of Android programming. Share to everyone for your reference, specific as follows:

In actual development, the process of developing the Android software needs to be tested continuously. Using the Junint test framework, is the regular Android development of the required technology, in the junint can get components, can simulate the sending event and the correctness of the detection program processing. A unit test is embedded in a project, or it can be tested as a separate project for a specific project.

The first step: first in the Androidmanifest.xml to add the following red code:

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.lee0000.test" android:versioncode= "1" android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<uses-library android:name= "Android.test.runner"/>
</application>
<USE-SDK android:minsdkversion= "6"/>
<instrumentation android:name= "Android.test.instrumentationTestRunner" android:targetpackage= " Com.lee0000.test "android:label=" Tests "/>
* * * above Targetpackage The specified package will be the same as the applied package.

Step Two: Write unit test code, and generally name testxxx for the method that will be tested. When you need to test choose the outline (outline view) Select the test method right click and choose Run as-"Android Junit Test".

Cases:

Project structure:

Androidmanifest.xml file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" com.lee0000.test "
 android:versioncode=" 1 "
 android:versionname=" 1.0 ">
 < Uses-sdk android:minsdkversion=/>
 <application
  android:icon= "@drawable/ic_launcher"
  Android:label= "@string/app_name" >
  <activity
   android:name= ". Juinttestactivity "
   android:label=" @string/app_name ">
   <intent-filter>
    <action Android : Name= "Android.intent.action.MAIN"/>
    <category android:name= "Android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
 <uses-library android:name= "Android.test.runner"/ >  
 </application>
 <instrumentation 
  android:name= " Android.test.InstrumentationTestRunner "
  android:targetpackage=" Com.lee0000.test "android:label=" Tests " > 
</manifest> 

Two ways to define tests:

public class TestClass {public
 void str (String s) {
  System.out.println (s.substring (6));
 }
 public int Add (int a,int b) {return
  a+b;
 }
} 

Generally inherited is androidtestcase, testing is the time to test these two methods, if the corresponding method to select "Run as"-"Android Junit Test" error, you can right-click Test class, select "Run as"-"run Configurations ", select in instrumentation Runner:

Import Junit.framework.Assert;
Import Android.test.AndroidTestCase;
public class Test extends androidtestcase{public
 void Teststr () throws exception{
  TestClass tc = new TestClass ( );
  TC.STR ("null");
 }
 public void Testadd () {
  TestClass TC = new TestClass ();
  int t = Tc.add (1, 2);
  Assert.assertequals (3, T);
 }
} 

I hope this article will help you with the Android program.

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.