Getting Started with Android Development Testing: Unit Tests and Espresso UI Tests
The guide walks developers through Android testing basics by explaining JUnit unit tests, instrumented tests, and Espresso UI tests, showing where local and device tests reside, demonstrating how to generate a Calculator unit test and an Espresso test for a simple activity, and how to run them in Android Studio.
As a qualified developer, basic testing skills are essential. This article introduces Android testing, covering unit testing with JUnit, instrumented testing with Instrumentation, and UI testing with Espresso.
1. JUnit – a Java unit‑testing framework.
2. Instrumentation – built on JUnit, provides base classes for testing Android components and allows you to run tests on a device or emulator.
3. Espresso – solves concurrency issues of UI testing by providing a safe, concise API for UI interactions.
Test Types
Local unit tests run on the JVM and are located in module-name/src/test/java/ . They are fast and can use mocking libraries such as Mockito.
Instrumented tests run on an Android device or emulator and are located in module-name/src/androidTest/java/ . They have access to the Android framework and the Instrumentation API.
Adding a New Local Unit Test
1. Open the Java class you want to test (e.g., Calculator.java ).
/**
* [description]
* author: yifei
* created at 17/6/8 下午12:00
*/
public class Calculator {
public double sum(double a, double b) {
return a + b;
}
public double substract(double a, double b) {
return a - b;
}
public double divide(double a, double b) {
return a * b; // intentional example
}
public double multiply(double a, double b) {
return a / b;
}
}2. Right‑click the class and choose Create Test (or press Ctrl+Shift+T ).
3. In the dialog, select the methods to test and include @Before setup.
/**
* [description]
* author: yifei
* created at 17/6/8 下午12:01
*/
public class CalculatorTest {
private Calculator mCalculator;
@Before
public void setUp() throws Exception {
mCalculator = new Calculator();
}
@Test
public void sum() throws Exception {
// expected: 6, sum of 1 and 5
assertEquals(6d, mCalculator.sum(1d, 5d), 0);
}
@Test
public void substract() throws Exception {
assertEquals(1d, mCalculator.substract(5d, 4d), 0);
}
@Test
public void divide() throws Exception {
assertEquals(4d, mCalculator.divide(20d, 5d), 0);
}
@Test
public void multiply() throws Exception {
assertEquals(10d, mCalculator.multiply(2d, 5d), 0);
}
}Run the test by selecting the method name and choosing Run 'method()' . The results appear in Android Studio.
Creating an Espresso UI Test
First, create a simple activity ( TestActivity.java ) with an EditText , a Button , and a TextView that displays the entered text.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
}
public void init() {
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
findViewById(R.id.btnText).setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btnText) {
textView.setText("Hello, " + editText.getText().toString() + "!");
}
}
}The corresponding layout XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.testing.androidtest.TestActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/textView"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText"
android:hint="Enter your name here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView" />
<Button
android:id="@+id/btnText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!"
android:layout_below="@+id/editText" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>Now create an Espresso test ( TestActivityInstrumentationTest.java ) under src/androidTest/java . The test performs the following steps:
Find the EditText by its ID, type a string (e.g., "Peter"), and close the keyboard.
Click the button (or the view containing the text "Hello world!").
Verify that the TextView displays the expected concatenated text.
Run the test via Run > TestActivityInstrumentationTest... or by right‑clicking the class name. The test executes on the connected device or emulator, showing the UI actions and finally reporting success or failure in Android Studio.
References:
Android Testing Overview
Getting Started with Android Tests
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.