Behavior driven development - cucumber, Junit and java

49
© 2014 Leanpitch | CONFIDENTIAL Behavior Driven Development Naveen Kumar Singh CSP, PMI-ACP, CSM, PMP

Transcript of Behavior driven development - cucumber, Junit and java

Page 1: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Behavior Driven Development

Naveen Kumar Singh CSP, PMI-ACP, CSM, PMP

Page 2: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Agenda

User Story

TDD, ATDD, and BDD

BDD in Details

BDD in Action

Why Cucumber?

Page 3: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

User Story

Page 4: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

User Story

• Product: Groceries Billing System

• Stories

• As an account I want to calculate bills of groceries purchased by customer so that I can generate invoice.

• As an account I want to update groceries rates so that bills reflect correct rate

• As an account I want to issue discount coupon so I can give to customer to redeem next time.

Page 5: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Acceptance Criteria

What to check at the end of development?

Ordered Pizza online, then Pizza get delivered at home or confirmation mail in your inbox

Page 6: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Acceptance Testing

Making sure the software works correctly for the intended user in his or her normal work environment.

• Alpha test

• Version of the complete software is tested by the customer under the supervision of the developer at the developer’s site

• Beta test

• Version of the complete software is tested by the customer at his or her own site without the developer being present

Page 7: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

TDD, ATDD, and BDD

Page 8: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

ATDD BDD TDD

Page 9: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Acceptance/Customer Test Driven Development

Acceptance Test-Driven Development (ATDD) is a development methodology based on communication between the business customers, the developers, and the testers. ATDD encompasses many of the same practices as Specification by Example, Behavior Driven Development (BDD), Example-Driven Development (EDD), and Story Test-Driven Development (SDD). All these process aid developers and testers in understanding the customer’s needs prior to implementation and allow customers to be able to converse in their own domain language.

Page 10: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

ATDD Advantage

Close collaborationSeeing concrete, working softwareBuilding trust and confidenceCustomer in controlEvolving a shared language

Tests as a shared languageTests as specificationSpecification by example

Page 11: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

ATDD Test Format

Scenario: Send email

Given that a web based email module has been developedAnd I am accessing it with proper authentication

When I shall write the sender email address in To fieldAnd keeping the subject field non emptyAnd write something in the body text area which accepts rich textAnd press or click send button

Then my email will be sentAnd the event will be logged in the log file.

Page 12: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD in Details

Page 13: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Behaviour Driven Development (BDD)

BDD is a second-generation, outside-in, pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters. – Dan North

Page 14: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD

Given- Set of preconditions

Then-Some testable outcome

When-When a event occurs

Page 15: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Features

• A testable story (it should be the smallest unit that fits in an iteration)

• The title should describe an activity

• The narrative should include a role, a feature, and a benefit

• The scenario title should say what's different

• The scenario should be described in terms of Givens, Events, and Outcomes

• The givens should define all of, and no more than, the required context

• The event should describe the feature

Page 16: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Cycle

Add Test

Test Fail

CodeRerun Test

Refactor

Page 17: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Example

Story: Returns go to stock

In order to keep track of stock

As a store owner

I want to add items back to stock when they're returned

Page 18: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Example

Scenario 1: Refunded items should be returned to stock

Given a customer previously bought a black sweater from me

And I currently have three black sweaters left in stock

When he returns the sweater for a refund

Then I should have four black sweaters in stock

Page 19: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Example

Scenario 2: Replaced items should be returned to stock

Given that a customer buys a blue garment

And I have two blue garments in stock

And three black garments in stock.

When he returns the garment for a replacement in black,

Then I should have three blue garments in stock

And two black garments in stock

Page 20: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Tools

ATDD

FitNesse

Spectacular

Concordian

Thucydides

BDD

SpecFlow

Cucumber

JBehave

NBehave

Behat

TDD

JUnit

NUnit

TestNG

MSTest

Page 21: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD is an Agile Process

BDD encourages collaboration between developers, QA and non-technical or business participants in a software project.

Page 22: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD in Action

Page 23: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-GOAL

