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

首頁 > 系統(tǒng) > Android > 正文

Android學習筆記之應用單元測試實例分析

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

本文實例講述了Android學習筆記之應用單元測試。分享給大家供大家參考,具體如下:

第一步:在AndroidManifest.xml中加入如下兩段代碼:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.pccw"   android:versionCode="1"   android:versionName="1.0">  <uses-sdk android:minSdkVersion="8" />  <application android:icon="@drawable/icon" android:label="@string/app_name">   <activity android:name=".MainActivity"      android:label="@string/app_name">    <intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" />    </intent-filter>   </activity>   <!―添加代碼1-->   <uses-library android:name="android.test.runner"/> </application>  <!―添加代碼2-->   <instrumentation android:name="android.test.InstrumentationTestRunner"    android:targetPackage="com.pccw" android:label="aaa"/> </manifest>

1. <uses-library android:name="android.test.runner"/>代表把單元測試框架中的一些依賴庫引入進來

2. <instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表配置單元測試框架的啟動裝置,啟動裝置有好幾個類,可以選擇,一般情況下我們使用上面這個。

3. targetPackage與上面的package相同,代表單元測試框架和當前應用是處于同一個進程中

第二步:編寫業(yè)務邏輯,即需要被測試的模塊

public class PersonService {  public void save(String name){   String sub = name.substring(6);  }  public int add(int a, int b){   return a+b;  } }

第三步:編寫單元測試代碼

public class PersonServiceTest extends AndroidTestCase {  public void testSave() throws Exception {   PersonService service = new PersonService();   service.save(null);  }  public void testAdd() throws Exception {   PersonService service = new PersonService();   int result = service.add(1, 2);   Assert.assertEquals(3, result);  } }

第四步:打開eclipse中的outline窗口,其中會顯示單元測試類的所有的方法

然后想要測試哪個方法,則在哪個測試方法上右鍵鼠標,選擇Run As,然后再選擇Android JUnit Test即可,如果有異常或者錯誤,則會出現如下情況:

如果是正常的,則會如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 威宁| 蒙山县| 谢通门县| 南京市| 皮山县| 济源市| 和平区| 璧山县| 都江堰市| 于田县| 普格县| 鱼台县| 英山县| 东平县| 左贡县| 防城港市| 句容市| 镇远县| 华蓥市| 古交市| 邳州市| 嘉荫县| 舟山市| 金寨县| 兴城市| 沙河市| 临潭县| 德江县| 新巴尔虎左旗| 麟游县| 炉霍县| 赞皇县| 丰都县| 托克托县| 体育| 阿拉善右旗| 芦溪县| 梁山县| 卓尼县| 平阴县| 将乐县|