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

首頁(yè) > 網(wǎng)站 > WEB開(kāi)發(fā) > 正文

Android 單元測(cè)試過(guò)程詳解

2024-04-27 13:53:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

android源代碼中每個(gè)app下中都自帶了一個(gè)test用例,下面主要介紹下camra單元測(cè)試用例 在AndroidManifest.xml中標(biāo)明了測(cè)試用例instrumentation函數(shù)入口

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2008 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License at            http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.camera.tests">    <application>        <uses-library android:name="android.test.runner" />    </application>    <instrumentation android:name="CameraLaunchPerformance"            android:targetPackage="com.android.camera"            android:label="Camera Launch Performance">    </instrumentation>        <instrumentation android:name="com.android.camera.CameraStressTestRunner"             android:targetPackage="com.android.camera"             android:label="Camera Stress Test InstrumentationRunner">    </instrumentation>    <instrumentation android:name="android.test.InstrumentationTestRunner"             android:targetPackage="com.android.camera"             android:label="Tests for Camera application."/></manifest> 

 

camera啟動(dòng)性能測(cè)試 

package com.android.camera;import android.app.Activity;import android.os.Bundle;import android.test.LaunchPerformanceBase;/** * Instrumentation class for Camera launch performance testing. */public class CameraLaunchPerformance extends LaunchPerformanceBase {    public static final String LOG_TAG = "CameraLaunchPerformance";    public CameraLaunchPerformance() {        super();    }    @Override    public void onCreate(Bundle arguments) {        super.onCreate(arguments);        mIntent.setClassName(getTargetContext(), "com.android.camera.Camera");        start();    }    /**     * Calls LaunchApp and finish.     */    @Override    public void onStart() {        super.onStart();        LaunchApp();        finish(Activity.RESULT_OK, mResults);    }}

 

camera拍照壓力測(cè)試,參數(shù)設(shè)定為反復(fù)拍照100次 

package com.android.camera.stress;import com.android.camera.Camera;import android.app.Instrumentation;import android.test.ActivityInstrumentationTestCase2;import android.test.suitebuilder.annotation.LargeTest;import android.util.Log;import android.view.KeyEvent;/** * Junit / Instrumentation test case for camera test * * Running the test suite: * * adb shell am instrument / *    -e class com.android.camera.stress.ImageCapture / *    -w com.android.camera.tests/com.android.camera.CameraStressTestRunner * */public class ImageCapture extends ActivityInstrumentationTestCase2 <Camera> {    private String TAG = "ImageCapture";    private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 100;    private static final int TOTAL_NUMBER_OF_VIDEOCAPTURE = 100;    private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 1000;    private static final long WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN = 50000; //50seconds    private static final long WAIT_FOR_PREVIEW = 1000; //1 seconds    public ImageCapture() {        super("com.android.camera", Camera.class);    }    @Override    protected void setUp() throws Exception {        getActivity();        super.setUp();    }    @Override    protected void tearDown() throws Exception {        super.tearDown();    }    @LargeTest    public void testImageCapture() {        Instrumentation inst = getInstrumentation();        try {            for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) {                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);            }        } catch (Exception e) {            Log.v(TAG, e.toString());        }            assertTrue("testImageCapture", true);    }    @LargeTest    public void testVideoCapture() {        Instrumentation inst = getInstrumentation();        //Switch to the video mode        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);        try {            for (int i = 0; i < TOTAL_NUMBER_OF_VIDEOCAPTURE; i++) {                Thread.sleep(WAIT_FOR_PREVIEW);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);                //record an video                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);                Thread.sleep(WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);                Thread.sleep(WAIT_FOR_PREVIEW);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);            }        } catch (Exception e) {            Log.v(TAG, e.toString());        }            assertTrue("testVideoCapture", true);    }}

 

 

如果想在android里面做單元測(cè)試,有兩條基本的路子可行。 

第一, 就是java程序員最為熟悉和常用的JUnit, 但是由于目前android sdk (version 1.1)中只是提供了stubbed methods/classes,沒(méi)有具體的實(shí)現(xiàn)代碼,所以如果用JUnit的話,我們需要在運(yùn)行單元測(cè)試時(shí),一定要用JDK來(lái)運(yùn)行,利用java命令來(lái)啟動(dòng)JUnit的某個(gè)Runner。如果是用Eclipse的話,可以在Run Configuration里新建一個(gè)JUnit。但是一定要記得在Classpath選項(xiàng)卡里將Bootstrap Entries中的Android Library改成JRE,并且添加junit.jar。具體的設(shè)置可以參考:http://developer.android.com/guide /appendix/faq/troubleshooting.html#addjunit。而且,更為遺憾的是,這種方法運(yùn)行的JUnit運(yùn)行在JDK之上的,而不是android,所以,只能測(cè)試一些和android無(wú)關(guān)的東西,比如業(yè)務(wù)邏輯,數(shù)據(jù)封裝,數(shù)值計(jì)算等等。并不能測(cè)試android api。 

第二, 采用Instrumentation. Android單元測(cè)試的主入口是InstrumentationTestRunner。它相當(dāng)于JUnit當(dāng)中TestRunner的作用。你可以將 Instrumentation理解為一種沒(méi)有圖形界面的,具有啟動(dòng)能力的,用于監(jiān)控其他類(lèi)(用Target Package聲明)的工具類(lèi)。任何想成為Instrumentation的類(lèi)必須繼承android.app.Instrumentation。 

下面通過(guò)一個(gè)實(shí)例來(lái)看一下如何通過(guò)Instrumentation來(lái)做單元測(cè)試。 

Step 1.首先編寫(xiě)需要測(cè)試的activity: 

import android.app.Activity;import android.os.Bundle;public class AndroidUT extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }       public int add(int a, int b)    {        return a + b;    }}

 

 

