Testing Support Library

Wow. We got one more day so its time to make this day awesome by learning some thing new :).
Hello Guys. This post is the next part of Support Libraries. In this post I am showing you awesome features of Android Studio in perspective of Unit testing, Code coverage tool and Android Testing Support Library. One more thing I am using Android Studio 1.3 Beta for this tutorial.

Motivation:
As expected first see the below image.

Calculator Application
Important thing. This app contains many logical and functionality bugs so you can use that as an assignment and write robust unit and functional tests. So lets get  started :).

Boring Theory 🙁 :
Now Google taking unit testing seriously and they give an awesome support library. Currently now there are two type of tests we can write in Android Studio. Local tests and instruments test.

  • Local Test:
    These tests run on local machine JVM without Android device or emulator. Execution time is very fast and great benefit is we can run these tests on CI servers. If test has dependency on Android framework then we can use mock objects.
  • Instrumented Test:
    These tests need Android device or emulator to run. These has access to Android framework components like Context. We can use these tests where we are facing some issues in mocking objects.

Back to topic. In Support Library following automation test tools included:

  • AndroidJUnitRunner: JUnit 4 compatible Android runner
  • Espresso : UI testing framework
  • UIAutomation: UI testing framework

I think its enough theory so now start PRACTICALHurray‘.

I am expecting you can create a New Project. Now after complete wizard of project creation, click on Build Variants tab and select Test artifact as Unit test as shown in below image.

Select Unit test artifact from Build Variants
As every body knows when we create a new project. We can see a folder of androidTest. Now for local tests we need to create a new folder with name test in src directory as shown below.

Test folder creation.
Next I need to add JUnit 4 dependency in  gradle.

JUnit 4 dependency in gradle.
Good news you completed your Unit test setup in Android studio.
As I have already written a Calculator application. I am showing you code of my Calculator.java class which I use for mathematical operations in application.

Calculator util class code.
Creating new Unit test for our Calculator.java by using builtin support of Android Studio.

Unit test class creation.

Unit test class creation dialog box.
After complete wizard I got a file with name CalculatorTest.java in src/test/packagename directory.

Unit test Class.
Our first unit test.

Add operation unit test.
Its time to run unit test. We have different options like I can run all test in class or I can run only one test. So now first I am showing you how only run single test.

Running single unit test
Add unit test pass.
One more unit test for Subtract operation.

Subtract unit test.
Now this time I am running all unit tests in a class.

Running all unit tests.
All unit test result
Wow our one test is failed after debugging I got the issue.

All tests result
So you can easily imagine the power of Unit tests also.

Code coverage:
In unit testing one important thing is code coverage. There are different tools which help us to know how much our unit test doing code coverage. Code coverage means how many lines of code executes in unit tests. So if you have 100% code coverage then you can change any thing in project with full confidence because if any thing break your unit tests will fail and you easily know something bad happen. Android Studio give us a builtin code coverage tool. As shown below.

Run unit test with code coverage.
After running all tests I can see my code coverage in project explorer panel as shown below.

Code coverage result
I also see code coverage in Coverage tab as shown below.

Code coverage 2.
Also if you open the Class there you can see easily which lines are covered in unit test and which are not.
Those code lines which executes in unit test are shown in green and other are in red as shown below.

Code coverage 3
Also I can get complete report as HTML
.

Code coverage HTML report.
Now its time to learn Instrument tests. So first we need to select Android Instrumentation Tests artifact from build variants tab.

Instrument artifcat
 Now add gradle dependencies and some other required tags as shown below.

Gradle dependencies
After adding if you face any warning. Then you can add resolution strategy forcefully in project gradle.

Resolution strategy forcefully add
Now create a new Java class in your androidTest directory.

Instrument test class
Write a small functional test.
Note: in this code I used wait function only to slow down test execution. 🙂

Instrument test class code.
Now its time for a magic. Run test in same manner as we run our unit test
.Instrument test run.
As you run test. Android Studio will ask you about device or emulator because they only run on that. Now its time to see when test run, how it look like on a device.

Instrument test in Action.
Instrument test run result.
Now every thing is complete for this post. I think you enjoy and also learned some thing new from this post. OK GUYS Bye.
You can get application code from Github.

Facebooktwitterredditpinterestlinkedinmailby feather

12 thoughts on “Testing Support Library

  1. Does anyone know why Android Studio does not provide statistics (%) of branch coverage (in tracing mode)? Please help.

  2. HI

    Have you tried uploading tests done with the support library into aws device farm, testdroid or something?
    I tried creating the “test apk” and “app apk” but looks like those service don´t recognize the tests and just run “tear down” and “setup up” things… any advice?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.