Android單元測試源碼解讀
Android手機操作系統(tǒng)是一個開源的操作系統(tǒng)。程序員們可以在模擬器的幫助下對其進行修改,來實現(xiàn)各種功能需求,滿足用戶的應用。在這里我們先來了解一下Android單元測試的相關(guān)內(nèi)容。#t#
在網(wǎng)絡上找了半天,也沒有找正確的Android的測試辦法,***還是自己琢磨出來的。以前就聽我朋友說過,Android毛病一堆,現(xiàn)在才體會到。Android SDK 和 iphone SDK 比,就不是一個檔次的東西。網(wǎng)上總是有人說,Android還年輕,要體諒。我可不同意,要知道,Android都已經(jīng)出了好幾個版本了,而現(xiàn)在***的1.5版和iphone sdk的beta版都比不上。
先說說它的單元測試吧,基于1.5和eclipse。這里我說的是我的思路,有更加好的思路,歡迎指導。
我的方法:如果你要測試的不是Activity或者Service,就用AndroidTestCase;否則選擇:ActivityUnitTestCase,ServiceTestCase。
在做Android單元測試以前,你需要創(chuàng)建一個新的項目,把要測試的項目包含到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單元測試相關(guān)應用方式就為大家介紹到這里。