Android Programming by Hellman Erik

Android Programming by Hellman Erik

Author:Hellman, Erik
Language: eng
Format: epub, pdf
Publisher: Wiley
Published: 2013-10-13T16:00:00+00:00


Chapter 10

Writing Automated Tests

If I had to pick one thing that developers of mobile apps often miss, it would be testing, despite the fact that high-quality software is the result of thorough testing. Consequently, this may be the most important chapter in this book.

The traditional way of developing software has been to design the software architecture, implement the code, and finally test the code, maybe by using automated tests to some degree. However, when testing is the last thing done in the development cycle, often too little testing is done or the wrong things are tested.

Test Driven Development (TDD) takes a different approach to software development, and its principles lead to high-quality software. With TDD, you start by defining your tests based on how your application will be used. Then you start implementing code that fulfills these tests, continuing until all the tests are satisfied. At this point, you can refactor your application code to optimize it for performance and to improve the overall design.

Using TDD involves a number of tools and techniques. First, you need a unit-testing framework for writing the automated tests. This framework, which is included in the Android APIs, is the main focus of this chapter. Second, you need a continuous integration and build server. This server automatically builds your application and performs all the automated tests for every change in the code. Finally, you need a code-coverage tool that tells you how much of the application’s code is really being tested.

Android Testing Principles

On Android, you can divide tests into two categories: unit and instrumentation tests. Although within the field of TDD, there are other types of tests (for example, integration, functional, system, and component tests) in the context of Android, I’m focusing on unit and instrumentation tests.

Unit testing works on a very fine-grained level, usually on individual methods and classes with all external dependencies removed. On the other hand, instrumentation tests focus on verifying the behavior of a component (Activity, Service, BroadcastReceiver or ContentProvider) in the overall system.

The goal with unit testing is to verify that your methods behave as expected and that they can handle erroneous input without crashing. A framework called JUnit was developed for the Java programming language. JUnit is also part of the official Android APIs in the junit package along with the Android-specific testing framework under android.test. To do unit testing, you call a method in your application code and test its result for the expected output. This is called, asserting the result, and there is a ready-made utility class named Assert for this purpose. Each test you write should perform an assertion on the value, which in turn tells the testing framework whether the test passed.

Each test is called a test case, and a set of test cases is called a test suite. Each test case starts with a setup where all dependencies are created and initialized. When the test case is complete, a teardown is performed to release the resources that were created during setup.

Because the



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.