Skip to main content

cucumber-jvm and Android Studio


During this Winter holidays, I've been exploring a Behavior Driven Development and experimented with a couple of testing frameworks: Calabash and cucumber-jvm.

In this post, I would like to describe setting up cucumber-jvm for Android Studio projects.

I've extended cucumber-jvm examples with a version for Android Studio. Here is a copy in my github branch: https://github.com/mdzyuba/cucumber-jvm/tree/add_android_studio_example/examples/android/android-studio/Cukeulator.

In addition to the example code, here is some details on how it was created.

Dependencies


The cucumber-jvm requires several libraries to be added to a project: https://github.com/cucumber/cucumber-jvm/tree/master/android

I've found it's easier to add those dependencies with the Android Studio project setup dialog because it takes care of the jar versions for you.

File > Project Structure > Modules > app > Dependencies > + > Library dependency
  > type "cucumber" in the search dialog and submit a search request
  > select
      cucumber-core.jar,
      cucumber-html.jar,
      cucumber-java.jar,
      cucumber-junit.jar,
      cucumber-jvm-deps.jar,
      gherkin.jar.

 Make sure to set the scope for the libs as Test compile:


The cucumber-android.jar contains an apklib. Turns out Android Studio does not support it yet. Please see this post for more details.

A workaround is to build the jar yourself or just append @jar to the dependency:

androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'

Note, the `@jar` suffix is required in order to use the embedded jar file.

If you like to build the jar yourself, make a copy of cucumber-jvm

git clone https://github.com/cucumber/cucumber-jvm.git

Build cucumber-android jar following the steps described here: https://github.com/cucumber/cucumber-jvm/tree/master/android

Copy the jar:

cp android/target/cucumber-android-1.2.1-SNAPSHOT.jar you_project/app/libs

Add a dependency on a jar file to your project

File > Project Structure > Modules > app > Dependencies > + > File dependency
> select the cucumber-android-1.2.1-SNAPSHOT.jar
> select Test compile scope.

Once this is done, the build.gradle should have something like this:

dependencies {
    androidTestCompile 'info.cukes:cucumber-core:1.2.0'
    androidTestCompile 'info.cukes:cucumber-html:0.2.3'
    androidTestCompile 'info.cukes:cucumber-java:1.2.0'
    androidTestCompile 'info.cukes:cucumber-junit:1.2.0'
    androidTestCompile 'info.cukes:cucumber-jvm-deps:1.0.3'
    androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.0'
    androidTestCompile 'info.cukes:gherkin:2.12.2'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile files('libs/cucumber-android-1.2.1-SNAPSHOT.jar')
}


assets/features


The cucumber tests are stored in a assets/features folder. I placed them under androidTest folder:

Project/app/src/androidTest/assets

Then updated build.gradle with assets.srcDirs for androidTest:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "cukeulator.android.example.cucumber.cukeulator"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testApplicationId "cukeulator.android.example.cucumber.cukeulator.test"
        testInstrumentationRunner "cucumber.api.android.CucumberInstrumentation"
    }
    sourceSets {
        androidTest {
            assets.srcDirs = ['src/androidTest/assets']
        }
    }

At this point the project should build with no errors.

Test Code


The actual tests code is described here:
https://github.com/cucumber/cucumber-jvm/tree/master/android
and in the project examples.

There are two things required by cucumber-jvm: the tests code should be in the same package as the test packageId, and the test runner should be cucumber.api.android.CucumberInstrumentation. Therefore, I've added these two lines to the defaultConfig part of the build.gradle:

 testApplicationId "cukeulator.android.example.cucumber.cukeulator.test"
 testInstrumentationRunner "cucumber.api.android.CucumberInstrumentation"

Test Run Configuration


In order to create a new test run configuration for cucumber tests in Android Studio, select

Run > Edit Configurations
Click + and Select Android Tests
Specify:
- Test name: CalculatorTest
- Module: app
- Specific instrumentation runner: cucumber.api.android.CucumberInstrumentation
Click Ok



If you like, you can  run the tests from a command line:

cd your_project_folder
./gradlew connectedCheck

Test Run Results


The test results are displayed in the Android Studio:



Comments

Tomasio said…
thanks man, now it works in my project ;)

Popular posts from this blog

How to debug SOAP on Mac with tcpdump

I’ve been using several tools to debug SOAP on Mac and Windows. I would like to share my experience in using some of the tools and show some examples. So far I found four major categories of tools that are useful in debugging SOAP: Interface listeners such as tcpdump and others Proxy tools such as TCPMonitor Servlet filters Application server logging. For example, Weblogic has a special logging option that dumps SOAP requests and responses to the sever log. All of these tools have their own advantages and disadvantages. In this article, I will describe the first type - an interface listener tool called tcpdump. I am planning to describe other three categories in the future posts. The tcpdump is a tool that sniffs IP traffic and dumps it to a file or a standard output stream. It was originally developed by Van Jacobson, Craig Leres and Steven McCanne from the Lawrence Berkeley National Laboratory, University of California, Berkeley, CA. It is open source. The tcpdump documentation...

MySQL macport install on Mac OS X Tiger

I have installed mysql5 macport on the MacBookPro laptop running OS X Tiger 10.4. There are a few steps that needs to be done once the port is installed. These steps are not documented (see Ticket #12694 on macports.org). So, after reading a few blogs and analyzing mysql startup error messages, I figured out what needs to be done in order to get it running. That info might be useful to others, so I’ve decided to publish the solution here. First of all, we need to create the initial database database $ sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql Here is the output of the command: $ sudo ./mysql_install_db --user=mysql Installing MySQL system tables... 071118 0:06:29 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive OK Filling help tables... 071118 0:06:29 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive OK To start mysqld at boot time you hav...

Deploying BlazeDS to Weblogic 10.0

The BlazeDS is a free and open source J2EE application that provides data services to Adobe Flex and AIR applications. I’ve deployed it to Weblogic 10.0 server. I thought somebody else would be interested to do the same. Here is how. In case you need to install Weblogic 10.0, the developers copy is available for free at http://commerce.bea.com/products/weblogicplatform/weblogic_prod_fam.jsp . I have it installed on my Windows XP at D:\bea10 folder. I have created a new domain for my BlazeDS applications. It is easy to do, see http://edocs.bea.com/common/docs100/confgwiz/index.html for details. In my case, the domain name is blazeds and it is located in D:\bea10\user_projects\domains\blazeds folder on my computer. The domain name and the folders names can be difference, I just mentioned them so it is easier to follow the examples.. Once the Weblogic server is installed and a domain is ready, the BlazeDS applications can be deployed. A copy of BlazeDS is available...