Slide 1

Slide 1 text

Automated Acceptance Testing with Cucumber and Espresso

Slide 2

Slide 2 text

Example Do you know what to test? prefill last logged-in username notify if username not set notify if password not set notify if invalid credentials display indicator while validating notify if too many attempts show “Forgot password?” en-/disable text fields and button navigate to start screen show timer when blocked after too many attempts … …

Slide 3

Slide 3 text

What? Acceptance testing “Build the right thing” Unit testing “Build the thing right” vs BDD TDD

Slide 4

Slide 4 text

Why? Motivation • having end-to-end tests − identify issues not covered by our unit tests − perform architectural-level refactorings • having a long-term test specification − due to evolution in Android testing we had to rewrite our tests a couple of times (Robotium → Robolectric → Espresso) − a high domain specific knowledge is required to write tests

Slide 5

Slide 5 text

Remember? Testing Quality “eliminating bugs”

Slide 6

Slide 6 text

Remember? Testing Design Quality “avoiding bugs”

Slide 7

Slide 7 text

How? Cucumber • tool for executable specifications • available for many major languages • tests readable/writeable for everyone • popular and vibrant community • …

Slide 8

Slide 8 text

How? Gherkin • a business readable, domain specific language • used for documentation and automated testing • understood by Cucumber • line oriented and structured by indentation • …

Slide 9

Slide 9 text

Example Gherkin Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario: Invalid credentials Given I entered my credentials When I click login Then I should see “invalid credentials”

Slide 10

Slide 10 text

Example Gherkin Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario: Invalid credentials Given I entered “andre” as username And I entered “secret” as password When I click login Then I should see “invalid credentials”

Slide 11

Slide 11 text

Example Gherkin Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario: Invalid credentials Given I entered “andre” as username And I entered “secret” as password When I click login Then I should see “invalid credentials” Scenario: Valid credentials … Scenario: No username … Scenario: No password …

Slide 12

Slide 12 text

Example Gherkin Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario Outline: Validate credentials Given I entered “” as username And I entered “” as password When I click login Then I should see “” Examples: |user |pass |msg | |andre |wrong |Invalid password.| |andre |secret |Welcome. | | |pass |No username. | |user | |No password. |

Slide 13

Slide 13 text

Gherkin? Keywords Feature Background Scenario Scenario Outline Given When Then And But * Examples

Slide 14

Slide 14 text

Gherkin? Features vs. Steps Features: • specify what to test • are written in Gherkin Steps: • specify how to test • are written in any language supported by Cucumber

Slide 15

Slide 15 text

Example Steps Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario Outline: Validate credentials Given I entered “” as username And I entered “” as password When I click login Then I should see “” Examples: |user |pass |msg | |andre |wrong |Invalid password.| |andre |secret |Welcome. | | |pass |No username. | |user | |No password. | @Given("^I entered \"(.+)\" as (.+)$") public void I_entered_(String text, String as) { //perform action on UI } @Given("^I click (.+)$") public void I_click_on_(String text) { //perform action on UI } @Given("^I should see \"(.+)\"$") public void I_should_see_(String text) { //perform assertion on UI }

Slide 16

Slide 16 text

How? Espresso

Slide 17

Slide 17 text

How? Espresso

Slide 18

Slide 18 text

How? Espresso

Slide 19

Slide 19 text

Example Steps Feature: Authentication In order to protect sensitive data, users have to login with their name and password. Scenario Outline: Validate credentials Given I entered “” as username And I entered “” as password When I click login Then I should see “” Examples: |user |pass |msg | |andre |wrong |Invalid password.| |andre |secret |Welcome. | | |pass |No username. | |user | |No password. | @Given("^I entered \"(.+)\" as (.+)$") public void I_entered_(String text, String as) { onView(withHint(as)).perform(typeText(text)); } @Given("^I click (.+)$") public void I_click_on_(String text) { onView(withText(text)).perform(click()); } @Given("^I should see \"(.+)\"$") public void I_should_see_(String text) { onView(withText(text)).check( matches(isDisplayed())); }

Slide 20

Slide 20 text

DEMO http://github.com/a11n/android-cucumber-espresso

Slide 21

Slide 21 text

BDD? Ideal vs Pragmatic • write features with customer • single source of truth • living documentation • … • specify what to test • focus on important use cases OR start each screen at least once • incorporate into build mgmt. • …

Slide 22

Slide 22 text

#ProTip Dry-Run • parses *.feature files without executing tests • discovers syntax issues • prints method stubs write features → dry-run → copy stubs → implement steps

Slide 23

Slide 23 text

Start? Resources • Web: − http://cucumber.io − https://github.com/cucumber/cucumber- jvm/tree/master/examples • Print: − The Cucumber for Java Book − Cucumber Recipes

Slide 24

Slide 24 text

Conclusion? Key aspects • Cucumber is a popular tool for BDD and automated acceptance testing • Gherkin is a business readable, domain specific language • Features specify what to test – steps specify how to test • Acceptance tests allow a quick and comprehensive test coverage