Wednesday, July 27, 2016

How to run Cucumber feature tests in android, quick steps for Android Studio 2.1





These steps are not for UI tests, if you want to do acceptance tests emulating user interaction with the UI better use Calabash.


  1. Create a new android project in Android Studio.
     
  2. Open build.gradle and add the following dependencies

    androidTestCompile 'junit:junit:4.12'
    androidTestCompile group: 'info.cukes', name: 'cucumber-java', version: '1.2.2'
    androidTestCompile group: 'info.cukes', name: 'cucumber-core', version: '1.2.2'
    androidTestCompile group: 'info.cukes', name: 'cucumber-html', version: '0.2.3'
    androidTestCompile group: 'info.cukes', name: 'cucumber-jvm-deps', version: '1.0.3'
    androidTestCompile group: 'info.cukes', name: 'gherkin', version: '2.12.2'
    androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'
    androidTestCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.2'

    it must be that exact version in all cucumber libraries, the latest version doesn't work due to they are compiled with java8, and java8 is not supported yet in android (soon, soon, we hope)

     
  3. We need to setup in gradle that the tests will be executed using Cucumber. Add the instrumentation runner in build.gradle, in the defaultConfig setup

    defaultConfig {
        applicationId "my.application.package"    minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"    
        testInstrumentationRunner "cucumber.api.android.CucumberInstrumentation"
    }
     
  4. Create a new assets folder for the cucumber features in src/androidTest/assets
     
  5. This new folder is not recognized by gradle yet, set the new assets folder in build.gradle exactly, e.g. below the defaultConfig

    defaultConfig {
        ...
    }
    sourceSets {
        androidTest {
            assets.srcDirs = ['src/androidTest/assets']
        }
    }
     
  6. Create a new folder named "features" in the assets folder. All the feature files will stored be here (you can use subfolders). Example of a feature, can be on any file, cucumber doesn't care about the name of the file:

    Feature: Calculator
    
      Scenario Outline: a simple sum
        Given I have numbers 2 and 3
        When I sum
        Then the result is 5
     
  7. This is an important step. Create your tests in the subpackage "test" of your main package id, e.g. my.application.package.test

    A test example for the previous feature:

    @CucumberOptions(features = "features")
    public class CalculatorFeatureTest {
    
        private int mNum1;
        private int mNum2;
        private int mResult;
    
        @Given("^I have numbers (-*\\d+) and (-*\\d+)$")
        public void i_have_numbers_and(int n1, int n2) throws Throwable {
            mNum1 = n1;
            mNum2 = n2;
        }
    
        @When("^I sum$")
        public void i_sum() throws Throwable {
            mResult = mNum1 + mNum2;
        }
    
        @Then("^the result is (-*\\d+)$")
        public void the_result_is(int result) throws Throwable {
            assertEquals(mResult, result);
        }
    
    }

    The annotation @CucumberOptions specifies the folder inside the assets for all the feature files, in this case "features"
     
  8. Right click the package with the tests, and choose "Run tests in ..."
     
  9. You are done







No comments:

Post a Comment