Learn the basics of Android tests

1. Before you begin

This codelab teaches you about testing and how to apply automated testing in Android apps.


  • Basic knowledge of how to navigate to project directories in Android Studio.

What you'll learn

  • What testing is.
  • What automated testing is.
  • What unit and instrumentation tests are.
  • Where to find unit and instrumentation test files in an Android Project.

What you need

  • A computer with Android Studio installed.
  • The project you created in the previous codelab in this pathway.

2. What is testing?

Testing, in the context of software, is a structured method of checking your software to make sure it works correctly. Automated testing is actual code that checks to ensure that another piece of code you wrote is working correctly.

Testing software is advantageous because it lets you weed out bugs before you release your code into the wild; it's essential to a positive user experience.

While manual testing almost always has a place, testing in Android can often be automated. Throughout the Android Basics in Kotlin course, you focus on automated tests to test the app code and the functional requirements of the app itself. In this codelab, you learn the very basics of testing in Android. In later codelabs, you learn more advanced practices of testing Android apps.

As you become familiar with Android development and testing Android apps, you should make it a regular practice to write tests alongside your app code. Creating a test every time you create a new feature in your app reduces your workload later as your app grows. It also provides a convenient way for you to make sure your app works properly without spending too much time manually testing your app.

3. Introduction to automated testing

An automated test is a piece of code that ensures another piece of code you have written is working correctly and continues to work correctly as your project continues to grow and change. Automated testing is an essential part of all software development and Android development is no exception. As such, there's no better time to introduce it than right now! When you created your first Android app, you probably saw that your main activity was located in a subfolder of the main directory. In the src directory, you may have also noticed the test and androidTest directories. These two directories are where test code is written. There are two types of automated tests in Android development: unit tests and instrumentation tests. The two directories represent these two categories of tests.


Find the unit-test code

Local tests in Android are located in the test directory and they're usually unit tests. Unit tests directly test a small piece of code to ensure it's functioning properly. With unit tests, you can test functions, classes, and properties. Local tests are executed on the Java Virtual Machine, which means they run in a development environment without needing a device or emulator. This is a fancy way of saying that unit tests run on your computer. Android Studio comes ready to run local tests automatically.

Android Studio automatically generates a simple unit test each time you create a new project. The same thing happens for instrumentation tests. It's important to note that these tests don't really do anything relevant. They only serve as a placeholder. For now, you're only going to go over where to find the test files. You dive into the content of these generated tests in a later pathway.

To find the unit-test code:

  1. Open the Birthday Card app from the previous project.
  2. If necessary, select Android from the navigation menu.
  3. Click app > java > com.example.happybirthday (test) > ExampleUnitTest.


Find the instrumentation-test code

In the context of Android Development, instrumentation test is the term for what is, usually, a user-interface test (UI test). Instrumentation tests let you test parts of an app that depend on activity and fragment lifecycles, and platform APIs and services.

Unlike unit tests, UI tests don't test code directly. Instead, they test the user interface to make sure that the correct UI components are displayed and that the user interface behaves as expected when actions are taken within the UI. Another difference is that all instrumentation tests must run on a physical device or emulator. In an earlier pathway, you set up an emulator, so this step is already taken care of.

When you run an instrumentation test on Android, what's really happening is that the test code is actually built into its own APK, just like a regular Android app. An APK is a compressed file that contains all the code and necessary files to run the app on a device or emulator. That test APK is installed on the device or emulator along with the regular app APK. The test APK then runs its tests against the app APK.

Before you run the test, take a look at what the code does.

To find the instrumentation-test code:

  1. If you're in the Android project view, click app > java > com.example.happybirthday (androidTest) > ExampleInstrumentedTest.


  1. If you're in the Project project view, click HappyBirthday > app > src > androidTest > java > com.example.happybirthday > ExampleInstrumentedTest.


4. Congratulations

You learned what testing in Android is and how to find unit and instrumentation tests in Android.