现在的位置: 首页 > 综合 > 正文

android自动化测试之单元测试实例

2013年09月14日 ⁄ 综合 ⁄ 共 4628字 ⁄ 字号 评论关闭

android源代码中每个app下中都自带了一个test用例,下面主要介绍下camra单元测试用例 

在AndroidManifest.xml中标明了测试用例instrumentation函数入口 

Java代码  

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <!-- Copyright (C) 2008 The Android Open Source Project    
  3.     
  4.      Licensed under the Apache License, Version 2.0 (the "License");    
  5.      you may not use this file except in compliance with the License.    
  6.      You may obtain a copy of the License at    
  7.       
  8.           http://www.apache.org/licenses/LICENSE-2.0    
  9.       
  10.      Unless required by applicable law or agreed to in writing, software    
  11.      distributed under the License is distributed on an "AS IS" BASIS,    
  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
  13.      See the License for the specific language governing permissions and    
  14.      limitations under the License.    
  15. -->    
  16.     
  17. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  18.     package="com.android.camera.tests">    
  19.     
  20.     <application>    
  21.         <uses-library android:name="android.test.runner" />    
  22.     </application>    
  23.     
  24.     <instrumentation android:name="CameraLaunchPerformance"    
  25.             android:targetPackage="com.android.camera"    
  26.             android:label="Camera Launch Performance">    
  27.     </instrumentation>    
  28.         
  29.     <instrumentation android:name="com.android.camera.CameraStressTestRunner"    
  30.              android:targetPackage="com.android.camera"    
  31.              android:label="Camera Stress Test InstrumentationRunner">    
  32.     </instrumentation>    
  33.     
  34.     <instrumentation android:name="android.test.InstrumentationTestRunner"    
  35.              android:targetPackage="com.android.camera"    
  36.              android:label="Tests for Camera application."/>    
  37. </manifest>  

   


camera启动性能测试 
Java代码  

Java代码  收藏代码
  1. package com.android.camera;    
  2.     
  3. import android.app.Activity;    
  4. import android.os.Bundle;    
  5. import android.test.LaunchPerformanceBase;    
  6.     
  7. /**  
  8.  * Instrumentation class for Camera launch performance testing.  
  9.  */    
  10. public class CameraLaunchPerformance extends LaunchPerformanceBase {    
  11.     
  12.     public static final String LOG_TAG = "CameraLaunchPerformance";    
  13.     
  14.     public CameraLaunchPerformance() {    
  15.         super();    
  16.     }    
  17.     
  18.     @Override    
  19.     public void onCreate(Bundle arguments) {    
  20.         super.onCreate(arguments);    
  21.     
  22.         mIntent.setClassName(getTargetContext(), "com.android.camera.Camera");    
  23.         start();    
  24.     }    
  25.     
  26.     /**  
  27.      * Calls LaunchApp and finish.  
  28.      */    
  29.     @Override    
  30.     public void onStart() {    
  31.         super.onStart();    
  32.         LaunchApp();    
  33.         finish(Activity.RESULT_OK, mResults);    
  34.     }    
  35. }    



camera拍照压力测试,参数设定为反复拍照100次 

Java代码  

Java代码  收藏代码
  1. package com.android.camera.stress;    
  2.     
  3. import com.android.camera.Camera;    
  4.     
  5. import android.app.Instrumentation;    
  6. import android.test.ActivityInstrumentationTestCase2;    
  7. import android.test.suitebuilder.annotation.LargeTest;    
  8. import android.util.Log;    
  9. import android.view.KeyEvent;    
  10.     
  11. /**  
  12.  * Junit / Instrumentation test case for camera test  
  13.  *  
  14.  * Running the test suite:  
  15.  *  
  16.  * adb shell am instrument \  
  17.  *    -e class com.android.camera.stress.ImageCapture \  
  18.  *    -w com.android.camera.tests/com.android.camera.CameraStressTestRunner  
  19.  *  
  20.  */    
  21.     
  22. public class ImageCapture extends ActivityInstrumentationTestCase2 <Camera> {    
  23.     private String TAG = "ImageCapture";    
  24.     private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 100;    
  25.     private static final int TOTAL_NUMBER_OF_VIDEOCAPTURE = 100;    
  26.     private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 1000;    
  27.     private static final long WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN = 50000//50seconds    
  28.     private static final long WAIT_FOR_PREVIEW = 1000//1 seconds    
  29.     
  30.     public ImageCapture() {    
  31.         super("com.android.camera", Camera.class);    
  32.     }    
  33.     
  34.     @Override    
  35.     protected void setUp() throws Exception {    
  36.         getActivity();    
  37.         super.setUp();    
  38.     }    
  39.     
  40.     @Override    
  41.     protected void tearDown() throws Exception {    
  42.         super.tearDown();    
  43.     }    
  44.     
  45.     @LargeTest    
  46.     public void testImageCapture() {    
  47.         Instrumentation inst = getInstrumentation();    
  48.         try {    
  49.             for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) {    
  50.                 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);    
  51.                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);    
  52.                 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);    
  53.                 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);    
  54.             }    
  55.         } 

抱歉!评论已关闭.