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.
As expected first see the below image.
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.
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.
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.
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.
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.
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.