Hi everyone, and welcome. In this chapter we will introduce the concept of mobile UI testing, the difference between native and cross-platform mobile testing tools, AndroidX, Android Jetpack, a high-level overview of Espresso, its features and how it works.
Testing your mobile app is a vital part of the app development process. Consistent execution of tests against your mobile app verifies app correctness, functional behavior, and usability prior to public release.
Within the mobile space, the term "cross-platform" implies support for all major mobile platforms including iOS, Android and Windows phones.
Appium is one example which also supports many programming languages. In contrast, a native tool is developed, released, and supported on a single mobile platform.
Examples include Espresso for Android and XCUITest for iOS. Espresso supports Java and Kotlin programming languages while XCUITest supports Swift and Objective-C.
What is Android Jetpack? Android Jetpack is a suite of libraries, tools, and guidance to help developers produce high quality apps. These templates or components help support best practices and simplify complex tasks, allowing developers to focus on coding. The above diagram categorizes Android Jetpack into different components like architecture, UI, Behavior, and Foundation. Test frameworks like Espresso reside under the Test package in the Foundation component.
AndroidX is an improved version of the Android support libraries that the Android team uses to develop, test, package, version, and release libraries within Jetpack.
AndroidX fully replaces the legacy support library with new libraries and includes the following features:
All packages in AndroidX use a consistent namespace starting with "Android X".
Unlike the previous support libraries, AndroidX packages are separately maintained and updated.
As mentioned in the course overview, Espresso is a testing framework provided by AndroidX and provides APIs for writing UI tests to simulate user interaction within a single target application.
Espresso tests can be executed on devices running Android 2.3.3 or API level 10 and higher.
The Espresso testing framework is an instrumentation API, meaning that the full Android environment, Android device, and Android OS are required to deploy and run tests.
"AndroidJUnitRunner" is used to run test cases.
Note that Espresso is a native testing tool for Android only and does not provide cross-platform support.
Finally, Espresso can assist integration and performance testing efforts using Java and Kotlin.
How does Espresso work? As seen in the above diagram, two APKs are used.
The first APK is the application under test and the second one is the Espresso instrumentation test APK.
After installing the two APKs on the target device, AndroidJUnitRunner will run the test suite against the application under test in the same process (such as App process).
To implement instrumented UI tests, we need to know three points:
The UI tests are separate from instrumented unit tests. They reside in a different folder called "AndroidTest".
To run a test class with AndroidJUnitRunner, the annotation
@RunWith is typically added.
A full Android environment, including emulator or physical device and Android operating system, is typically required to run test cases.
A key benefit of using Espresso is that it provides automatic synchronization of test actions with the UI of the application that you are testing.
Espresso detects when the main thread is idle and is able to run test commands at the appropriate time.
This improves the reliability of test cases and negates the need for hacky timing workarounds such as
Thread.sleep() in the test code.
Espresso is simple to use and helps provide fast, reliable feedback to developers. With less flakiness compared to other testing tools, Android developers can easily produce UI test scripts.