android基礎知識12:android自動化測試06—Instrumentation01例子

轉載處:http://blog.csdn.net/xianming01/article/details/7893391

目前創新互聯公司已為超過千家的企業提供了網站建設、域名、網站空間網站運營、企業網站設計、拜城網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。

下面通過一個簡單的例子來講解Instrumentation的基本測試方法。在這個例子中我們會建立一個簡單的android應用,同時在其上添加Instrumentation測試程序。

    1.首先建立一個android  project,其文件結構最終如下:

android基礎知識12:android自動化測試06—Instrumentation 01 例子

2、布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.hustophone.sample" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <!--用于引入測試庫-->  
  7.         <uses-library android:name="android.test.runner" />  
  8.         <activity android:name=".Sample" android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="3" />  
  16.     <!--表示被測試的目標包與instrumentation的名稱。-->  
  17.     <instrumentation android:targetPackage="com.hustophone.sample"  
  18.         android:name="android.test.InstrumentationTestRunner" />  
  19. </manifest>  
3、被測程序Sample類

[java] view plaincopy
  1. package com.hustophone.sample;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;   
  8.   
  9. public class Sample extends Activity {  
  10.   
  11.     private TextView myText = null;  
  12.     private Button button = null;   
  13.   
  14.     /** Called when the activity is first created. */  
  15.   
  16.     @Override  
  17.   
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.   
  20.         super.onCreate(savedInstanceState);  
  21.   
  22.         setContentView(R.layout.main);  
  23.   
  24.         myText = (TextView) findViewById(R.id.text1);  
  25.   
  26.         button = (Button) findViewById(R.id.button1);  
  27.   
  28.         button.setOnClickListener(new OnClickListener() {  
  29.    
  30.   
  31.             @Override  
  32.   
  33.             public void onClick(View arg0) {  
  34.   
  35.                 // TODO Auto-generated method stub  
  36.   
  37.                 myText.setText("Hello Android");  
  38.   
  39.             }  
  40.   
  41.         });  
  42.   
  43.     }   
  44.   
  45.     public int add(int i, int j) {  
  46.   
  47.         // TODO Auto-generated method stub  
  48.   
  49.         return (i + j);  
  50.   
  51.     }  
  52.   
  53. }  
        這個程序的功能比較簡單,點擊按鈕之后,TextView的內容由Hello變為Hello Android.同時,在這個類中,我還寫了一個簡單的方法,沒有被調用,僅供測試而已。
4、測試類SampleTest
      通常可以將測試程序作為另一個android應用程序。但是這里我們為了操作方便,寫在了一個應用里面了。
[java] view plaincopy
  1. package com.hustophone.sample.test;  
  2. import com.hustophone.sample.R;  
  3. import com.hustophone.sample.Sample;  
  4. import android.content.Intent;  
  5. import android.os.SystemClock;  
  6. import android.test.InstrumentationTestCase;  
  7. import android.util.Log;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;   
  10.   
  11. public class SampleTest extends InstrumentationTestCase {  
  12.   
  13.     private Sample sample = null;  
  14.     private Button button = null;  
  15.     private TextView text = null;  
  16.   
  17.     /* 
  18.  
  19.      * 初始設置 
  20.      * 
  21.      * @see junit.framework.TestCase#setUp() 
  22.      */  
  23.   
  24.     @Override  
  25.   
  26.     protected void setUp()  {  
  27.   
  28.         try {  
  29.             super.setUp();  
  30.         } catch (Exception e) {  
  31.   
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         }  
  35.   
  36.         Intent intent = new Intent();  
  37.         intent.setClassName("com.hustophone.sample", Sample.class.getName());  
  38.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  39.         sample = (Sample) getInstrumentation().startActivitySync(intent);  
  40.         text = (TextView) sample.findViewById(R.id.text1);  
  41.         button = (Button) sample.findViewById(R.id.button1);  
  42.   
  43.     }  
  44.   
  45.     /* 
  46.  
  47.      * 垃圾清理與資源回收 
  48.  
  49.      * 
  50.      * @see android.test.InstrumentationTestCase#tearDown() 
  51.  
  52.      */  
  53.   
  54.     @Override  
  55.   
  56.     protected void tearDown()  {  
  57.   
  58.         sample.finish();  
  59.   
  60.         try {  
  61.   
  62.             super.tearDown();  
  63.   
  64.         } catch (Exception e) {  
  65.   
  66.             // TODO Auto-generated catch block  
  67.   
  68.             e.printStackTrace();  
  69.   
  70.         }  
  71.   
  72.     }  
  73.   
  74.    
  75.   
  76.     /* 
  77.  
  78.      * 活動功能測試 
  79.  
  80.      */  
  81.   
  82. public void testActivity() throws Exception {  
  83.   
  84. Log.v("testActivity", "test the Activity");  
  85.   
  86.         SystemClock.sleep(1500);  
  87.   
  88.         getInstrumentation().runOnMainSync(new PerformClick(button));  
  89.   
  90.         SystemClock.sleep(3000);  
  91.   
  92.         assertEquals("Hello Android", text.getText().toString());  
  93.   
  94.     }   
  95.   
  96.     /* 
  97.  
  98.      * 模擬按鈕點擊的接口 
  99.  
  100.      */  
  101.   
  102.     private class PerformClick implements Runnable {  
  103.   
  104.         Button btn;   
  105.   
  106.         public PerformClick(Button button) {  
  107.   
  108.             btn = button;  
  109.   
  110.         }   
  111.   
  112.         public void run() {  
  113.   
  114.             btn.performClick();  
  115.   
  116.         }  
  117.   
  118.     }   
  119.   
  120.     /* 
  121.  
  122.      * 測試類中的方法 
  123.  
  124.      */  
  125.   
  126.     public void testAdd() throws Exception{  
  127.   
  128.         String tag = "testAdd";  
  129.   
  130.         Log.v(tag, "test the method");  
  131.   
  132.         int test = sample.add(1, 1);  
  133.   
  134.         assertEquals(2, test);  
  135.   
  136.     }   
  137.   
  138. }  
