国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程之單元測試實例分析

2020-04-11 11:17:00
字體:
來源:轉載
供稿:網友

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

在實際開發中,開發android軟件的過程需要不斷地進行測試。使用Junint測試框架,是正規Android開發的必用技術,在Junint中可以得到組件,可以模擬發送事件和檢測程序處理的正確性。單元測試是嵌入到項目中;也可以作為一個單獨的項目爭對某個具體項目進行測試。

第一步:首先在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指定的包要和應用的package相同。

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

例:

項目結構:

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> 

定義測試的兩個方法:

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,測試的時候就是測試這兩個方法,如果在對應方法中選擇"Run As" - "Android Junit Test"時出錯,可以右鍵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); }} 

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 固安县| 延庆县| 桦南县| 塘沽区| 金昌市| 恩施市| 乌恰县| 南召县| 射阳县| 绥棱县| 福鼎市| 西乡县| 海林市| 宜兰市| 通道| 新乐市| 马公市| 玉门市| 诸暨市| 石楼县| 连云港市| 东阳市| 定日县| 双桥区| 西乌珠穆沁旗| 筠连县| 新绛县| 砚山县| 清徐县| 苍南县| 新津县| 武宣县| 远安县| 茂名市| 荣成市| 浦江县| 巍山| 绍兴县| 东台市| 平安县| 新营市|