Tuesday, September 29, 2015

Testing with Android Studio

Tried to write this in the order one would discover how to configure and test in Android Studio. Test support has come in many forms with different purposes in AS(Android Studio). There are/were plugins and various 3rd pty libs. AS now supplies its own test platform. None of which is configured to fly right out of the box.


Get the Android Support Library from SDK manager. Recently the manager has been moved to the setting interface. You can still get to the old sdk interface.
Testing Support
Verify you have the libs in your sdk folder.

Create a new project to test with.

Create a new blank activity app.
  • compile app
  • open the default MainActivity class
Gradle plugin testInstrumentationRunner needs overriding.
  • Manually change the build.gradle file. If you search the gradle site for testInstrumentationRunner you will see the default is “android.test.InstrumentationTestRunner”.
  • Wasn’t able to find UI way to change this value.
  • This will come in handy as it supplies a value for the debug/run config interface.
defaultConfig {
        testInstrumentationRunner ""
Open create test interface
  • right click on the MainActivity class name -> Go To -> Test
  • CTRL + SHIFT + T
Configure the test interface
  • select JUnit4 and Fix (what does fix do?)
  • select one or more methods to test and before after checks
  • should create import statments and test methods in test class
choose the destination test directory interface
  • the one that contains “src” in the path
  • this can be a larger list if you have other modules in your project
Should have a new file MainActivityTest in the test directory.
  • The build.gradel file still does not have reference to the important libs we are going to use “*”
Apply the first annotation that requires reference
  • Add @RunWith(AndroidJUnit4.class) to test class
  • Alt + Enter @RunWith to create import
  • AndroidJUnit4 class reference not found. Dependencies missing.
Add* dependencies to the project
  • modifying build.gradle is can be done via the project structure interface. (or manually)
  • select your app right click and select "open module setting (F4)
  • select the module app and choose the dependicies tab
  • find the + - buttons + add the “library dependency”
  • paste in the full lib name in the “choose library dependency” interface. It will not work with a partial match.
  • change the “scope” to Test compile.
  • OK, clean, compile
  • you should see the new references in the (app)build.gradle file.
Here are the latest libs for test support. They will change.
Add the import reference AndroidJUnit4.class
  • dependency should now exist for AndroidJUnit4.class
  • Alt + Enter AndroidJUnit4 to create import
Setup one test to Pass
  • we will use assertThat from
  • add this test method
public void DonkeyTest() throws Exception {
    assertThat("", is(""));
  • Alt + Enter “assertThat” to create import
  • Alt + Enter “is” to create import
Try running the test
  • Right click on test class file. Should see Create, debug, run
  • Select Create to generate a run configuration
  • The “Specific Instrumentation runner(optional):” is not optional. Should be already set to “”
  • if not select … and choose “”
  • Apply, Ok
  • Right click again and select run/debug
  • If you goto “Edit Configurations” you can delete what was made in this step.
Locate the results
  • Run dialog should show the Result / Command / Test Method


Getting an idea of where all the moving parts are is a good first step. Primary suspects is the presense of the testing libraries and the correct configuration of the build.gradle.
There are a lot of new api introduced which can be overwhelming. Hard to tell which is the current official storyline.
Google is making attempts at gathering this config stuff in one spot with Android Testing Blueprint


No comments:

Post a Comment