A First Look at the Android Testing Framework
来源:互联网 发布:手机解锁软件大全 编辑:程序博客网 时间:2024/05/20 18:41
This article comes from: http://codinghard.wordpress.com/2009/06/04/a-first-look-at-the-android-testing-framework/
Android has a built-in testing framework that includes a subset of JUnit. All of its test-case classes are directly or indirectly derived from JUnit's TestCase. If you're already familiar with JUnit, it's not that difficult to use the Android Testing Framework. The Android SDK has some sample testing programs in android-sdk-mac_x86-1.5_r1/platforms/android-1.5/samples/ApiDemos/tests. Here are some common testing classes in the framework:
TestCase – Plain old JUnit test case. It can be extended to test utility classes that are not tied to the Android framework.
AndroidTestCase – It extends JUnit's TestCase. It's a lighter testing class compared to ActivityTestCase. It doesn't need to launch an activity to run it. Its getContext() method allows you to get an injected context if you need one. Since you can get a context from this class, you can inflate your UI objects to test their behaviors. However, the UI behaviors will not show in the emulator. I like to use this class to test my data access objects.
ActivityInstrumentationTestCase2
ActivityUnitTestCase
ApplicationTestCase
InstrumentationTestRunner – The runner that runs the Android test cases.
The class diagram of those classes probably looks something like this (NOTE: Not all the attributes and operations are listed):
Here is how to create a sample testing project in Eclipse to test the NotesDdAdapter object of Notepad3 (Android's Notepad tutorial).
- Create the Notepad3 project in Eclipse.
- Create an Android project in Eclipse called Notepad3Tests. Make sure the Create Activity option is unchecked.
- Make sure Notepad3′s bin folder is included in the build path of Notepad3Tests.
- Add this class NotesDbAdapterTests to the Notepad3Tests project. It's the sample code that tests NotesDbAdapter's createNote(…) function. // see it below.
- Modify the AndroidManifest.xml file in the Notepad3Tests project. a) Click the Application tab. Under the Application Nodes, add a Uses Library node. b) Click the node that you just created. In the Name field on the right, add android.test.runner. I don't know why this has to be done since there is not much information about android.test.runner. c) Click the Instrumentation tab and add an instrumentation. d) On the right side, in the Name field, add android.test.InstrumentationTestRunner. In the Target Package field, add com.android.demo.notepad3.
- Run Notepad3 so it can be installed on the emulator.
- Run NotesDbAdapterTests by selecting Run As > Android JUnit Test.
public class NotesDbAdapterTests extends AndroidTestCase {
private static final String TAG = "NotesDbAdapterTests";
private NotesDbAdapter dbAdapter;
@Override
protected void setUp() throws Exception {
super.setUp();
// Do some initial setup here
Log.d(TAG, "In setUp");
dbAdapter = new NotesDbAdapter(this.getContext());
dbAdapter.open();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// Do some clean up here
Log.d(TAG, "In tearDown");
dbAdapter.close();
}
// Test createNote
public void testCreateNote() {
long id = dbAdapter.createNote("this is title", "this is body");
Log.d(TAG, "Note id: " + id);
assertTrue("Failed to insert note", id > 0);
}
}
The final manifest file looks like this:package="com.flybul.android"
android:versionCode="1"
android:versionName="1.0">
- A First Look at the Android Testing Framework
- [zz]A Closer Look At the Blueprint CSS Framework
- A first hand look at building an Android application
- A First Look At Input/Output
- A First Look at Rust Language
- WebKit 框架一瞥(A Look at the WebKit Framework) – Part 1
- A Look at the Buffer-Overflow Hack
- A First Look at SQL Server 2005 for Developers:Introduction
- A First Look at ASP.NET v. 2.0
- A Look At Android Support Annotations
- A closer look at the Android project build system part I
- A closer look at the Android project build system part II
- A low-level Look at the ASP.NET Architecture
- A low-level Look at the ASP.NET Architecture
- A brief look at the Xcode project format
- A low-level Look at the ASP.NET Architecture
- A look at the top posts about Revolutions from 2014
- A look at the PowerVR graphics architecture: Tile-based rendering
- A Fall in Love Story
- A function for punbb
- Amazon's wish list service is amazing
- Android G2手机如何使用CMWAP上网
- Android Function: public static boolean isCmwap(Context context)
- A First Look at the Android Testing Framework
- Android 文件操作 读/写
- Android取用户屏幕大小
- Blackberry version "hello world", which use a socket to send msg to a server
- Compiler CC and gcc under Unix are able to link each
- Dropbox, Amazing!
- Dream - Plan - Action
- Goal for 2010
- GPRS、WAP、WAP网关、HTTP、cmwap,、cmnet