Category Archives: Automation

Using the Gradle wrapper on your Android project

I have an android project I’ve been working on for a few weeks. I got a new computer recently and I wanted to work on this project. I downloaded the Android SDK and gradle. When I tried to run a build:

1
gradle assembleDebug

I got this error:

1
Gradle version 2.2 is required. Current version is 2.11. If using the gradle wrapper, try editing the distributionUrl in /home/you/repos/asdf/gradle/wrapper/gradle-wrapper.properties to gradle-2.2-all.zip

Read more »

Project scaffolding with Yeoman

Every time I start a new project I go to old projects to review and copy my build steps and linting rules among other things. A good developer would have automated the creation of a new project instead of going back every time. I want to redeem myself so I’m taking a look a Yeoman.

Yeoman is a tool for scaffolding web apps. It basically allows you to create custom reusable app skeletons called generators.

Install

1
npm install -g yo

Read more »

Enforce android coding style with checkstyle

If you appreciate elegant code that is easier to read and consistent from file to file you probably want to start using checkstyle on you java projects.

If your project is a simple java project that uses gradle you can start using checkstyle by adding this to your build.gradle file:

1
apply plugin: 'checkstyle'

and creating this file under config/checkstyle/checkstyle.xml (I stole it from Marco’s example):

Read more »

Android UI Automation using Espresso

I asked around my Android developers which was the best framework out there for Android automation and the most convincing answer was Espresso, so I decided to give it a try. Espresso is designed to be used in environments where the developers write their own tests (which I think should be everywhere), and promises a concise and beautiful syntax.

Set up

To use espresso you have to set up a test project. I decided I was going to place all my automation tests under tests/automation so I created that folder, moved into it and ran this command:

1
2
3
4
android create test-project \
-n MyProjectAutomation \
-p . \
-m ../../

Read more »

Customizing PMD rules

PMD allows you to perform code static analysis for your project, but sometimes the default doesn’t fit the way you decided to write code. The good thing is that you can customize the rules you want to use to fit your preferences.

To customize the rules you will need to create an xml file with this structure:

1
2
3
4
5
6
7
< ?xml version="1.0"?>
<ruleset name="Custom ruleset"
   xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>Rules for my project</description>
</ruleset>

Read more »

Java code static analysis with Pmd

Pmd is a tool for running code static analysis for multiple languages. The first thing you need to use it is download it from Pmd’s website. Clicking the download button will download a zip file. Uncompress that zip and you will have all you need.

Running code static analysis

Once you have pmd on your computer you can analyse your code using this command:

1
<path to pmd>/bin/run.sh pmd -d <src folder> -l java -f <reporting format> -R <rules>

Read more »

Detect when Android emulator is ready

I am writing an Android app, so as any good developer I want to have some tests in place that run continuously to make sure my app doesn’t break. The thing about Android is that I need an emulator in order to run my unit tests, so I need a way to start the emulator and detect that it is ready to be used. To do this we need to verify if the boot animation has finished using this command:

1
adb shell getprop init.svc.bootanim

Now, the only thing we need to do is call this command constantly until we get “stopped”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash

# Kill emulator
adb -s emulator-5554 emu kill

# Start the emulator
emulator -avd NexusOne -gpu on -qemu -enable-kvm &

# Don't exit until emulator is loaded
output=''
while [[ ${output:0:7} != 'stopped' ]]; do
  output=`adb shell getprop init.svc.bootanim`
  sleep 1
done

Instant mock testing with PowerMock – Book Review

A few weeks ago someone from Packt publishing contacted me offering me a free copy of Instant mock testing with PowerMock if I accepted to write a review afterwards. Since I’ve been lately suffering with Unit Testing on Android I accepted and here is my review.

The book is really short and focused, which made it a very fast ready. I like this because I don’t really want to spend a lot of time learning how to mock something, it should be really straight forward.

Read more »

Hooking your github project to Travis-CI

I have a little open source project that I am trying to slowly improve. One of the steps I’m taking to do this is to add some tests and code static analysis. If something is running correctly I don’t want regressions so I need to plug it to CI so it runs for every commit. A lot of people are using Travis so I decided to give it a try. The first steps can be found at Travis’ getting started page.

My project is a PHP project but it needs node to run grunt tasks so I was worried about not being able to specify two programming languages in the yml file. Luckily Travis includes a version of node on all VMs no matter what type of project you are using, so I could freely use npm and grunt:

1
2
3
4
5
language: php
php:
  - "5.4"
before_script: "npm install"
script: "./node_modules/grunt-cli/bin/grunt"

I also found that if you have a very specific requirement you can even use apt-get to download dependencies and then you will be able to use it as part of your task.

Getting started with Jenkins Continuous Integration

For those of you who don’t know, Jenkins is an application that allows you to create a Continuous Integration environment for your software projects. Most of the time it is used to periodically run test and build your project in an environment that should be similar to your production environment.

Installation

To install Jenkins on a Debian based system you only need to use:

1
sudo apt-get install jenkins

This command will install it and will automatically start it as a service. This service will be started automatically every time your system is restarted.

If you are using another OS you can download a war file from Jenkins main site and run it using:

1
java -jar jenkins.war

To see your instance of Jenkins running you can visit http://localhost:8080

Read more »