Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Test-driven Development Approach in Android - Facilitated by Important Tool-Sets

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 910
    Comment on it

    TDD in Android

     

    A couple of months ago, I happened to attend Google i.O developer conference 2017 where I was fortunate enough to meet some of the biggest and brightest Android-minds. We talked and shared amazing things which we have had learned and experienced in our professional life.

     

    I learned an important aspect which we had been avoiding since longer in our Android projects. It was Test driven development which makes your code-base lesser prone to bugs.

     

    Since then, I decided to adopt the TDD approach in my development process and to my confidence - it really worked. This article will walk you throughout the important toolsets which I personally prefer while implementing TDD approach in Android. 

     

    What is TDD in Android?

    Test-Driven Development is a software development approach which combines test-first development (TFD) which involves writing tests before actually writing your code for production.

     

    Let me break down TDD to you:

     

    Basically,

    TDD = Refactoring + TFD

    The first step towards TDD includes writing the minimum code to justify whether a test fails or not. If your test fails, then you make minor changes in your test and run it again. If your test fails again, then you need to update the functional code and retest it. Once the test passes then you can start over. This process is termed as Test First Development (TFD).

    If it fails still then you need to refactor your code to ensure that your code is not altering any existing functionality. This process is termed as Refactoring.

    In a simpler language, the combination of above two is what we refer to as the Test-driven development approach. High performing teams use this approach to build mobile apps faster.

     

    Busting the Myth - Why TDD in Android?

    There is a common notion among mobile app developers that testing is a time consuming process. But any good developer would know that testing isn’t that time consuming, often developers test is a bad practice.

     

    Setting up a TDD initially consumes some time initially when you are setting up your tests and testing frameworks.. However, in the long run it saves you a ton of time.

     

    At the initial stages of my career into app development, testing used to be a pain even for me. I felt that not setting up the right tools upfront was eating up a considerable part of work.

     

    As a result of which, I used to spend long time thinking and figuring out how to write tests instead of implementing the approach practically. But as I gained experienced by experimenting and writing the actual tests, things started to fall in pace which makes my code structure easier and with lesser bugs.

     

    Before I dive in TDD tools, let’s first look at the architecture design patterns that are required to implement and facilitate a TDD process.

     

    Selecting a suitable architectural pattern to implement TDD for Android

    In Android, we commonly deal with three important app architectural patterns: MVC, MVP and MVVM

     

    We usually go for MVP and MVVM when we select a TDD type approach, mainly because they facilitate unit testing and provide better separation of concerns.

     

    Let’s move forward into tools for TDD (assuming that have selected an architectural design pattern already!). Let me share the tools that I use personally for TDD in Android. These are some of the simpler tools that anyone can get started within a day.

     

    Your Android Toolsets for TDD?

    With this article, I am not only just listing my favorite tools to implement TDD, but some tutorials to get you started right away as well.

     

    Tools for Unit testing in Android

    Unit test involves testing the smallest testable parts (such as method, class, component or module) which are called as units in isolation from other components of an application.

     

    Unit testing can be implemented either in a manual or automated way.

    Following are some tools which i personally prefer in Unit testing:

     

    JUnit

    JUnit is the popular unit-testing framework which provides you with a simple yet convenient API to write tests. It also allows you to perform some common operations for testing such as teardown, setup and assertions etc.

     

    You can use JUnit to write unit tests and integration tests.

     

    Rule of thumb to remember about Junit - Remember to spend your time learning testing life cycles in Junit along with various important annotations such as @Test, @Before, @Rule and @After.

     

    Also, remember that each JUnit test should not depend on any other tests and remains isolated from them. Also, don’t pay attention to the order in which these tests run.

     

    Mockito

    Many times you can’t test your code while doing the correct approach in Unit test because it requires isolation from any sort of dependencies you have with different components in your application. This is where you use Mockito.

     

    Mockito is an amazing framework which provides you with a very simple API so that you can create mock objects with ease and simplicity. The most exciting part is that you can use Mockito in conjunction with JUnit.

     

    Rule of thumb to remember about Mockito - Remember to understand the difference between Mocks and stubs. Martin Fowler has written an interesting article which will make you understand the difference between these twos.

     

    Tools for Instrumentation testing in Android

    Instrumentation test are basically tests that are useful to mock User Interaction (UI) in an application such as clicking button or typing a text in a text box.

     

    These kinds of tests require actual devices and emulator to run.  

     

    Following are some example of tools which i personally use and prefer for Instrumentation testing:

     

    Espresso

    Espresso is the best framework developed by Google which allows you to write and automate your UI tests. One of the advantage that comes with Espresso is that it automatically handles test synchronization with the user interface of your application.

     

    Rule of thumb to remember about Espresso - While learning Espresso, consider trying out Espresso test recorder which is a fun way of testing. With the help of Espresso test recorder, you can actually test your Android UI without having to write an actual code.  

     

    Robolectric

    Espresso tests are great but there is a problem with them that instead of running on local JVM, they need to run on a real device or emulator. This makes it less convenient for testing Android APIs.  

     

    Robolectric makes it possible to run and implement android API testing right on your local JVM machine. It does it by providing a dummy implementation of Android SDK using shadow classes.

     

    Robolectric is not for integration testing and mocking your test objects. Rather it is for unit testing your android API.

     

    Here is a great tutorial to learn Robolectric.


    UIAutomator

    One limitation with Espresso is that it doesn’t allow you to write UI tests that involve interactions across multiple apps. For instance you are developing an app which allows user to tap a button to open image Viewer, select an image and return to the original screen to display the picture.

     

    UIAutomator is an android UI test framework which makes it possible to test such type of interactions.  It can interact with Views that are beyond your app’s boundary.

     

    Selendroid

    Selendroid or Selenium for Android is another useful Instrumentation testing framework which contains four key components -  namely webdriver client, Android driver webview, Selendroid server and Selendroid standalone.

    • Webdriver client: Webdriver client is a java client library which is based on Selenium. It must be installed in your framework setup to facilitate creation of tests case.
    • Android driver webview app: It is a built-in Android driver which is mainly required for testing mobile based web apps.
    • Selendroid server - As the name relies, it is a server which runs beside an Android app on real devices and emulators.
    • Selendroid standalone - It is a central driver class which acts as a proxy between selendroid client and selendroid server.

     

    Selendroid give developers ample number of benefits such as:

    • Compatibility with JSON Wire Protocol - Selendroid is compatible with JSON Wire protocol which was originally developed for Web application. The protocol allow app developers to evaluate multiple gestures while testing Android apps.
    • Parallel testing advantage - By simply integrating a node in Selenium grid, developers can parallely test their android apps.
    • Supports multiple versions of Android API - Selendroid supports multiple API versions from Android API 10 to Android API 19.

     

    Robotium

    Robotium is an android integration testing framework which is designed to support black box testing for Android Applications. A black box testing facilitates a developer to examine the functionality of application without actually caring of the internal structure of code or a program.

     

    With the help of Robotium, developers can write function, system and user acceptance test cases.

     

    Here is a great tutorial to learn UI testing with Robotium

     

    Conclusion

    Listed above are some helpful tools which can help speeding up your test-driven-development approach while developing android apps.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: