项目作者: configurer

项目描述 :
Credit to https://github.com/emmasuzuki/CucumberEspressoDemo
高级语言: Java
项目地址: git://github.com/configurer/EspressoCucumberAndroid.git
创建时间: 2017-03-13T06:55:24Z
项目社区:https://github.com/configurer/EspressoCucumberAndroid

开源协议:MIT License

下载


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.

As of 1/27/2015, there are not good cucumber support on Android Studio.
It would be a little bit of pain that you need to manually translate behavior into step definition annotation.
Hopefully, Gherkin plugin (Cucumber plugin) (https://plugins.jetbrains.com/plugin/7211?pr=androidstudio) is fixed soon to work with Android Studio 1.0.2.
If Gherkin plugin starts to work with Android Studio, it should make BDD more fun.

UPDATE (12/08/2015):

Gherkin plugin is working 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. Create custom instrumentation runner

    1. public class Instrumentation extends MonitoringInstrumentation {
    2. private final CucumberInstrumentationCore mInstrumentationCore = new CucumberInstrumentationCore(this);
    3. @Override
    4. public void onCreate(Bundle arguments) {
    5. super.onCreate(arguments);
    6. mInstrumentationCore.create(arguments);
    7. start();
    8. }
    9. @Override
    10. public void onStart() {
    11. super.onStart();
    12. waitForIdleSync();
    13. mInstrumentationCore.start();
    14. }
    15. }
  2. Application ID / Runner setup in app/build.gradle

    1. testApplicationId "com.emmasuzuki.cucumberespressodemo.test"
    2. testInstrumentationRunner "com.emmasuzuki.cucumberespressodemo.test.Instrumentation"
  3. Set assets directory for feature files in app/build.gradle

    1. sourceSets {
    2. androidTest {
    3. assets.srcDirs = ['src/androidTest/assets']
    4. }
    5. }
  4. Add dependencies

    1. androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    2. androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    3. androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'
    4. androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.0'

Write behavior

  1. Feature: Login
  2. Perform login on email and password are inputted
  3. Scenario Outline: Input email and password in correct format
  4. Given I have a LoginActivity
  5. When I input email <email>
  6. And I input password "<password>"
  7. And I press submit button
  8. Then I should <see> auth error
  9. Examples:
  10. | email | password | see |
  11. | espresso@spoon.com | bananacake | true |
  12. | espresso@spoon.com | lemoncake | false | <-- valid email and password
  13. | latte@spoon.com | lemoncake | true |

Write step definition

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

Write Espresso test in step definition

  1. @When("^I input email (\\S+)$")
  2. public void I_input_email(final String email) {
  3. onView(withId(R.id.email)).perform(typeText(email));
  4. }
  5. @Then("^I should (true|false) auth error$")
  6. public void I_should_see_auth_error(boolean shouldSeeError) {
  7. if (shouldSeeError) {
  8. onView(withId(R.id.error)).check(matches(isDisplayed()));
  9. } else {
  10. onView(withId(R.id.error)).check(matches(not(isDisplayed())));
  11. }
  12. }

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 emma11suzuki@gmail.com