Unit Testing in Android Li-Wei Cheng
Outline • Android Testing Framework • Test Projects • The Testing API • Running Tests • Seeing Test Results
Android Testing Framework (1) • An integral part of the development environment. • Provides architecture and powerful tools help you to test your app at every level from unit to framework.
Android Testing Framework (2) • Key features for the testing framework (1) o Android Test Suites are based on JUnit. Use plain JUnit to test a class doesn't call the Android API. Use Android's JUnit extensions to test Android components. o Android JUnit extensions provide component-specific test classes. Helper methods for creating mock objects. Helper methods for controlling the lifecycle of a component. o Test suites are contained in test packages that are similar to a main application packages. You don't to learn a new set of tools or techniques for designing and building tests.
Android Testing Framework (3) • Key features for the testing framework (2) o The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDES. 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 the directory structure for the test package. o The SDK also provides monkeyrunner, a API testing devices with Python programs. UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
Android Testing Framework (4) • Summarize Testing Framework
Test Projects (1) • Test, like Android applications, are organized into projects. test project usually bundles with tested project
Test Projects (2) • Two ways to create your test project o Using ADT An eclipse plugin will generate files automatically. o Using command-line tools It will create Ant build files you need automatically.
Test Projects (3) • Use Android tools to create test project having the following benefits: o Set up your test project to use the InstrumentationTestRunner as the test case runner. o Create appropriate package name for the test project. o Automatically create the proper build files. Manifest files and directory structure for the test project. Helps you to build the test project without having to modify build files and sets up the linkage between test package and the application under test.
The Testing API (1) • Provided by JUnit. o TestCase Class To do unit testing on a class that doesn't call Android APIs. Offers setup, and teardown methods. o JUnit Assertion Class Compare values you expect from a test to the actual results and throw an exception if the comparison fails. Display the test results. • Provided by Android. o AndroidTestCase Class A subclass of TestCase in JUnit. Offers Android-specific setup, teardown, and helper methods. o Android Assertion Class A subclass of Junit Assertion class.
The Testing API (2) • Provided by Android. o Instrumentation A set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal lifecycle. Use instrumentation to invoke the activity's lifecycle callback directly. o InstrumentationTestCase A subclass of JUnit's TestCase Class. The class can access the instance of instrumentation directly.
The Testing API (3) • Provided by Android. o ApplicationTestCase It's useful to verify that the <application> elements in the manifest file is correctly set up. o Component-specific test cases Activity Testing Service Testing Content Provider Testing Android doesn't provide a separate test case for BroadcastReceiver. Instead, test a BroadcastReceiver by testing the component that sends it intent object, to verify that the BroadcastReceiver responds correctly.
The Testing API (4) • Provided by Android. o Mock object classes o Context for testing
Example (1) test target test apk
Example (2)
Example (3)

Unit testing in android

  • 1.
    Unit Testing in Android Li-Wei Cheng
  • 2.
    Outline • Android Testing Framework • Test Projects • The Testing API • Running Tests • Seeing Test Results
  • 3.
    Android Testing Framework(1) • An integral part of the development environment. • Provides architecture and powerful tools help you to test your app at every level from unit to framework.
  • 4.
    Android Testing Framework(2) • Key features for the testing framework (1) o Android Test Suites are based on JUnit. Use plain JUnit to test a class doesn't call the Android API. Use Android's JUnit extensions to test Android components. o Android JUnit extensions provide component-specific test classes. Helper methods for creating mock objects. Helper methods for controlling the lifecycle of a component. o Test suites are contained in test packages that are similar to a main application packages. You don't to learn a new set of tools or techniques for designing and building tests.
  • 5.
    Android Testing Framework(3) • Key features for the testing framework (2) o The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDES. 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 the directory structure for the test package. o The SDK also provides monkeyrunner, a API testing devices with Python programs. UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
  • 6.
    Android Testing Framework(4) • Summarize Testing Framework
  • 7.
    Test Projects (1) • Test, like Android applications, are organized into projects. test project usually bundles with tested project
  • 8.
    Test Projects (2) • Two ways to create your test project o Using ADT An eclipse plugin will generate files automatically. o Using command-line tools It will create Ant build files you need automatically.
  • 9.
    Test Projects (3) • Use Android tools to create test project having the following benefits: o Set up your test project to use the InstrumentationTestRunner as the test case runner. o Create appropriate package name for the test project. o Automatically create the proper build files. Manifest files and directory structure for the test project. Helps you to build the test project without having to modify build files and sets up the linkage between test package and the application under test.
  • 10.
    The Testing API(1) • Provided by JUnit. o TestCase Class To do unit testing on a class that doesn't call Android APIs. Offers setup, and teardown methods. o JUnit Assertion Class Compare values you expect from a test to the actual results and throw an exception if the comparison fails. Display the test results. • Provided by Android. o AndroidTestCase Class A subclass of TestCase in JUnit. Offers Android-specific setup, teardown, and helper methods. o Android Assertion Class A subclass of Junit Assertion class.
  • 11.
    The Testing API(2) • Provided by Android. o Instrumentation A set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal lifecycle. Use instrumentation to invoke the activity's lifecycle callback directly. o InstrumentationTestCase A subclass of JUnit's TestCase Class. The class can access the instance of instrumentation directly.
  • 12.
    The Testing API(3) • Provided by Android. o ApplicationTestCase It's useful to verify that the <application> elements in the manifest file is correctly set up. o Component-specific test cases Activity Testing Service Testing Content Provider Testing Android doesn't provide a separate test case for BroadcastReceiver. Instead, test a BroadcastReceiver by testing the component that sends it intent object, to verify that the BroadcastReceiver responds correctly.
  • 13.
    The Testing API(4) • Provided by Android. o Mock object classes o Context for testing
  • 14.
    Example (1) test target test apk
  • 15.
  • 16.