歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android單元測試

Android單元測試

日期:2017/3/1 10:34:28   编辑:Linux編程

在實際開發中,開發Android軟件的過程需要不斷地進行測試。而使用Junit測試框架,側是正規的Android開發的必用技術,在Junit中可以得到組件,可以模擬發送事件和檢測程序處理的正確性。

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="cn.xxx.action“ 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>

<uses-sdk android:minSdkVersion="6" />

<instrumentation android:name="android.test.InstrumentationTestRunner"

android:targetPackage="cn.xxx.action" android:label="Tests for My App" />

</manifest>

上面targetPackage指定的包要和應用的package相同。

第二步:編寫單元測試代碼(選擇要測試的方法,右鍵點擊“Run As”--“Android Junit Test” ):

import android.test.AndroidTestCase;

import android.util.Log;

public class XMLTest extends AndroidTestCase {

public void testSomething() throws Throwable {

Assert.assertTrue(1 + 1 == 3);

}

}

Copyright © Linux教程網 All Rights Reserved