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 – It's the newer version of ActivityInstrumentationTestCase.  ActivityInstrumentationTestCase is deprecated in Android SDK 1.5.  It's a heavier testing class compared to AndroidTestCase.  It provides UI and functional testing for a single activity.  You can get an injected activity that you are testing on by calling its getActivity() method.  The activity being tested is launched and finished before and after each test.  Since the activity is launched for each test, you can see how the UI is being tested in the emulator.

ActivityUnitTestCase – It gives the tested activity an isolated environment.  When using it to test an activity, the activity is not attached to the system.  This gives you more control over what kind of environment that you want your activity to be tested in.

ApplicationTestCase – It provides testing for Application classes.  It can be used to test the life cycle of an application.

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).

  1. Create the Notepad3 project in Eclipse.
  2. Create an Android project in Eclipse called Notepad3Tests.  Make sure the Create Activity option is unchecked.
  3. Make sure Notepad3′s bin folder is included in the build path of Notepad3Tests.
  4. Add this class NotesDbAdapterTests to the Notepad3Tests project.  It's the sample code that tests NotesDbAdapter's createNote(…) function. // see it below.
  5. 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.
  6. Run Notepad3 so it can be installed on the emulator.
  7. 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">







原创粉丝点击