欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程之單元測(cè)試實(shí)例分析

 更新時(shí)間:2015年11月17日 11:46:27   作者:lee0oo0  
這篇文章主要介紹了Android編程之單元測(cè)試,結(jié)合具體實(shí)例分析了Android單元測(cè)試的具體實(shí)現(xiàn)步驟與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android編程之單元測(cè)試用法。分享給大家供大家參考,具體如下:

在實(shí)際開發(fā)中,開發(fā)android軟件的過程需要不斷地進(jìn)行測(cè)試。使用Junint測(cè)試框架,是正規(guī)Android開發(fā)的必用技術(shù),在Junint中可以得到組件,可以模擬發(fā)送事件和檢測(cè)程序處理的正確性。單元測(cè)試是嵌入到項(xiàng)目中;也可以作為一個(gè)單獨(dú)的項(xiàng)目爭(zhēng)對(duì)某個(gè)具體項(xiàng)目進(jìn)行測(cè)試。

第一步:首先在AndroidManifest.xml中加入下面紅色代碼:

<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"/> 
 ***上面targetPackage指定的包要和應(yīng)用的package相同。

第二步:編寫單元測(cè)試代碼,一般對(duì)將要測(cè)試的方法命名testXXX。需要測(cè)試的時(shí)候選擇大綱(Outline視圖)選擇測(cè)試的方法右鍵點(diǎn)擊,選擇"Run As" - "Android Junit Test"。

例:

項(xiàng)目結(jié)構(gòu):

AndroidManifest.xml文件:

<?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="15" />
 <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> 

定義測(cè)試的兩個(gè)方法:

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

一般繼承的是AndroidTestCase,測(cè)試的時(shí)候就是測(cè)試這兩個(gè)方法,如果在對(duì)應(yīng)方法中選擇"Run As" - "Android Junit Test"時(shí)出錯(cuò),可以右鍵Test類,選擇"Run as" - "Run Configurations",在 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);
 }
} 

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論