Best practices for Android Unit Testing? - android

I am developing a mobile android app. What are the most common libraries/frameworks used for Android unit testing? I feel that most of the business logic, database testing, Web services testing can all be done using JUnit.
However, what's the best approach for testing UI, the UI workflow, etc? For example, how can we test if the Android app launches a web browser successfully? Or how can we confirm if buttons, certain things are pressed successfully? Or if images are loaded successfully?

I use JUnit for unit testing and Robolectric for instrumentation tests.
This article shows you a Hello World example with Robolectric
In recent times, I have been researching about integration testing in Android using Arquillian Droidium.
If you want to test some code that consumes a REST API, you can mock it with WireMock. With this library, you can mock REST APIs by code or even deploy a mock HTTP server in your own machine and set up your own mocked mappings.
For REST API mocks, I also recommend you to use Mockable.io.

2017 answer
The Android documentation has a topical series called Best Practices for Testing. I would start there.
Local Unit Tests and Instrumented Tests are set up by default when you start a new project. The general advice given is to use the local unit tests whenever possible. Sometimes this requires mocking an object that uses the Android API. The documentation recommends using Mockito in these cases. When UI tests (instrumented tests) need to be done, Android provides the Espresso framework. There are also other tools available, like the Exerciser Monkey (for stress testing) and UI Automator (for testing multiple app interaction).
Example
See this answer for how to get started with tests in Android Studio.

There is a guide for automated user interface testing on the Android Developers Website. I haven't tried it but it looks like what you've been looking for.

Related

How to create stand alone UI Automator test using UI Automator testing framework

I use Appium to create instrumented tests. I would like to switch over to a native testing client for Android, and there appear to be a couple options
1) Espresso testing framework (for single app)
2) UI Automator testing framework (for multiple apps)
and as per the suggestion, I imagine I should use UI Automator testing framework since I will be testing multiple instances of the same app on multiple devices.
However, all tutorials and information on how to use UI Automator testing framework indicate its usage from within an Android application project.
So my question is, can I create a stand alone project, say like a Gradle IntelliJ project that is not coded from within an Android Studio project?
Is this technically possible?
edit
Even the sample app includes the test within an android app module project. If I can define any apk build I want, I guess I have no problem using an application project. Maybe that's the direction I need to go in?
edit
Just looking at the sample test, it looks like the framework is deeply coupled within the android project, and I'm disappointed I can't make a stand alone framework.
What I learned is I can create a separate app module with no app source, and only test code, then use a script to automate the install of the build from another app module.
However, the test code is run internally to the device as an APK, and thus testing is restricted to one device only AFAIK for now.

Mocking web server for development

I'm curious if anyone uses a tool like MockWebServer for general development?
I've used MockWebServer to run instrumentation tests against. In a testing situation it's very easy to use because you can enqueue the response you want per test method.
I feel it could be useful for general development (not running an instrumentation test) where the endpoint hasn't been developed yet but you still want to start client side development and don't necessarily want to have a TDD approach where you run an instrumentation test every time you want to try out the feature you're developing.
Does anyone have experience using MockWebServer (or a similar tool) in this way? Are there any pitfalls to it?

How can I test integration between apps on mobile?

I am using Robotium to do the UI tests on my app, but I would like to test the integration between this app and other apps that we developed. I found about UIAutomator, but I would like to know if there is another solution to test this integration using Robotium or another framework. I don't want to mock the apps.

Android black-box testing (no access to app code). Is Google's Espresso a good choice?

I have recently been looking in to Espresso, which looks for me very promising.
I am recently working in an Android test project, and we are recently using Espresso as we see some benefits with that. Unfortunately the setup in the company I work for is such that I do not have access to the entire code for the Application we are testing.
So how I can create a test using android studio?
You can use UIAutomator to get the ids of various views.
See here
With view ids you can perform assertions/click and other actions on them
An example
onView(withId(R.id.play_search_container)).perform(click());
Hence you can go through the whole UI flow by clicking on various elements.
if you want to write a test outside of application's code, I mean: pure black-boxing, Espresso would be a pretty hard to implement as it needs a bit of application architecture (the same thing is with Robotium or `UiAutomator): names of Activities, views texts, contentDescriptions or idies.
You can use instead of Espresso:
UI/Application Exerciser Monkey
The Monkey is a program that runs on your emulator or device and
generates pseudo-random streams of user events such as clicks,
touches, or gestures, as well as a number of system-level events. You
can use the Monkey to stress-test applications that you are
developing, in a random yet repeatable manner.
monkeyrunner(requires Python language programming basics)
The monkeyrunner tool provides an API for writing programs that
control an Android device or emulator from outside of Android code.
With monkeyrunner, you can write a Python program that installs an
Android application or test package, runs it, sends keystrokes to it,
takes screenshots of its user interface, and stores screenshots on the
workstation.
The monkeyrunner tool is primarily designed to test
applications and devices at the functional/framework level and for
running unit test suites, but you are free to use it for other
purposes.
Calabash
Calabash is a framework that enables Automated UI Acceptance Tests
written in Cucumber to be run on iOS and Android applications.
Calabash works by enabling automatic UI interactions within an
application such as pressing buttons, entering text, validating
responses, etc.
From: Introduction to
Calabash
Hope it will help

Android Test Automation

How can i automate Android Apis for better quality ? How can i test the android applications to get a better quality of this software..?
I would recommend you checkout Robolectric for testing Android apps.
Use Robotium, its a good tool to use for Android Automation
http://www.robotium.org/
Try robotium, i am using it, Its very easy for writing your Functional Test Cases and can be tested in all android Devices.
Espresso is the best option coming forward to automate the android API's. Google has developed this testing kit for developers to write the unit test cases. Here's the Google's home page for espresso
https://code.google.com/p/android-test-kit/wiki/EspressoStartGuide
Google has also implemented the sample test cases which help to learn espresso faster. Main advantage of espresso is its speed of execution and its work asynchronously meaning we are not explicitly required to write wait for actions in espresso. Developer should only perform action, waiting time taken care by espresso.
Here's link which gives start to espresso in easy to understand way:
http://www.stevenmarkford.com/android-ui-testing-with-espresso-basics-tutorial/
I would suggest you to use Seetest.
This is also an automation tool independent of any platform. You can run the same test script across all the platforms like Android, IPhone, Windows Phone using this tool. Moreover it has a plugin for existing test frameworks like QTP, Testcomplete etc. Here is the link to download the trial version of the same.
http://experitest.com/support/download-2/
I would suggest Webdriver/ selenium 2.0 android driver if you are looking for open source. its good and robust as well
Try bot-bot. Its a functional automation tool built over Robotium/Nativedriver. It have recording feature which helps in easy element identification and writing test-cases.
It uses "Ant" for build and running the tests. This helps in integration with continuous integration tools.
At the end of your test execution it generates emailable html reports with pass-fail status.
If you have a spare Raspberry-Pi, you can also use that to speedup your test :) , much better than running on Emulator:
Raspberry-PI for Android Test Automation

Categories

Resources