轉載處:http://blog.csdn.net/xianming01/article/details/7893391
目前創新互聯公司已為超過千家的企業提供了網站建設、域名、網站空間、網站運營、企業網站設計、拜城網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
下面通過一個簡單的例子來講解Instrumentation的基本測試方法。在這個例子中我們會建立一個簡單的android應用,同時在其上添加Instrumentation測試程序。
1.首先建立一個android project,其文件結構最終如下:

2、布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.hustophone.sample" android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <!--用于引入測試庫-->
- <uses-library android:name="android.test.runner" />
- <activity android:name=".Sample" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <!--表示被測試的目標包與instrumentation的名稱。-->
- <instrumentation android:targetPackage="com.hustophone.sample"
- android:name="android.test.InstrumentationTestRunner" />
- </manifest>
3、被測程序Sample類
- package com.hustophone.sample;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class Sample extends Activity {
-
- private TextView myText = null;
- private Button button = null;
-
- /** Called when the activity is first created. */
-
- @Override
-
- public void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.main);
-
- myText = (TextView) findViewById(R.id.text1);
-
- button = (Button) findViewById(R.id.button1);
-
- button.setOnClickListener(new OnClickListener() {
-
-
- @Override
-
- public void onClick(View arg0) {
-
- // TODO Auto-generated method stub
-
- myText.setText("Hello Android");
-
- }
-
- });
-
- }
-
- public int add(int i, int j) {
-
- // TODO Auto-generated method stub
-
- return (i + j);
-
- }
-
- }
這個程序的功能比較簡單,點擊按鈕之后,TextView的內容由Hello變為Hello Android.同時,在這個類中,我還寫了一個簡單的方法,沒有被調用,僅供測試而已。
4、測試類SampleTest
通常可以將測試程序作為另一個android應用程序。但是這里我們為了操作方便,寫在了一個應用里面了。
下面來簡單講解一下代碼:
setUp()和tearDown()都是受保護的方法,通過繼承可以覆寫這些方法。
在android Developer中有如下的解釋
- protected void setUp ()
-
- Since: API Level 3
-
- Sets up the fixture, for example, open a network connection. This method is called before a test is executed.
-
- protected void tearDown ()
-
- Since: API Level 3
-
- 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
同時可以通過LogCat工具查看信息
(2) 通過模擬器運行單元測試
點擊模擬器界面的Dev Tools菜單
再點擊Instrumentation選項,進入Instrumentation菜單
這里有一個InstrumentationTestRunner,它是測試的入口,點擊這個選項,就可以自動運行我們的測試代碼。以下為運行結果:
按鈕點擊前
按鈕點擊后
至此,一個簡單的測試過程結束了。當然,android的測試內容還有很多,也有比較復雜的,我會在以后的學習過程中繼續分享我的體會。好了,今天就到這里吧!
分享標題:android基礎知識12:android自動化測試06—Instrumentation01例子
文章源于:http://m.kartarina.com/article38/pihjsp.html
成都網站建設公司_創新互聯,為您提供小程序開發、動態網站、商城網站、移動網站建設、域名注冊、全網營銷推廣
廣告
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源:
創新互聯