Git Product home page Git Product logo

cucumberespressodemo's Introduction

CucumberEspressoDemo

Cucumber is BDD framework which works for iOS, Android and more. For Android, we are going to use cucumber-jvm, java port of cucumber.

BDD's behavior text is written in a business-readable domain-specific language. It aims to communicate better between non-tech to tech over Software trueness and quality.
The readable behavior also serves as documentation.

Gherkin plugin works with Android Studio 2.0. Manual translation is still required but .feature file has pretty syntax highlighting and any invalid cucumber syntax will be flagged with an error.

Install Plugin: Android Studio > Preferences > Plugins > Search "Gherkin" > Install & Restart Android Studio

Setup

  1. Add dependencies

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'
    androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.0'
    
  2. Create custom instrumentation runner under androidTest package

    public class Instrumentation extends MonitoringInstrumentation {
    
      private final CucumberInstrumentationCore mInstrumentationCore = new CucumberInstrumentationCore(this);
    
      @Override
      public void onCreate(Bundle arguments) {
        super.onCreate(arguments);
    
        mInstrumentationCore.create(arguments);
        start();
      }
    
      @Override
      public void onStart() {
        super.onStart();
    
        waitForIdleSync();
        mInstrumentationCore.start();
      }
    }
  3. Application ID / Runner setup in app/build.gradle. Make sure this matches with the package name of the test.

    testApplicationId "com.emmasuzuki.cucumberespressodemo.test"
    testInstrumentationRunner "com.emmasuzuki.cucumberespressodemo.test.Instrumentation"
    
  4. Create assets/features directory under androidTest. This directory holds behavior(.feature) files.

  5. Set assets directory in app/build.gradle.

    android {
        ...
        sourceSets {
            androidTest {
                assets.srcDirs = ['src/androidTest/assets']
            }
        }
    }
    

Write behavior

Make a file, login.feature, and put under src/androidTest/assets/features.

```
Feature: Login
Perform login on email and password are inputted

  Scenario Outline: Input email and password in correct format
    Given I have a LoginActivity
    When I input email <email>
    And I input password "<password>"
    And I press submit button
    Then I should <see> auth error

  Examples:
    | email              | password   | see   |
    | [email protected] | bananacake | true  |
    | [email protected] | lemoncake  | false |     <-- valid email and password
    | [email protected]    | lemoncake  | true  |
```

Write step definition

EX) 
"Given I have a LoginActivity" in behavior translates to
    
@Given("^I have a LoginActivity")
public void I_have_a_LoginActivity(){}
    
in step definition
    
"Then I should see error on the <view>" in behavior translates to
    
@Then("^I should see error on the (\\S+)$")
public void I_should_see_error_on_the_editTextView(final String viewName) {}

Write Espresso test in step definition

@When("^I input email (\\S+)$")
public void I_input_email(final String email) {
    onView(withId(R.id.email)).perform(typeText(email));
}
    
@Then("^I should (true|false) auth error$")
public void I_should_see_auth_error(boolean shouldSeeError) {
    if (shouldSeeError) {
        onView(withId(R.id.error)).check(matches(isDisplayed()));
    } else {
        onView(withId(R.id.error)).check(matches(not(isDisplayed())));
    }
}

Run

On command line, run with $./gradlew connectedCheck

On Android Studio, take the following steps:

  1. Run > Edit Configurations
  2. Click "+" on left pane
  3. Select Android Tests
  4. Put any name at Name:
  5. Select "app" for module
  6. Add the created custom runner for specific instrumentation runner
  7. Hit Apply
  8. Choose the craeted Run configuration
  9. Click Run

Write code to make the behavior pass

Write code and run test again. Observe the tests pass.

Any Questions ?

Please feel free to contact me at [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.