android基础知识12:android自动化测试06—Instrumentation 01 例子
转载处:http://blog.csdn.net/xianming01/article/details/7893391
下面通过一个简单的例子来讲解Instrumentation的基本测试方法。在这个例子中我们会建立一个简单的android应用,同时在其上添加Instrumentation测试程序。
1.首先建立一个android project,其文件结构最终如下:
2、布局文件
[html]view plaincopy <?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.hustophone.sample"android:versionCode="1" android:versionName="1.0"> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <!--用于引入测试库--> <uses-libraryandroid:name="android.test.runner"/> <activityandroid:name=".Sample"android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-sdkandroid:minSdkVersion="3"/> <!--表示被测试的目标包与instrumentation的名称。--> <instrumentationandroid:targetPackage="com.hustophone.sample" android:name="android.test.InstrumentationTestRunner"/> </manifest> 3、被测程序Sample类
[java]view plaincopy packagecom.hustophone.sample; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.TextView; publicclassSampleextendsActivity{ privateTextViewmyText=null; privateButtonbutton=null; /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); myText=(TextView)findViewById(R.id.text1); button=(Button)findViewById(R.id.button1); button.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewarg0){ //TODOAuto-generatedmethodstub myText.setText("HelloAndroid"); } }); } publicintadd(inti,intj){ //TODOAuto-generatedmethodstub return(i+j); } } 这个程序的功能比较简单,点击按钮之后,TextView的内容由Hello变为Hello Android.同时,在这个类中,我还写了一个简单的方法,没有被调用,仅供测试而已。
4、测试类SampleTest 通常可以将测试程序作为另一个android应用程序。但是这里我们为了操作方便,写在了一个应用里面了。 [java]view plaincopy packagecom.hustophone.sample.test; importcom.hustophone.sample.R; importcom.hustophone.sample.Sample; importandroid.content.Intent; importandroid.os.SystemClock; importandroid.test.InstrumentationTestCase; importandroid.util.Log; importandroid.widget.Button; importandroid.widget.TextView; publicclassSampleTestextendsInstrumentationTestCase{ privateSamplesample=null; privateButtonbutton=null; privateTextViewtext=null; /* *初始设置 * *@seejunit.framework.TestCase#setUp() */ @Override protectedvoidsetUp(){ try{ super.setUp(); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } Intentintent=newIntent(); intent.setClassName("com.hustophone.sample",Sample.class.getName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sample=(Sample)getInstrumentation().startActivitySync(intent); text=(TextView)sample.findViewById(R.id.text1); button=(Button)sample.findViewById(R.id.button1); } /* *垃圾清理与资源回收 * *@seeandroid.test.InstrumentationTestCase#tearDown() */ @Override protectedvoidtearDown(){ sample.finish(); try{ super.tearDown(); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } /* *活动功能测试 */ publicvoidtestActivity()throwsException{ Log.v("testActivity","testtheActivity"); SystemClock.sleep(1500); getInstrumentation().runOnMainSync(newPerformClick(button)); SystemClock.sleep(3000); assertEquals("HelloAndroid",text.getText().toString()); } /* *模拟按钮点击的接口 */ privateclassPerformClickimplementsRunnable{ Buttonbtn; publicPerformClick(Buttonbutton){ btn=button; } publicvoidrun(){ btn.performClick(); } } /* *测试类中的方法 */ publicvoidtestAdd()throwsException{ Stringtag="testAdd"; Log.v(tag,"testthemethod"); inttest=sample.add(1,1); assertEquals(2,test); } } 下面来简单讲解一下代码:
setUp()和tearDown()都是受保护的方法,通过继承可以覆写这些方法。
在android Developer中有如下的解释 [java]view plaincopy protectedvoidsetUp() Since:APILevel3 Setsupthefixture,forexample,openanetworkconnection.Thismethodiscalledbeforeatestisexecuted. protectedvoidtearDown() Since:APILevel3 Makesureallresourcesarecleanedupandgarbagecollectedbeforemovingontothenexttest.Subclassesthatoverridethismethodshouldmakesuretheycallsuper.tearDown()attheendoftheoverridingmethod. 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的测试内容还有很多,也有比较复杂的,我会在以后的学习过程中继续分享我的体会。好了,今天就到这里吧!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。