These steps are not for UI tests, if you want to do acceptance tests emulating user interaction with the UI better use Calabash.
- Create a new android project in Android Studio.
- 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)
- 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" }
- Create a new assets folder for the cucumber features in src/androidTest/assets
- 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'] } }
- 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
- 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"
- Right click the package with the tests, and choose "Run tests in ..."
- You are done
I created these steps after reading http://mdzyuba.blogspot.nl/2015/01/cucumber-jvm-and-android-studio.html