Write BDD code to calculate Groceries Bills

Page 24: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Feature File

#encoding: utf-8

Feature: Checkout

In order to calculate price of groceries

As a Store Staff

I should be able to calculate price for groceries during checkout

Scenario: Checkout a banana

Given The price of a “Banana” is $5

When I checkout 1 “Banana”

Then the total price should be $5

Page 25: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Add Test Runner Class

package test.java;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = "src/test/resources")

public class testrunner

{

}

Page 26: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Run BDD Scenarios

1 Scenarios ([33m1 undefined[0m)

3 Steps ([33m3 undefined[0m)

0m0.000s

You can implement missing steps with the snippets below:

@Given("^The price of a \"([^\"]*)\" is \\$(\\d+)$")

public void The_price_of_a_is_$(String arg1, int arg2) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

@When("^I checkout (\\d+) \"([^\"]*)\"$")

public void I_checkout(int arg1, String arg2) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

@Then("^the total price should be \\$(\\d+)$")

public void the_total_price_should_be_$(int arg1) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

Page 27: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Add Test Step

public class teststeps {

@Given("^The price of a \"([^\"]*)\" is \\$(\\d+)$")

public void The_price_of_a_is_$(String arg1, int arg2) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

@When("^I checkout (\\d+) \"([^\"]*)\"$")

public void I_checkout(int arg1, String arg2) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

@Then("^the total price should be \\$(\\d+)$")

public void the_total_price_should_be_$(int arg1) throws Throwable {

// Express the Regexp above with the code you wish you had

throw new PendingException();

}

}

Page 28: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Add Required Functionality and Test Scenarios

public class teststeps extends TestCase {

String product;

int rate, qty;

@Given("^The price of a \"([^\"]*)\" is \\$(\\d+)$")

public void The_price_of_a_is_$(String arg1, int arg2) throws Throwable {

product = arg1;

rate = arg2;

}

@When("^I checkout (\\d+) \"([^\"]*)\"$")

public void I_checkout(int arg1, String arg2) throws Throwable {

qty=arg1;

}

@Then("^the total price should be \\$(\\d+)$")

public void the_total_price_should_be_$(int arg1) throws Throwable {

if(product.equals("Banana"))

assertEquals(arg1, rate*qty);

}

}

Page 29: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

BDD Classroom Exercise-Run Scenarios Again

Page 30: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Why Cucumber?

Page 31: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber

Cucumber was originally created as a command-line tool by members of the Ruby community. It has since been translated into several different development environments, including Java, to allow more of us to enjoy its benefits.

When you run Cucumber, it reads in your specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against your system.

Each scenario is a list of steps for Cucumber to work through. So that Cucumber can understand these feature files, they must follow some basic syntax rules. The name for this set of rules is Gherkin.

Page 32: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber

Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into code to carry out whatever action is being described by the step. In a mature test suite, the step definition itself will probably just be one or two lines of code that delegate to a library of support code, specific to the domain of your application, that knows how to carry out common tasks on the system.

Sometimes that may involve using an automation library, like the browser automation library Selenium, to interact with the system itself.

Page 33: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber

Your Project Features Scenarios Steps

Your SystemAutomation

LibrarySupport

CodeStep

Definitions

Technology Facing

Business Facing

Page 34: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber

Page 35: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin-Keywords

A Gherkin file is given its structure and meaning using a set of special keywords.

There’s an equivalent set of these keywords in each of the supported spoken languages, but for now let’s take a look at the English ones:

• Feature

• Background

• Scenario

• Given

• When

• Then

• And

• But

• *

• Scenario Outline

• Examples

Page 36: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin- Background

Feature: Feedback when entering invalid credit card details

In user testing we've seen a lot of people who made mistakes entering their credit card. We need to be as helpful as possible here to avoid losing users at this crucial stage of the transaction.

Background:Given I have chosen some items to buyAnd I am about to enter my credit card details

Scenario: Credit card number too short

When I enter a card number that's only 15 digits longAnd all the other details are correctAnd I submit the formThen the form should be redisplayedAnd I should see a message advising me of the correct number of digits

Page 37: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin- Multiple AND

Scenario: Expiry date invalid

When I enter a card expiry date that's in the pastAnd all the other details are correctAnd I submit the formThen the form should be redisplayedAnd I should see a message telling me the expiry date must be wrong

Page 38: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin- Replacing Given/When/Then with Bullets

Some people find Given, When, Then, And, and But a little verbose. There is anadditional keyword you can use to start a step: * (an asterisk). We could havewritten the previous scenario like this:

Scenario: Attempt withdrawal using stolen card* I have $100 in my account* my card is invalid* I request $50* my card should not be returned* I should be told to contact the bank

To Cucumber, this is exactly the same scenario. Do you find this version easier to read? Maybe. Did some of the meaning get lost? Maybe. It’s up to you and your team how you want to word things. The only thing that matters is that everybody understands what’s communicated.

Page 39: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin- Comments

# This feature covers the account transaction and hardware-driver modulesFeature: Withdraw CashIn order to buy beerAs an account holderI want to withdraw cash from the ATM

# Can't figure out how to integrate with magic wand interfaceScenario: Withdraw too much from an account in creditGiven I have $50 in my account# When I wave my magic wandAnd I withdraw $100Then I should receive $100

Page 40: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Gherkin- Doc Strings

Doc strings just allow you to specify a larger piece of text than you could fit on a single line. For example, if you need to describe the precise content of an email message, you could do it like this:

Scenario: Ban Unscrupulous UsersWhen I behave unscrupulouslyThen I should receive an email containing:"""Dear Sir,Your account privileges have been revoked due to your unscrupulous behavior.Sincerely,The Management"""And my account should be locked

Page 41: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Step Definitions

In Cucumber, results are a little more sophisticated than a simple pass or fail. A scenario that’s been executed can end up in any of the following states:

• Failed• Pending• Undefined• Skipped• Passed

These states are designed to help indicate the progress that you make as youdevelop your tests.

Page 42: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Step Definitions

Failed – During execution based on AssertPassed – During execution based on AssertPending – Pending exceptionUndefined – Steps are missing in step fileSkipped – All pending and undefined

Page 43: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Step Definitions

Element Purpose Default

dryRun true (Skip execution of glue code) False

strict true (will fail execution if there are undefined or pending steps)

False

glue where to look for glue code (stepdefs and hooks) {}

features the paths to the feature(s) False

monochrome whether or not to use monochrome output False

format what formatter(s) to use {}

tags hat tags in the features should be executed {}

Page 44: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Step Definitions

@CucumberOptions(

dryRun = false,

strict = true,

features = "src/test/features/com/sample",

glue = "com.sample",

tags = { "~@wip", "@executeThis" },

monochrome = true,

format = {"pretty", "html:target/cucumber", "json:target_json/cucumber.json",

"junit:taget_junit/cucumber.xml“ }

)

Page 45: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Step Definitions

dryRun: if dryRun option is set to true then cucumber only checks if all the steps have their corresponding step definitions defined or not. The code mentioned in the Step definitions is not executed. This is used just to validate if we have defined a step definition for each step or not.

Strict: if strict option is set to false then at execution time if cucumber encounters any undefined/pending steps then cucumber does not fail the execution and undefined steps are skipped and BUILD is SUCCESSFUL.

Monochrome: if monochrome option is set to False, then the console output is not as readable as it should be. may be the output images will make this more clear.

Page 46: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Cucumber- Regular Expression

Step definitions use regular expressions to declare the steps that they can handle. Because regular expressions can contain wildcards, one step definition can handle several different steps.

Page 47: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Page 48: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Our Upcoming CSD Training – Please check Scrum Alliance

Please Reach out to [email protected]

+91 9902377677

Page 49: Behavior driven development - cucumber, Junit and java

© 2014 Leanpitch | CONFIDENTIAL

Naveen Kumar Singh

[email protected]

Agile Coach

http://in.linkedin.com/in/naveenkumarsingh1

Twitter - @naveenhome