Testing with Android Part I of II

24
Testing with Android Part I of II

description

Testing with Android Part I of II. Android Testing Framework. Based on JUnit The Android JUnit extensions provide component-specific test case classes. . Android Testing Framework. Test suites are contained in test packages that are similar to main application packages. - PowerPoint PPT Presentation

Transcript of Testing with Android Part I of II

Page 1: Testing with Android  Part I of II

Testing with Android Part I of II

Page 2: Testing with Android  Part I of II

Android Testing Framework

• Based on JUnit• The Android JUnit extensions provide

component-specific test case classes.

Page 3: Testing with Android  Part I of II

Android Testing Framework

• Test suites are contained in test packages that are similar to main application packages.

• The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDES.

Page 4: Testing with Android  Part I of II

Android Testing Framework

• These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.

• The SDK also provides monkeyrunner, an API testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.

Page 5: Testing with Android  Part I of II

Android Testing Framework

Page 6: Testing with Android  Part I of II

JUnit

• You can use the JUnit TestCase class to do unit testing on a plain Java object.

• TestCase is also the base class for AndroidTestCase, which you can use to test Android-dependent objects.

• Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup, teardown, and helper methods.

Page 7: Testing with Android  Part I of II

Testing from Eclipse with ADTADT provides several features that help you set up and manage your testing environment effectively:

• It lets you quickly create a test project and link it to the application under test.

• It lets you quickly import the classes of the application under test.

• It lets you create run configurations for your test package and include in them flags that are passed to the Android testing framework.

• It lets you run your test package without leaving Eclipse.

Page 8: Testing with Android  Part I of II

Creating a Test Project

Page 9: Testing with Android  Part I of II
Page 10: Testing with Android  Part I of II

The class contains definitions for four methods: • HelloAndroidTest: This defines the constructor for the class. It is required

by the Android testing framework. • setUp(): This overrides the JUnit setUp() method. You use it to initialize

the environment before each test runs. • testPreconditions(): This defines a small test that ensures the Hello,

Android application starts up correctly. • testText(): This tests that what is displayed on the screen is the same as

what is contained in the application's string resources. It is an example of a real unit test you would perform against an application's UI.

Page 11: Testing with Android  Part I of II

Creating the Test Case Class

Page 12: Testing with Android  Part I of II
Page 13: Testing with Android  Part I of II

HelloAndroidTest.java

package com.example.helloandroid.test;

import android.test.ActivityInstrumentationTestCase2;import com.example.helloandroid.HelloAndroid;

public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> {}

Page 14: Testing with Android  Part I of II

Constructorprivate HelloAndroid mActivity;private TextView mView;private String resourceString;     public HelloAndroidTest() {

      super("com.example.helloandroid",HelloAndroid.class);

    }

Page 15: Testing with Android  Part I of II

setUp()@Overrideprotected void setUp() throws Exception {

super.setUp();mActivity = this.getActivity();

mView = (TextView) mActivity.findViewById (com.example.helloandroid.R.id.textview);

resourceString = mActivity.getString (com.example.helloandroid.R.string.hello);}

Page 16: Testing with Android  Part I of II

Adding a preconditions testA preconditions test checks the initial application conditions prior to executing other tests.

public void testPreconditions() { assertNotNull(mView); }

Page 17: Testing with Android  Part I of II

Adding a unit testpublic void testText() {

assertEquals(resourceString,(String)mView.getText());}

Page 18: Testing with Android  Part I of II

Finished Class - 1package com.example.helloandroid.test;

import com.example.helloandroid.HelloAndroid;import android.test.ActivityInstrumentationTestCase2;import android.widget.TextView;

public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> { private HelloAndroid mActivity; // the activity under test private TextView mView; // the activity's TextView (the only view) private String resourceString;

public HelloAndroidTest() { super("com.example.helloandroid", HelloAndroid.class); }}

Page 19: Testing with Android  Part I of II

Finished Class@Override protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview); resourceString = mActivity.getString(com.example.helloandroid.R.string.hello); } public void testPreconditions() { assertNotNull(mView); } public void testText() { assertEquals(resourceString,(String)mView.getText()); }}

Page 20: Testing with Android  Part I of II

Running Tests• When you run a test package in Eclipse with ADT, the output appears in

the Eclipse JUnit view. • You can run the entire test package or one test case class. To do run tests,

Eclipse runs the adb command for running a test package, and displays the output, so there is no difference between running tests inside Eclipse and running them from the command line.

Page 21: Testing with Android  Part I of II
Page 22: Testing with Android  Part I of II
Page 23: Testing with Android  Part I of II
Page 24: Testing with Android  Part I of II