下面來簡單講解一下代碼:
  setUp()和tearDown()都是受保護的方法,通過繼承可以覆寫這些方法。
  在android Developer中有如下的解釋
[java] view plaincopy
  1. protected void setUp ()  
  2.   
  3. Since: API Level 3  
  4.   
  5. Sets up the fixture, for example, open a network connection. This method is called before a test is executed.  
  6.   
  7. protected void tearDown ()  
  8.   
  9. Since: API Level 3  
  10.   
  11. Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.   
 setUp ()用來初始設置,如啟動一個Activity,初始化資源等。
    tearDown ()則用來垃圾清理與資源回收。
 在testActivity()這個測試方法中,我模擬了一個按鈕點擊事件,然后來判斷程序是否按照預期的執行。在這里PerformClick這個方法繼承了Runnable接口,通過新的線程來執行模擬事件,之所以這么做,是因為如果直接在UI線程中運行可能會阻滯UI線程。
<uses-library android:name="android.test.runner" />用于引入測試庫
<instrumentation android:targetPackage="com.hustophone.sample"
       android:name="android.test.InstrumentationTestRunner" />
   表示被測試的目標包與instrumentation的名稱。 
   經過以上步驟,下面可以開始測試了。測試方法也有以下幾種,下面介紹兩個常用的方法: 
 (1) 用Eclipse集成的JUnit工具
     在Eclipse中選擇工程Sample,單擊右鍵,在Run as子菜單選項中選擇Android JUnit Test
android基礎知識12:android自動化測試06—Instrumentation 01 例子
同時可以通過LogCat工具查看信息
android基礎知識12:android自動化測試06—Instrumentation 01 例子
(2) 通過模擬器運行單元測試
     點擊模擬器界面的Dev Tools菜單
android基礎知識12:android自動化測試06—Instrumentation 01 例子
再點擊Instrumentation選項,進入Instrumentation菜單
android基礎知識12:android自動化測試06—Instrumentation 01 例子
android基礎知識12:android自動化測試06—Instrumentation 01 例子
這里有一個InstrumentationTestRunner,它是測試的入口,點擊這個選項,就可以自動運行我們的測試代碼。以下為運行結果:
按鈕點擊前
android基礎知識12:android自動化測試06—Instrumentation 01 例子
按鈕點擊后
android基礎知識12:android自動化測試06—Instrumentation 01 例子

         至此,一個簡單的測試過程結束了。當然,android的測試內容還有很多,也有比較復雜的,我會在以后的學習過程中繼續分享我的體會。好了,今天就到這里吧!


分享標題:android基礎知識12:android自動化測試06—Instrumentation01例子
文章源于:http://m.kartarina.com/article38/pihjsp.html

成都網站建設公司_創新互聯,為您提供小程序開發動態網站商城網站移動網站建設域名注冊全網營銷推廣

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 成人无码精品一区二区三区| 无码天堂亚洲国产AV| 日本爆乳j罩杯无码视频| 夜夜添无码试看一区二区三区 | 久久亚洲精品中文字幕无码| 免费A级毛片无码A∨免费| 亚洲国产精品无码成人片久久| 久久精品无码免费不卡| 国产精品亚洲αv天堂无码| 亚洲熟妇少妇任你躁在线观看无码| 久久久久久久人妻无码中文字幕爆| 无码熟妇αⅴ人妻又粗又大| 天堂一区人妻无码| 麻豆精品无码国产在线果冻| 无码毛片内射白浆视频| 国内精品久久人妻无码不卡| 中文字幕在线无码一区二区三区| 精品无码中文视频在线观看| 国产精品亚洲а∨无码播放麻豆| 在线看片无码永久免费aⅴ| 无码专区国产无套粉嫩白浆内射| 亚洲午夜福利精品无码| 91嫩草国产在线无码观看| 成人免费一区二区无码视频| 人妻丰满熟妇aⅴ无码| 精品国精品无码自拍自在线| 无码国产精品久久一区免费| a级毛片无码免费真人久久| 国产成人无码a区在线观看视频免费 | 影院无码人妻精品一区二区| 免费a级毛片无码av| 亚洲国产精品无码久久青草 | 无码性午夜视频在线观看| 亚洲AV无码乱码国产麻豆| 无码AV片在线观看免费| 免费A级毛片无码A∨男男| 久久久久亚洲av无码专区蜜芽| 日韩精品无码熟人妻视频| 亚洲AV无码一区二区二三区入口| 亚洲午夜无码片在线观看影院猛 | 国产成人无码av在线播放不卡 |