Sunday, 13 April 2014

The Ins & Outs of Mobile App Testing

Over the last decade, application testing has continually proved itself to be an important concern. When done well, testing can drastically reduce the number of bugs that make it into your release code (and thus actually affect your users). In addition, good testing approaches will help your team catch bugs earlier in the development lifecycle – resulting in a savings of both time and money (not to mention reputation with your users). Code that has good test coverage enables you and your team to make changes and introduce new features to your app without the fear of it breaking existing functionality.
shutterstock_131210207
The word “Testing” is a large umbrella, and is usually better understood when you break it down to specific types of testing. For example:
  • Unit Testing – automated tests written by developers, with each test targeting a narrow slice of application behavior.
  • Functional & Acceptance Testing – typically performed by QA personnel or automated UI testing frameworks.
  • Performance Testing – often performed manually with profiling tools (for example – heap and CPU profiling tools), though many mobile app developers are moving towards integrating app analytics to gather this data from real usage as well.
That’s certainly not an exhaustive list of the types of testing, but enough to make an important point: mobile applications face several challenges when it comes to testing. Key among those challenges are:
  • Platform & Device Diversity
  • Immature Tooling
  • Lack of Awareness
If you opt to write native applications for each target platform, then any code-level testing (i.e. – Unit Tests) will not be transportable as you move from Objective-C (iOS) to Java (Android). In addition, any scripted UI-Automation testing tools may not work for multiple platforms (or at the very least require separate scripts for each platform). Hybrid solutions like PhoneGap, or cross-compiled solutions like Xamarin can offer a single approach to unit testing (given a single codebase for multiple platforms) – but do not always offer the same level of quality as native tooling when it comes to performance profiling. Despite the trade offs involved, I’ve found in my own experience that the biggest barrier to entry in mobile app testing is often a lack of awareness of what tools are available. That is the barrier which I hope to address in this post.

Unit Testing

Unit testing for specific platforms or cross-platform tools is not difficult, and your options abound. Let’s look at a sample of some of these choices.

iOS

iOS developers who’ve been writing Objective-C for a while may be familiar with OCUnit, which shipped with XCode prior to the XCTest framework. It’s still supported in XCode 5, but the understanding is that new and future projects should focus on using XCTest.
Don’t let Apple’s sparse documentation on unit testing deter you from checking out the XCTest framework. If you’re running an OS X Server, you can also take advantage of theXCode service’s continuous integration features. As part of a continuous integration workflow, you can create “bots”, which can continually build and test your app.
Many developers prefer a Behavior-Driven-Development (BDD) style syntax for unit testing. If this describes you, be sure to check out Kiwi – a BDD style unit testing framework for iOS.
One other important mention is OCMock a mocking framework for iOS. Mocks are an indispensable part of writing adequate tests around your application’s behavior.

Android

JUnit is perhaps by far the most well known (and officially recommended by Google) testing framework for Android. The JUnit Android extensions allow you to mock Android components, but I’ve also seen quite a number of Android developers use JUnit withMockito, another Android mocking framework.
Robolectric takes a different – and very interesting – approach by allowing you to run your Android unit tests in the normal JVM (Java Virtual Machine), without the need for an emulator. This enables your tests to not only run from within your IDE, but also as part of a continuous integration workflow.

Qt

Qt made the top 5 most used CPTs in 2013. If you’re building mobile applications with Qt, you’ll be happy to know about QTestLib, a unit testing framework built by Nokia. Based on my research, it appears that QTestLib can be integrated with a 3rd party continuous integration workflow – enabling very helpful testing automation.

PhoneGap/Apache Cordova

Web-based hybrid approaches to mobile apps can take advantage of a host of testing and mocking frameworks, not to mention scripted UI/acceptance testing tools as well (more on that in a moment). When it comes to unit testing JavaScript, three of the biggest names are QUnit, Mocha and Jasmine. I’ve personally used all three, with my favorite setup including Mocha and expect.js (which provides a BDD style test syntax). Mocking and “spy” frameworks abound in JavaScript as well, with Sinon.js and JsMockito among the more popular stand-alone mocking options.
Many PhoneGap developers take advantage of tools like PhantomJS – which is a “headless” (no UI) WebKit browser, with a JavaScript API. PhantomJS can be easily integrated into a continuous integration workflow to automatically run unit tests against your hybrid mobile application’s codebase.

