O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Android Application Testing

Book Description

Improve your Android applications through intensive testing and debugging

In Detail

This book is a practical introduction to readily available techniques, frameworks, and tools to thoroughly test your Android applications and improve project development.

You will learn the Java testing framework, how to create a test case and debug it. Next, you'll be walked through using the Android SDK to test using the ActivityTestCase and ActivityUnitTest classes as well as discussing popular testing libraries. Through examples you will test files, databases, ContentProviders, exceptions, services, and test your app using Espresso. You will discover how to manage your Android testing environment using Android emulators, deep dive into how adb and the emulator can super charge your testing automation, and also test user interactions with monkeyrunner. You will be guided through different testing methodologies including Test-driven Development and Behavior-driven Development and will learn how to perform Unit and Functional testing applying them to your Android projects. You will also use continuous integration techniques for ultimate application quality control using Gradle and Jenkins.

By the end of the book, you'll be looking through alternative testing tactics including Fest and Spoon to build upon and expand your Android testing range and finesse.

What You Will Learn

  • Apply testing techniques and utilize tools to improve Android application development
  • Get to grips with the nuances of testing on Android, including how to architect an application to facilitate better testing
  • Explore the Android instrumentation testing framework to optimize your activities, services, content providers, and usage of other Android components
  • Understand different development methodologies including Test-driven Development and Behavior-driven Development
  • Apply the continuous integration technique for ultimate application quality control
  • Improve application performance by analyzing the results returned from performance tests
  • Expose your application to a wide range of conditions and configurations to simulate real-life network conditions and detect problems in the application
  • Explore further tools to improve application quality such as micro benchmarks and code coverage

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Android Application Testing
    1. Table of Contents
    2. Learning Android Application Testing
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
        5. Questions
    8. 1. Getting Started with Testing
      1. Why, what, how, and when to test?
        1. What to test
          1. Activity lifecycle events
          2. Database and filesystem operations
          3. Physical characteristics of the device
      2. Types of tests
        1. Unit tests
          1. The setUp() method
          2. The tearDown() method
          3. Outside the test method
          4. Inside the test method
            1. Mock objects
        2. Integration tests
          1. UI tests
        3. Functional or acceptance tests
          1. Test case scenario
        4. Performance tests
        5. System tests
          1. Android Studio and other IDE support
      3. Java testing framework
      4. Android testing framework
        1. Instrumentation
        2. Gradle
        3. Test targets
      5. Creating the Android project
        1. Package explorer
        2. Creating a test case
        3. Test annotations
        4. Running the tests
          1. Running all tests from Android Studio
          2. Running a single test case from your IDE
          3. Running from the emulator
          4. Running tests from the command line
          5. Running all tests
          6. Running tests from a specific test case
          7. Running a specific test by name
          8. Running specific tests by category
          9. Running tests using Gradle
            1. Creating a custom annotation
            2. Running performance tests
            3. Dry run
        5. Debugging tests
        6. Other command-line options
      6. Summary
    9. 2. Understanding Testing with the Android SDK
      1. The demonstration application
      2. Assertions in depth
        1. Custom messages
        2. Static imports
      3. View assertions
      4. Even more assertions
      5. The TouchUtils class
      6. Mock objects
        1. An overview of MockContext
        2. The IsolatedContext class
        3. Alternate route to file and database operations
        4. The MockContentResolver class
      7. The TestCase base class
        1. The default constructor
        2. The given name constructor
        3. The setName() method
      8. The AndroidTestCase base class
        1. The assertActivityRequiresPermission() method
          1. Description
          2. Example
        2. The assertReadingContentUriRequiresPermission method
          1. Description
          2. Example
        3. The assertWritingContentUriRequiresPermission() method
          1. Description
          2. Example
      9. Instrumentation
        1. The ActivityMonitor inner class
          1. Example
      10. The InstrumentationTestCase class
        1. The launchActivity and launchActivityWithIntent methods
        2. The sendKeys and sendRepeatedKeys methods
        3. The runTestOnUiThread helper method
      11. The ActivityTestCase class
        1. The scrubClass method
      12. The ActivityInstrumentationTestCase2 class
        1. The constructor
        2. The setUp method
        3. The tearDown method
      13. The ProviderTestCase2<T> class
        1. The constructor
        2. An example
      14. The ServiceTestCase<T>
        1. The constructor
      15. The TestSuiteBuilder.FailedToCreateTests class
      16. Using libraries in test projects
      17. Summary
    10. 3. Baking with Testing Recipes
      1. Android unit tests
      2. Testing activities and applications
        1. Mocking applications and preferences
          1. The RenamingMockContext class
          2. Mocking contexts
        2. Testing activities
      3. Testing files, databases, and content providers
        1. The BrowserProvider tests
      4. Testing exceptions
      5. Testing local and remote services
      6. Extensive use of mock objects
        1. Importing libraries
        2. Mockito usage example
        3. The EditNumber filter tests
      7. Testing views in isolation
      8. Testing parsers
        1. Android assets
      9. The parser test
      10. Testing for memory usage
      11. Testing with Espresso
      12. Summary
    11. 4. Managing Your Android Testing Environment
      1. Creating Android Virtual Devices
      2. Running AVDs from the command line
        1. Headless emulator
        2. Disabling the keyguard
        3. Cleaning up
        4. Terminating the emulator
      3. Additional emulator configurations
        1. Simulating network conditions
      4. Speeding up your AVD with HAXM
      5. Alternatives to the AVD
      6. Running monkey
        1. The client-server monkey
      7. Test scripting with monkeyrunner
        1. Getting test screenshots
        2. Record and playback
      8. Summary
    12. 5. Discovering Continuous Integration
      1. Building Android applications manually using Gradle
      2. Git – the fast version control system
        1. Creating a local Git repository
      3. Continuous integration with Jenkins
        1. Installing and configuring Jenkins
        2. Creating the jobs
        3. Obtaining Android test results
      4. Summary
    13. 6. Practicing Test-driven Development
      1. Getting started with TDD
        1. Writing a test case
        2. Running all tests
        3. Refactoring the code
        4. Advantages of TDD
        5. Understanding the requirements
      2. Creating a sample project – the temperature converter
        1. List of requirements
        2. User interface concept design
          1. Creating the project
      3. Creating a Java module
      4. Creating the TemperatureConverterActivityTests class
        1. Creating the fixture
        2. Creating the user interface
        3. Testing the existence of the user interface components
        4. Getting the IDs defined
        5. Translating requirements to tests
          1. Empty fields
          2. View properties
        6. Screen layout
      5. Adding functionality
        1. Temperature conversion
        2. The EditNumber class
        3. The TemperatureConverter unit tests
        4. The EditNumber tests
        5. The TemperatureChangeWatcher class
        6. More TemperatureConverter tests
        7. The InputFilter tests
      6. Viewing our final application
      7. Summary
    14. 7. Behavior-driven Development
      1. Given, When, and Then
      2. FitNesse
        1. Running FitNesse from the command line
        2. Creating a TemperatureConverterTests subwiki
          1. Adding child pages to the subwiki
          2. Adding the acceptance test fixture
          3. Adding the supporting test classes
      3. GivWenZen
        1. Creating the test scenario
      4. Summary
    15. 8. Testing and Profiling Performance
      1. Ye Olde Logge method
      2. Timing logger
      3. Performance tests in Android SDK
        1. Launching the performance test
          1. Creating the LaunchPerformanceBase instrumentation
          2. Creating the TemperatureConverterActivityLaunchPerformance class
        2. Running the tests
      4. Using the Traceview and dmtracedump platform tools
      5. Dmtracedump
      6. Microbenchmarks
        1. Caliper microbenchmarks
          1. Benchmarking the temperature converter
          2. Running Caliper
      7. Summary
    16. 9. Alternative Testing Tactics
      1. Code coverage
        1. Jacoco features
      2. Temperature converter code coverage
        1. Generating code coverage analysis report
        2. Covering the exceptions
      3. Introducing Robotium
        1. Adding Robotium
        2. Creating the test cases
          1. The testFahrenheitToCelsiusConversion() test
          2. Testing between Activities
      4. Testing on the host's JVM
        1. Comparing the performance gain
        2. Adding Android to the picture
      5. Introducing Robolectric
        1. Installing Robolectric
        2. Adding resources
        3. Writing some tests
      6. Google's march on shadows
      7. Introducing Fest
      8. Introducing Spoon
      9. Introducing Fork
      10. Summary
    17. Index