生活随笔
收集整理的這篇文章主要介紹了
单元测试instrumentation入门---eclipse
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言:進公司要先做兩個月測試,我了個去,對測試是不大了解啊,在測試主管的指導下學instrumentation接口,好像還挺好用的,看到一篇文章將其稍做補充摘錄于下,分享給大家。
參考文章地址:《Android單元測試初探——Instrumentation》? ? 這里與原文有些出入,有些不必要的部分我將其去掉了,并增加了一些知識。
正文 首先,我們來了解一下Android的測試類的層次結構:
可以看出android中的測試方法主要有AndroidTextCase和InstrumentationTextCase。在這篇文章中,我將介紹Instrumentation這種測試方法,那么什么是Instrumentation? Instrumentation和Activity有點類似,只不過Activity是需要一個界面的,而Instrumentation并不是這樣的,我們可以將它理解為一種沒有圖形界面的,具有啟動能力的,用于監控其他類(用Target Package聲明)的工具類。 下面通過一個簡單的例子來講解Instrumentation的基本測試方法。 1.首先建立一個Android project,類名為test,代碼如下:
[java] view plaincopy
package ?com.example.test;???? 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 ?MainActivity?extends ?Activity?{???? ????TextView?myText;?? ????Button?button;?? ????@Override ?? ????protected ?void ?onCreate(Bundle?savedInstanceState)?{?? ????????super .onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ?????????? ??????myText?=?(TextView)?findViewById(R.id.text1);?? ??????button?=?(Button)?findViewById(R.id.btn1);?? ??????button.setOnClickListener(new ?OnClickListener()?{?? ??????????@Override ?? ??????????public ?void ?onClick(View?arg0)?{?? ??????????????myText.setText("Hello?Android" );?? ??????????}?? ??????});?? ????}?? ?????? ????public ?int ?add(int ?i,?int ?j)?{?? ????????return ?(i?+?j);?? ????}?? }?? 對應的xml布局代碼如下: [html] view plaincopy
< RelativeLayout ?xmlns:android ="http://schemas.android.com/apk/res/android" ??????xmlns:tools ="http://schemas.android.com/tools" ?? ????android:layout_width ="match_parent" ?? ????android:layout_height ="match_parent" ?? ????tools:context ="com.example.test.MainActivity" ?> ?? ?? ????< TextView ?? ????????android:id ="@+id/text1" ?? ????????android:layout_width ="wrap_content" ?? ????????android:layout_height ="wrap_content" ?? ????????android:text ="@string/hello_world" ?/> ?? ????< Button ?android:id ="@+id/btn1" ?? ????????android:layout_width ="wrap_content" ?? ????????android:layout_height ="wrap_content" ?? ????????android:text ="hellowrold?Btn" ?/> ?? ?? </ RelativeLayout > ?? 這個程序的功能比較簡單,點擊按鈕之后,TextView的內容由Hello變為Hello Android.同時,在這個類中,我還寫了一個簡單的add方法,沒有被調用,僅供測試而已。 2. 在src文件夾中添加一個測試包,在測試包中添加一個測試類SampleTest
測試類的代碼如下:
[java] view plaincopy
package ?com.example.sample.test;???? import ?com.example.test.*;??import ?android.content.Intent;??import ?android.os.SystemClock;??import ?android.test.InstrumentationTestCase;??import ?android.util.Log;??import ?android.widget.Button;??import ?android.widget.TextView;?????? public ?class ?Sampletest?extends ?InstrumentationTestCase?{??????private ?MainActivity?sample?=?null ;?? ????private ?Button?button?=?null ;?? ????private ?TextView?text?=?null ;?? ???? ????? ? ? ?? ????@Override ?? ????protected ?void ?setUp()??{?? ????????try ?{?? ????????????super .setUp();?? ????????}?catch ?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????????Intent?intent?=?new ?Intent();?? ????????intent.setClassName("com.example.test" ,?MainActivity.class .getName());?? ????????intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?? ????????sample?=?(MainActivity)?getInstrumentation().startActivitySync(intent);?? ????????text?=?(TextView)?sample.findViewById(R.id.text1);?? ????????button?=?(Button)?sample.findViewById(R.id.btn1);?? ????}?? ???? ????? ? ? ?? ????@Override ?? ????protected ?void ?tearDown()??{?? ????????sample.finish();?? ????????try ?{?? ????????????super .tearDown();?? ????????}?catch ?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????}?? ???? ????? ? ?? ????public ?void ?testActivity()?throws ?Exception?{?? ????????Log.v("testActivity" ,?"test?the?Activity" );?? ????????SystemClock.sleep(1500 );?? ?????????? ????????for (int ?i=0 ;i<1000 ;i++)?? ????????{?? ????????????getInstrumentation().runOnMainSync(new ?PerformClick(button));?? ????????}?? ?????????? ????????SystemClock.sleep(3000 );?? ????????Log.v("testActivity" ,??text.getText().toString());?? ????????assertEquals("Hello?Android222" ,?text.getText().toString());?? ?????????? ????}?? ???? ????? ? ?? ????private ?class ?PerformClick?implements ?Runnable?{?? ????????Button?btn;?? ????????public ?PerformClick(Button?button)?{?? ????????????btn?=?button;?? ????????}?? ???? ????????public ?void ?run()?{?? ????????????btn.performClick();?? ????????}?? ????}?? ???? ????? ? ?? ????public ?void ?testAdd()?throws ?Exception{?? ????????String?tag?=?"testActivity" ;?? ????????Log.v(tag,?"test?the?method" );?? ????????int ?test?=?sample.add(1 ,?1 );?? ????????assertEquals(2 ,?test);?? ????}?? }?? 下面來簡單講解一下代碼: 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線程。 2.要想正確地執行測試,還需要修改AndroidManifest.xml這個文件.
[html] view plaincopy
<? xml ?version ="1.0" ?encoding ="utf-8" ?> ??< manifest ?xmlns:android ="http://schemas.android.com/apk/res/android" ??????package ="com.example.test" ?? ????android:versionCode ="1" ?? ????android:versionName ="1.0" ?> ?? ?? ????< uses-sdk ?? ????????android:minSdkVersion ="14" ?? ????????android:targetSdkVersion ="14" ?/> ?? ?? ????< application ?? ????????android:allowBackup ="true" ?? ????????android:icon ="@drawable/ic_launcher" ?? ????????android:label ="@string/app_name" ?? ????????android:theme ="@style/AppTheme" ?> ?? ?????????? ??????????? ????????< uses-library ?android:name ="android.test.runner" ?/> ?? ????????< activity ?? ????????????android:name =".MainActivity" ?? ????????????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 > ?? ?????? ??????? ????< instrumentation ?android:targetPackage ="com.example.test" ?android:name ="android.test.InstrumentationTestRunner" ?/> ?? ?? </ manifest > ?? 這里添加了兩行代碼: 1、引入測試庫:<uses-library android:name="android.test.runner" /> 這句是固定的,不允許任何更改
2、被測試的目標包與instrumentation的名稱:<instrumentation android:targetPackage="com.example.test" android:name="android.test.InstrumentationTestRunner" />, 其中目標包名就是我們要測試的包名,一定不要寫錯。
注意:關于這個包名是寫哪個,可能總是有同學搞不清楚,看下面:參考網址:http://stackoverflow.com/questions/16591504/android-studio-import-existing-unit-tests-unable-to-find-instrumentation-info
經過以上步驟,下面可以開始測試了。
用Eclipse集成的JUnit工具 在Eclipse中選擇工程test,單擊右鍵,在Run as子菜單選項中選擇Android JUnit Test
同時可以通過LogCat工具查看信息
所以,instumentation可以實現自動測試,然后找出哪里的結果沒有達到預期,并標出出錯位置,我們只需要根據出錯的位置斷續調代碼就好了。
源碼來啦,地址:http://download.csdn.net/detail/harvic880925/7648933
請大家尊重原創者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/37924189 ?不勝感激……
總結
以上是生活随笔 為你收集整理的单元测试instrumentation入门---eclipse 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。