Step 2. 

接下來(lái)編寫(xiě)測(cè)試類(lèi),其中主要來(lái)測(cè)試add()方法。我們?cè)诋?dāng)前代碼目錄下,在新建一個(gè)文件夾,命名為test,并在里面新建了包c(diǎn)om.android.ut.test。然后往里面新增加一個(gè)class.具體如下: 

import com.android.ut.AndroidUT;import android.test.ActivityInstrumentationTestCase;public class TestApp extends ActivityInstrumentationTestCase<AndroidUT> {       public TestApp()    {        super("com.android.ut", AndroidUT.class);    }       public void testSum()    {        assertEquals(5, getActivity().add(2, 3));    }   }

 

Step 3.最后一步就是要改一下Manifest文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.ut"      android:versionCode="1"      android:versionName="1.0.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".AndroidUT"                  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:targetPackage="com.android.ut" android:name="android.test.InstrumentationTestRunner" android:label="Test Unit Tests"></instrumentation></manifest>

 

需要注意的是,在這里面我加上了: 

<uses-library android:name="android.test.runner" /> 

以及: 

<instrumentation android:targetPackage="com.android.ut" 		 android:name="android.test.InstrumentationTestRunner" 		 android:label="Test Unit Tests"></instrumentation>  

 

 

Step 4.運(yùn)行 

首先通過(guò)模擬器運(yùn)行一下AndroidUT,然后在命令行終端中運(yùn)行 

adb shell am instrument -e class com.android.ut.test.TestApp -wcom.android.ut/android.test.InstrumentationTestRunner  

 

 

這樣你就可以看到測(cè)試結(jié)果了

# am instrument -e class com.cn.test.TestApp -w com.cn/android.test.InstrumentationTestRunnercom.cn.test.TestApp:..Test results for InstrumentationTestRunner=..Time: 2.866OK (2 tests)

 

后臺(tái)測(cè)試log日志信息 
D/AndroidRuntime(  941): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<D/AndroidRuntime(  941): CheckJNI is OND/AndroidRuntime(  941): --- registering native functions ---D/FileBackupHelper_native(  941): register_android_backup_FileBackupHelperD/ActivityManager(  581): Uninstalling process com.cnI/ActivityManager(  581): Start proc com.cn for added application com.cn: pid=948 uid=10013 gids={}I/TestRunner(  948): started: testSum(com.cn.test.TestApp)      //啟動(dòng)add()測(cè)試方法I/ActivityManager(  581): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.cn/.AndroidUT }I/ActivityManager(  581): Displayed activity com.cn/.AndroidUT: 645 ms (total 645 ms)I/TestRunner(  948): finished: testSum(com.cn.test.TestApp)I/TestRunner(  948): passed: testSum(com.cn.test.TestApp)I/TestRunner(  948): started: testActivityTestCaseSetUpProperly(com.cn.test.TestApp)I/ActivityManager(  581): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.cn/.AndroidUT }I/ActivityManager(  581): Displayed activity com.cn/.AndroidUT: 412 ms (total 412 ms)I/TestRunner(  948): finished: testActivityTestCaseSetUpProperly(com.cn.test.TestApp)I/TestRunner(  948): passed: testActivityTestCaseSetUpProperly(com.cn.test.TestApp)D/ActivityManager(  581): Uninstalling process com.cnD/ActivityManager(  581): Force removing process ProcessRecord{43851fa0 948:com.cn/10013} (com.cn/10013)D/AndroidRuntime(  941): Shutting down VM

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 河池市| 昭觉县| 涿鹿县| 武隆县| 淮北市| 阜宁县| 黔江区| 秦安县| 稷山县| 乌海市| 宁城县| 花莲市| 清徐县| 顺平县| 太保市| 临武县| 桦南县| 图木舒克市| 新田县| 科尔| 光泽县| 彰化市| 滨海县| 寿光市| 西林县| 班玛县| 呼图壁县| 阿尔山市| 琼结县| 周至县| 南和县| 志丹县| 庐江县| 新疆| 元氏县| 城固县| 崇信县| 涡阳县| 涡阳县| 石棉县| 陕西省|