Testing your app is an integral part of the app development process. Testing allows you to verify the correctness, functional behavior, and usability of your app before it is released publicly. The android support testing library was announced in Google I/o. The library provides samples and APIs to run unit and integration tests. This approach is similar to the one we can find on other platforms, but also this path has already been traveled by other plugins like Robolectric.
Before we get started with testing we will cover the basic of Android testing in this post
WhyUnit testing ?
- Unit testing helps to remove or reduce bugs in code.
- Second, unit tests provide a source of good documentation. Unit tests serve a form of a sample, or example code, that gives others an appreciation of how to exercise the API that is ingrained in your software. When you work within a team to get an Android application built, you might find that your unit tests provide the guidance fellow developers first turn to when trying to figure out how to use your code.
- unit testing often improves the code and the overall design of the software. Easily testable code, it turns out, it's usually better quality code
Rule of thumb,most applications exercise roughly 70 to 80% of their application code by unit tests.
As a general rule, test what could break, and when in doubt, create a unit test around the code that you think might break. By definition, unit tests test the smallest units of code in as isolated a fashion as possible so as to ensure the unit behaves exactly as expected.
Typically in Java Android development, methods in a class are the smallest units of code for which we write unit tests. Testing individual methods that don't call on other code from other methods or other classes are good unit test starting points and are often referred to as logic unit tests.
Typically in Java Android development, methods in a class are the smallest units of code for which we write unit tests. Testing individual methods that don't call on other code from other methods or other classes are good unit test starting points and are often referred to as logic unit tests.
Functional testing is testing the application against its specifications or use cases. That is, functional tests check that certain expected inputs result in expected outputs. Functional testing is often done by testers or quality assurance analysts.
System testing tests the whole system or application and is accomplished by QA teams.
Usability testing measures how user-friendly an app is or isn't.
External Tools and dependencies to get started with testing your app.
In 2015, Google made some significant improvements to the testing tools available with Android and the Android SDK.
The standard Android testing tools are now available as part of something called the Android testing support library.
As you'll see later, this library is available through the SDK manager. Just like any other Android SDK feature, this makes getting the tools and adding them to your Android development environment, like Android Studio, quick and easy.
It includes AndroidJUnitRunner, JUnit 4 Rules, Espresso, and UI Automator.
More information about these tools and the library, in general, can be found on developers.android.com website, and at the Google GitHub site for Android testing support library.
JUnit is the most popular unit testing framework for Java code. When you need to run your JUnit tests on a device or virtual device, you'll need a runner. JUnit runners create test case instances, execute the test in the test case, and then report results.
It can run JUnit 3 or JUnit 4 test classes on Android devices to include Espresso and UI automator tests, which I'll talk about in a second.
Espresso is a relatively new testing framework. Created in 2013, it is built on top of JUnit and the Android JUnit runner exercise and test the user interface of an Android app. In the past, writing UI tests was difficult and required developers to build in a lot of custom synchronization and wait times in tests for the UI to fully display and behave reliably. Espresso is fast and requires less work on the part of the developers to interact with and check the UI elements.
UI Automator is a tool for testing user interfaces. However, its focus is really on testing the interaction between your app and another third party app or system application, like the Contacts or Settings app. The UI Automator also allows tests to include interaction with device buttons like the volume controls or the on/off button. While not technically part of the Android testing support library, two additional tools to help with Android app testing provided with the Android SDK are Monkey and monkeyrunner.
Monkey is a tool that runs on an emulator or real device, and it generates random user events,that is, clicks, touches, gestures, and system level events like garbage collection against the device. You use Monkey to stress the application and ensure the application behaves well.Monkey watches the device and reports on any application crashes, unhandled exceptions,or application not responding, ANR errors. Monkey is a black box testing tool, meaning it doesn't have to know anything about your app code and it runs from the command line.
Monkeyrunner is a tool that allows writing programs or scripts that control and test an Android device or emulator and all of its applications from outside of Android code, that is, from a workstation connected to the device or emulator.
Monkeyrunner programs are written in Python, and when running on a device or emulator, they send a scripted set of keystrokes, touch events, and other interactions to the system. These scripts can even deploy apps or be programmed to take screenshots of the device.
Robotium is another alternative Android test automation framework. It can be used to test native as well as hybrid apps. Hybrid apps are those that run native Android code as well as have HTML 5 JavaScript parts that run on a device's internal browser or web view. Robotium test cases are written in Java but require minimal knowledge of the application code. Robotium Recorder, an augmenting commercial product, creates test cases by recording user interactions with the app.
In addition, you'll find plenty of commercial and open source tools like Ranorex and Appium to help you improve the quality of your apps, and there are cloud services like Testdroid & firebase testlab that can run your test apps on hundreds of devices hosted in the cloud. These tools can often help augment your overall test strategy and give you a better assurance that your app will behave well in a multitude of platforms.
EmoticonEmoticon