android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation01例子

轉(zhuǎn)載處:http://blog.csdn.net/xianming01/article/details/7893391

目前創(chuàng)新互聯(lián)公司已為超過(guò)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、拜城網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

下面通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)講解Instrumentation的基本測(cè)試方法。在這個(gè)例子中我們會(huì)建立一個(gè)簡(jiǎn)單的android應(yīng)用,同時(shí)在其上添加Instrumentation測(cè)試程序。

    1.首先建立一個(gè)android  project,其文件結(jié)構(gòu)最終如下:

android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試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.         <!--用于引入測(cè)試庫(kù)-->  
  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.     <!--表示被測(cè)試的目標(biāo)包與instrumentation的名稱(chēng)。-->  
  17.     <instrumentation android:targetPackage="com.hustophone.sample"  
  18.         android:name="android.test.InstrumentationTestRunner" />  
  19. </manifest>  
3、被測(cè)程序Sample類(lèi)

[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. }  
        這個(gè)程序的功能比較簡(jiǎn)單,點(diǎn)擊按鈕之后,TextView的內(nèi)容由Hello變?yōu)镠ello Android.同時(shí),在這個(gè)類(lèi)中,我還寫(xiě)了一個(gè)簡(jiǎn)單的方法,沒(méi)有被調(diào)用,僅供測(cè)試而已。
4、測(cè)試類(lèi)SampleTest
      通常可以將測(cè)試程序作為另一個(gè)android應(yīng)用程序。但是這里我們?yōu)榱瞬僮鞣奖?,?xiě)在了一個(gè)應(yīng)用里面了。
[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.      * 初始設(shè)置 
  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.      * 活動(dòng)功能測(cè)試 
  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.      * 模擬按鈕點(diǎn)擊的接口 
  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.      * 測(cè)試類(lèi)中的方法 
  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. }  
下面來(lái)簡(jiǎn)單講解一下代碼:
  setUp()和tearDown()都是受保護(hù)的方法,通過(guò)繼承可以覆寫(xiě)這些方法。
  在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 ()用來(lái)初始設(shè)置,如啟動(dòng)一個(gè)Activity,初始化資源等。
    tearDown ()則用來(lái)垃圾清理與資源回收。
 在testActivity()這個(gè)測(cè)試方法中,我模擬了一個(gè)按鈕點(diǎn)擊事件,然后來(lái)判斷程序是否按照預(yù)期的執(zhí)行。在這里PerformClick這個(gè)方法繼承了Runnable接口,通過(guò)新的線程來(lái)執(zhí)行模擬事件,之所以這么做,是因?yàn)槿绻苯釉赨I線程中運(yùn)行可能會(huì)阻滯UI線程。
<uses-library android:name="android.test.runner" />用于引入測(cè)試庫(kù)
<instrumentation android:targetPackage="com.hustophone.sample"
       android:name="android.test.InstrumentationTestRunner" />
   表示被測(cè)試的目標(biāo)包與instrumentation的名稱(chēng)。 
   經(jīng)過(guò)以上步驟,下面可以開(kāi)始測(cè)試了。測(cè)試方法也有以下幾種,下面介紹兩個(gè)常用的方法: 
 (1) 用Eclipse集成的JUnit工具
     在Eclipse中選擇工程Sample,單擊右鍵,在Run as子菜單選項(xiàng)中選擇Android JUnit Test
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
同時(shí)可以通過(guò)LogCat工具查看信息
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
(2) 通過(guò)模擬器運(yùn)行單元測(cè)試
     點(diǎn)擊模擬器界面的Dev Tools菜單
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
再點(diǎn)擊Instrumentation選項(xiàng),進(jìn)入Instrumentation菜單
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
這里有一個(gè)InstrumentationTestRunner,它是測(cè)試的入口,點(diǎn)擊這個(gè)選項(xiàng),就可以自動(dòng)運(yùn)行我們的測(cè)試代碼。以下為運(yùn)行結(jié)果:
按鈕點(diǎn)擊前
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子
按鈕點(diǎn)擊后
android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation 01 例子

         至此,一個(gè)簡(jiǎn)單的測(cè)試過(guò)程結(jié)束了。當(dāng)然,android的測(cè)試內(nèi)容還有很多,也有比較復(fù)雜的,我會(huì)在以后的學(xué)習(xí)過(guò)程中繼續(xù)分享我的體會(huì)。好了,今天就到這里吧!


分享標(biāo)題:android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation01例子
文章源于:http://muchs.cn/article38/pihjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、全網(wǎng)營(yíng)銷(xiāo)推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作