編輯:高級開發
android手機操作系統是一個開源的操作系統。程序員們可以在模擬器的幫助下對其進行修改,來實現各種功能需求,滿足用戶的應用。在這裡我們先來了解一下android單元測試的相關內容。
在網絡上找了半天,也沒有找正確的Android的測試辦法,最後還是自己琢磨出來的。以前就聽我朋友說過,Android毛病一堆,現在才體會到。Android SDK 和 iphone SDK 比,就不是一個檔次的東西。網上總是有人說,Android還年輕,要體諒。我可不同意,要知道,android都已經出了好幾個版本了,而現在最新的1.5版和iphone sdk的beta版都比不上。
先說說它的單元測試吧,基於1.5和eclipse。這裡我說的是我的思路,有更加好的思路,歡迎指導。
我的方法:如果你要測試的不是Activity或者Service,就用androidTestCase;否則選擇:ActivityUnitTestCase,ServiceTestCase。
在做android單元測試以前,你需要創建一個新的項目,把要測試的項目包含到Java build path中來。項目中的androidManifest.XML如下(這裡假設你的your.test.package裡面包含你的tests,your.work.package包含被測試的類):
XML代碼
- < ?XML version="1.0" encoding="utf-8"?>
- < manifest XMLns:android=
"http://schemas.Android.com/apk/res/android"- package="your.tests.package">
- < application>
- < uses-library android:name="android.test.runner" />
- < /application>
- < instrumentation android:name=
"android.test.InstrumentationTestRunner"- android:targetPackage="your.work.package"
- android:label="Tests for Api Demos."/>
- < /manifest>
- < ?XML version="1.0" encoding="utf-8"?>
- < manifest XMLns:android=
"http://schemas.Android.com/apk/res/android"- package="your.tests.package">
- < application>
- < uses-library android:name="android.test.runner" />
- < /application>
- < instrumentation android:name=
"android.test.InstrumentationTestRunner"- android:targetPackage="your.work.package"
- android:label="Tests for Api Demos."/>
- < /manifest>
那麼就可以做android單元測試了,給出下面的實例;
1.用androidTestCase;
Java代碼
- public class SdcardTest extends androidTestCase {
- public void test1(){
- // Log.v()
- File f=new File("/sdcard");
- String[] l=f.list();
- this.assertTrue(f.exists());
- this.assertTrue(f.isDirectory());
- this.assertTrue(f.list().length>0);
- }
- }
- public class SdcardTest extends androidTestCase {
- public void test1(){
- // Log.v()
- File f=new File("/sdcard");
- String[] l=f.list();
- this.assertTrue(f.exists());
- this.assertTrue(f.isDirectory());
- this.assertTrue(f.list().length>0);
- }
- }
2.用ActivityUnitTestCase
Java代碼
- public class ForwardingTest extends ActivityUnitTestCase
< Forwarding> {- private Intent mStartIntent;
- private Button mButton;
- public ForwardingTest() {
- super(Forwarding.class);
- }
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- // In setUp, you can create any shared test data,
or set up mock components to inject- // into your Activity. But do not call startActivity()
until the actual test methods.- mStartIntent = new Intent(Intent.ACTION_MAIN);
- }
- /**
- * The name 'test preconditions' is a convention
to signal that if this- * test doesn't pass, the test case was not set up
properly and it might- * explain any and all failures in other tests.
This is not guaranteed- * to run before other tests, as junit uses
reflection to find the tests.- */
- @MediumTest
- public void testPreconditions() {
- startActivity(mStartIntent, null, null);
- mButton = (Button) getActivity().findVIEwById(R.id.go);
- assertNotNull(getActivity());
- assertNotNull(mButton);
- }
- /**
- * This test demonstrates examining the way that
activity calls startActivity() to launch- * other activitIEs.
- */
- @MediumTest
- public void testSubLaunch() {
- Forwarding activity = startActivity(mStartIntent, null, null);
- mButton = (Button) activity.findVIEwById(R.id.go);
- // This test confirms that when you click the
button, the activity attempts to open- // another activity (by calling startActivity)
and close itself (by calling finish()).- mButton.performClick();
- assertNotNull(getStartedActivityIntent());
- assertTrue(isFinishCalled());
- }
- /**
- * This test demonstrates ways to exercise the Activity's life cycle.
- */
- @MediumTest
- public void testLifeCycleCreate() {
- Forwarding activity = startActivity(mStartIntent, null, null);
- // At this point, onCreate() has been called, but nothing else
- // Complete the startup of the activity
- getInstrumentation().callActivityOnStart(activity);
- getInstrumentation().callActivityOnResume(activity);
- // At this point you could test for various configuration
ASPects, or you could- // use a Mock Context to confirm that your activity
has made certain calls to the system- // and set itself up properly.
- getInstrumentation().callActivityOnPause(activity);
- // At this point you could confirm that the activity
has paused properly, as if it is- // no longer the topmost activity on screen.
- getInstrumentation().callActivityOnStop(activity);
- // At this point, you could confirm that the activity
has shut itself down appropriately,- // or you could use a Mock Context to confirm that your
activity has released any system- // resources it should no longer be holding.
- // ActivityUnitTestCase.tearDown(), which is always
automatically called, will take care- // of calling onDestroy().
- }
- }
android單元測試相關應用方式就為大家介紹到這裡。
不久前51CTO對國內的移動廣告平台進行一個詳細的介紹,對哇棒(wooboo)、架勢(casee)、有米(youmi)、微雲各平台的優劣都做了評估。本文將對各個平台做一
現在您已經了解了傳感器子系統,接下來的部分將回顧一個在 android 手機上錄制音頻的代碼樣例,該樣例運行在 android設備上,onAccuracyChanged
本文向你展示了在你的android應用程序中創建一個簡單的Button或ImageButton控件的步驟。首先,你會學到如何向你的布局文件中添加按鈕控件。然後你會學習如
廣播接收器(Broadcast)簡單地從其他應用程序或系統響應廣播消息。這些消息有時稱為事件或意圖。例如,應用程序也可以發起廣播,以讓其他應用程序知道某些數據已經被下載到