Adroid 應用程式的測試方法 - I
圖片引用自 tucowsinc.com
Unit tests and testing are parts of each developer's life and they are important parts of development. Developers usually concentrate on unit tests which help them ensure that future changes will not break the system and let other people do tests and stress tests of applications to reveal bugs, which we as developers simply cannot see.
About This Series of Articles
This is the first article about testing Android applications. We will look at some testing background, discuss basic elements available for you in the SDK and show how to prepare testing environment to run tests. In following articles we will concentrate on describing testing methods in detail.
Short History of Android Testing
Prior to Android SDK 1.0 there was a huge gap in testing of android applications which inspired 3rd-party testing frameworks such as Positron and Electron. With Android SDK 1.0 Google filled the gap with its own testing framework and Positron and Electron frameworks are not needed any more. Filled the gap in testing and filled it well. Let's have a look at what is available there for us. We will concentrate on unit and functional test which are supported with new part of Android SDK.
Unit Testing There are basically three levels of tests/unit-test you can use in your application: To be able to test layers of the application in isolation using and fully use
prevoius three levels. It is useful to follow these guidelines during design
and development phases of your Android application: How to Prepare Testing Environment for Unit
Tests Good starting point for preparing environment for tests which do not depend
on Android can be found here.
Android runtime in android.jar contains some subset of junit which can not be
used out of android environment. If you try to run unit test directly from Eclipse,
you get exception like this: To be able to run tests from Eclipse you have to replace android.jar by junit.jar Image on the left shows "Run Configuration" dialog with Android library not
deleted and image on the right shows configuration of build path. It is sometimes
even possible to unit test classes which depends on classes from android.jar
by deleting junit classes in the jar file. But once tested classes requires
something from android runtime, you get exceptions. Setup for Tests Depending on Android Good starting point is here
which shows how to set separate application and test environment but does not
show that is possible to have everything working together in one project. You
have to setup instrumentation for your project which allows your tests to hook
into your application and take control over application ui and pass various
events. where: How to Run Tests Depending on Android in Eclipse
and Emulator To run tests directly from emulator go to DevTools application in the emulator,
select Instrumentation and click on the name of the tests you want to run. Results
and lot of debugging output can be found in Logcat console. How to Run Tests Depending on Android with
adb To run tests from command line enter where PACKAGE is full name of application package which should be instrumented. Monkey Does everything seem to be too complicated for you? Do you wonder if there
is simple way how to test Android application? The answer is UI/Application
Exerciser Monkey tool available in Android SDK. Monkey is quite fast way
for testing of ui. Just be warned that monkey does not like real devices yet.
There is bug which causes that monkey is killed by Android whenever it tries
to send a notification key to the real T-Mobile G1 device. Now our clever monkey
likes to work without bugs with the emulator only. Next Article... So far we know what we can test on Android application and we know how to
setup and run various kinds of tests. Next time we will look at some tips and
hints how to actually write unit and functional tests. Stay tuned... 註: 這是 Ondrej 的第一篇文章,原先是想幫他在文中譯註關鍵部份。不過我讀了一下,發覺他的用語都還簡單易懂,加上又是技術文章,大家應該有能力,理解他所要傳遞的知識。如果硬在原文中,加上中文譯註,反倒破壞了閱讀者的流暢性。 最後決定,我會在文章的最後,加上關鍵部份的註解。不會逐字翻譯,多是將重點或精神點出。如果你有任何的問題,歡迎在這裡提問,中文發問也可以的。 單元測試 (Unit Testing) 以測試的角度來看,基本上一 個應用程式可分成三層: 要讓你的程式達到 "可測試" 的程度,你必須在寫程式注意一些事情,及遵守下列原則:
...
Internal Error (classFileParser.cpp:2924), pid=11018, tid=3084700560 # Error:
ShouldNotReachHere()
...
adb shell am instrument -w PACKAGE/android.test.InstrumentationTestRunner
引用來源:http://ysl-paradise.blogspot.com/2009/03/testing-android-applications-part-1.html