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

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">
- <!--用于引入測(cè)試庫-->
- <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" />
- <!--表示被測(cè)試的目標(biāo)包與instrumentation的名稱。-->
- <instrumentation android:targetPackage="com.hustophone.sample"
- android:name="android.test.InstrumentationTestRunner" />
- </manifest>
3、被測(cè)程序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);
-
- }
-
- }
這個(gè)程序的功能比較簡單,點(diǎn)擊按鈕之后,TextView的內(nèi)容由Hello變?yōu)镠ello Android.同時(shí),在這個(gè)類中,我還寫了一個(gè)簡單的方法,沒有被調(diào)用,僅供測(cè)試而已。
4、測(cè)試類SampleTest
通常可以將測(cè)試程序作為另一個(gè)android應(yīng)用程序。但是這里我們?yōu)榱瞬僮鞣奖悖瑢懺诹艘粋€(gè)應(yīng)用里面了。
下面來簡單講解一下代碼:
setUp()和tearDown()都是受保護(hù)的方法,通過繼承可以覆寫這些方法。
在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 ()用來初始設(shè)置,如啟動(dòng)一個(gè)Activity,初始化資源等。
tearDown ()則用來垃圾清理與資源回收。
在testActivity()這個(gè)測(cè)試方法中,我模擬了一個(gè)按鈕點(diǎn)擊事件,然后來判斷程序是否按照預(yù)期的執(zhí)行。在這里PerformClick這個(gè)方法繼承了Runnable接口,通過新的線程來執(zhí)行模擬事件,之所以這么做,是因?yàn)槿绻苯釉赨I線程中運(yùn)行可能會(huì)阻滯UI線程。
<uses-library android:name="android.test.runner" />用于引入測(cè)試庫
<instrumentation android:targetPackage="com.hustophone.sample"
android:name="android.test.InstrumentationTestRunner" />
表示被測(cè)試的目標(biāo)包與instrumentation的名稱。
經(jīng)過以上步驟,下面可以開始測(cè)試了。測(cè)試方法也有以下幾種,下面介紹兩個(gè)常用的方法:
(1) 用Eclipse集成的JUnit工具
在Eclipse中選擇工程Sample,單擊右鍵,在Run as子菜單選項(xiàng)中選擇Android JUnit Test
同時(shí)可以通過LogCat工具查看信息
(2) 通過模擬器運(yùn)行單元測(cè)試
點(diǎn)擊模擬器界面的Dev Tools菜單
再點(diǎn)擊Instrumentation選項(xiàng),進(jìn)入Instrumentation菜單
這里有一個(gè)InstrumentationTestRunner,它是測(cè)試的入口,點(diǎn)擊這個(gè)選項(xiàng),就可以自動(dòng)運(yùn)行我們的測(cè)試代碼。以下為運(yùn)行結(jié)果:
按鈕點(diǎn)擊前
按鈕點(diǎn)擊后
至此,一個(gè)簡單的測(cè)試過程結(jié)束了。當(dāng)然,android的測(cè)試內(nèi)容還有很多,也有比較復(fù)雜的,我會(huì)在以后的學(xué)習(xí)過程中繼續(xù)分享我的體會(huì)。好了,今天就到這里吧!
分享標(biāo)題:android基礎(chǔ)知識(shí)12:android自動(dòng)化測(cè)試06—Instrumentation01例子
文章源于:http://chinadenli.net/article38/pihjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、全網(wǎng)營銷推廣
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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í)需注明來源:
創(chuàng)新互聯(lián)