Xamarin

Xamarin uses a customized version of NUnit (ported from JUnit), called NUnitLite which enables you to write unit tests against your Xamarin iOS & Android projects. For any shared codebase, you can use the unit testing framework of your choice.

Scripted UI Testing

Not every team can afford to hire an army of manual QA testers, despite how valuable that can be. Automated tooling can bridge the gap.
If you’re writing native iOS and Android apps, you’re in luck. Apple provides an“Automation instrument” that will automate UI tests against your iOS mobile application. The Android SDK provides the “uiautomator” library, described as “A Java library containing APIs to create customized functional UI tests, and an execution engine to automate and run the tests.” In addition to these, you can use third party tools likeSquish, Ranorex and Perfecto Mobile’s MobileCloud Automation to automate UI tests against Android and iOS apps, web apps and more. It’s worth mentioning that Perfecto Mobile’s MobileCloud Automation exposes an API to better facilitate integration with existing build/continuous integration tools. Perfecto Mobile also offers MobileCloud Interactive, which enables you to “perform remote manual testing on real smartphones and tablets regardless of where you are” – who wouldn’t want to have a “testing army” of real mobile device users at their disposal?
Among the more interesting developments in mobile UI automated testing is the emergence of an open source project named Appium. Appium uses the WebDriverJsonWireProtocol to interact with iOS, Android and Firefox OS apps and gives you the choice of writing your UI tests in any WebDriver-compatible language (Java, Objective-C, JavaScript, PHP, Python, Ruby, C#, Clojure, Perl and others).

Performance & Profiling

Apple’s Instruments is one of the more impressive native toolsets I’ve seen recently. With Instruments, it’s possible to profile how your app executes, run stress tests, record and replay user actions, create custom instruments and a lot more. If you’re writing native iOS apps & not using Instruments, I recommend reading through the Quick Start to get up to speed.
With Android apps, you have several (albeit, lower-level) tools available: Systrace &Traceview. You can also use the Device Monitor to view memory usage based on logcatmessages.
For hybrid mobile apps, you have a host of mature desktop browser tools (Chrome Developer Tools, Firefox/Firebug, etc.), which you can bring to bear on your app to profile CPU usage, memory, DOM manipulation and much more.
Many mobile developers have started taking advantage of third party analytics services such as Google Mobile Analytics, Countly, EQATEC, Flurry, Perfecto Mobile’s MobileCloud Monitoring and many others. The focus of these kind of analytics is usually more about how your app is actually used, user engagement, demographics, feature popularity, etc. However, it provides an opportunity to measure certain pieces of application performance from within real-world usage. While I wouldn’t recommend this being your first line-of-defense in performance testing, having the ability to track real world performance metrics can be a powerful tool in tuning your application to your users’ needs.
We’ve only scratched the surface of the various testing options available for mobile app development. What testing approaches & tools are you using when writing mobile apps? If you’re not currently testing your application, what are some factors that would change your mind?
Source - http://www.developereconomics.com/testing-mobile-applications/

Sunday, 9 February 2014

Getting Started with Watir In One Minute Guide

Watir (Web Application Testing in Ruby) is a great functional testing framework for web applications. However, newcomers often have difficulties. If you do, you may find this useful.

Use TestWise RubyShell

TestWise Rubyshell is a pre-packaged Watir software on Windows platform.
  1. Install TextWise RubyShell
    Download latest RubyShell installer, double click rubyshell-1.x.x-setup.exe to install, accept all default options.
    (Default to c:\rubyshell)
  2. Try out: Watir
    You are ready to run Watir scripts. Start a Window command window,
    1.cd C:\rubyshell\samples\watir
    2.ruby google_watir.rb
  3. Try out: Watir with RSpec + Reports
    Double click C:\rubyshell\samples\watir_rspec\demo.bat

Use RubyInstaller and install Watir manually

  1. Install Ruby
    Install Ruby (recommending v1.8.7) using the one-click installer for Windows.
  2. Install Watir
    Start a Windows command window (if you are unfamiliar with this, you'd better try TestWise IDE)
    gem install watir
    If you behind a firewall and need proxy to access Internet,
    gem install -p http://proxy_server:proxy_port watir
  3. Create a sample Watir script file
    Open notepad, paste into the text below,
    1.require 'watir'
    2. 
    3.browser = Watir::IE.start("http://google.com")
    4.browser.text_field(:name, "q").set "Watir IDE"
    5.browser.button(:name, "btnG").click
    then save as c:\google_watir.rb
  4. Run Watir script
    Start a Windows command window,
    1.cd C:\
    2.ruby c:\google_watir.rb
    to run the watir test script you just created, which will start an Internet Explorer window: doing a google search.

Use TestWise IDE

Create, edit, manage and run Watir/RWebSpec test scripts in single tool.

  1. Install TestWise IDE
    Download free TestWise Community Edition installer, double click TestWise-1.x.x-setup.exe to install, accept all default options.
  2. Startup TestWise IDE
    You may launch TestWise after the installation (default) 
    Or start up normally
  3. Try it out
    TestWise will load the sample project if it is first time. 
    If not, close current project, click menu File -> Open Project, then choose c:\Program File\TestWise\samples\demo-watir\demo-watir.tprproject file.
    Select the file hello_world_watir.rb on the left, the test script will be opened in the editor.
    Right mouse click in the editor, select 'Run Ruby script'
    An Internet Explorer will be started to run the Watir script.

Installing Ruby and Watir on Windows 7

I recently started playing around with Web Application Testing in Ruby or Watir for short. As is recommended I downloaded the Homebrewer's Guide to Watir by Zeljko Filipin and started trying to install the latest versions of the RubyInstaller for Windows (currently 2.0.0) when I ran into a few problems. (Random tangent: I like the idea of using Leanpub to create and modify install documents for open source applications.)

I installed Ruby and Watir on a Windows 7 64 bit machine using the 32 bit versions and everything seems to work fine so far. Here's how I installed everything using the RubyInstaller and instructions from the guide above (note some of these instructions will be a duplicate of the guide):

Ruby Installation
  1. Open a command prompt (Run and type cmd) and type ruby -v to see if you have Ruby installed (if you don't it will say the command isn't recognized) and the version number. 
  2. I have Windows 7 64 bit but I use the 32 bit installers from RubyInstaller Downloads including:
    1. Ruby 2.0.0-p247
    2. DevKit-mingw64-32-4.7.2
  3. Run the Ruby installer and on the optional tasks page select the below items before completeing:
    1. Add Ruby executables to your PATH
    2. Associate .rb and .rbw files with this Ruby installation
  4. Open a new command prompt and type ruby -v to see if Ruby installed and the version number. 
  5. (Optional) Once installed you can update the RubyGems distributed with the installer by typinggem update --system and watching for the "RubyGems system software updated" to know when its complete. 
  6. Move on to the DevKit installation. 
DevKit Installation
  1. Run the DevKit installer but change the extraction folder to C:\devkit
  2. Open a command prompt (Run and type cmd) and change the folder to C:\devkit (use the command cd c:\devkit).
  3. Run the command ruby dk.rb init. If this step is successful you'll see a response like "[INFO] found RubyInstaller v2.0.0 at C:/Ruby200".
  4. Run the command ruby dk.rb install. If this step is successful you'll see a response like "[INFO] Installing 'C:/Ruby200/lib/ruby/site_ruby/2.0.0/rubygems/defaults/operating_system.rb'" and "[INFO] Installing 'C:/Ruby200/lib/ruby/site_ruby/devkit.rb'"
  5. Move on to Watir installation.
Watir Installation
This is where I ran into problems with the 2.0.0 version of Watir. Something about the mini_magick version erroring out. To prevent this problem we do:
  1. Run the command (still from the C:\devkit command window) gem install mini_magick -v 3.5.0 which works around the version problem. You should get a response like "2 gems installed"
  2. Then run the command gem install watir --no-ri --no-rdoc to install the rest of Watir. You should get a response like "Successfully installed watir-4.0.2-x86-mingw32 and 23 gems installed". 
Check the installation
In the same command window type (after each line of code submit the commands)

  1. irb
  2. require "watir"
  3. browser = Watir::Browser.new
  4. browser.goto "google.com"
If all is setup correctly you should get your default browser to open a new window and then browse to Google. Good luck.