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.
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 PRACTICAL ‘Hurray’.
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.
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.
Next I need to add 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.
Creating new Unit test for our Calculator.java by using builtin support of Android Studio.
After complete wizard I got a file with name CalculatorTest.java in src/test/packagename directory.
Our first 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.
One more unit test for Subtract operation.
Now this time I am running all unit tests in a class.
Wow our one test is failed after debugging I got the issue.
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.
After running all tests I can see my code coverage in project explorer panel as shown below.
I also see code coverage in Coverage tab as shown below.
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.
Also I can get complete report as HTML
.
Now its time to learn Instrument tests. So first we need to select Android Instrumentation Tests artifact from build variants tab.
Now add gradle dependencies and some other required tags as shown below.
After adding if you face any warning. Then you can add resolution strategy forcefully in project gradle.
Now create a new Java class in your androidTest directory.
Write a small functional test.
Note: in this code I used wait function only to slow down test execution. :)
Now its time for a magic. Run test in same manner as we run our unit test
.
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.
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.