37
Intro to TDD & BDD Test Driven Development & Behaviour Driven Development Kev McCabe In to The Box 2015 Sunday, 19 April 15

ITB2015 - Behavior Driven Development, Automation and Continuous Integration

Embed Size (px)

Citation preview

Intro  to  TDD  &  BDDTest Driven Development

& Behaviour Driven Development

Kev McCabe

In to The Box 2015

Sunday, 19 April 15

Who am I?

• Kev McCabe AKA @bigmadkev

• Freelance Code Smith & Agile Coach

• Developer for over 18 years

• Worked in TV Broadcast, Healthcare, Travel & Finance

• Adobe Community Professional

Agenda

• Types of testing

• Why Test

• TDD Intro

• BDD Intro

• TestBox BDD

Types of Testing

GUI

Integration

Unit Tests

Exploratory testing

Real World Testing Pyramid

GUI

Integration

Exploratory testing

Unit TestsManual Testing

Bugs cost money

^ Kaner, Cem; James Bach, Bret Pettichord (2001). Lessons Learned in Software Testing: A Context-Driven Approach. Wiley. p. 4. ISBN 0-471-08112-4.

One Big Lie!

What is TDD

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards

What is TDD

What it is• A tool to drive design

• Developer focused

• Works with BDD

• Non-intuitive but powerful tool

What it is not• About testing

• For the faint hearted

• A silver bullet

• Easy

TDD Process

From  Growing  Object-­‐Oriented  Software  by  Nat  Pryce  and  Steve  Freeman

What is BDD

In software engineering, behavior-driven development (BDD) is a software development process that emerged from test-driven development (TDD).Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software developers and business analysts with shared tools and a shared process to collaborate on software development, with the aim of delivering “software that matters”

What is BDD

• Dan North - http://dannorth.net/introducing-bdd • Ubiquitous language

– existing or being everywhere at the same time : constantly • Promotes communication & collaboration between

– Developers + business analysts + stakeholders • Focuses on stories or requirements rather than on functions • Focuses on what a system should do and not on how it should be

implemented – Better readability and visibility – Verify that software works but also that it meets customer

expectations

BDD Process

Story

Generate Scenarios

Pass Scenarios

Generating Scenarios

As an Insurance Company, I want to generate life insurance risk

so that we don’t lose money

Example 3 Amigos Session

In order to not lose money

as an insurance company, I want to generate life insurance risk

Example 3 Amigos Session

Scenario One

Given a customer

When they request a quote

Then we return an insurance risk

BDD

User stories & acceptance

criteria

Domain Learning

ExamplesExamplesExamplesExamplesExamples

Open Questions

New Stories

Scenario One

Given a customer under 30 years

When they request a life insurance quote

Then we return an insurance risk of low

Given a customer under 55 years

When they request a life insurance quote

Then we return an insurance risk of med

Scenario Two

Given a customer over 55 years

When they request a life insurance quote

Then we return an insurance risk of high

Scenario Three

Given a customer between <lowerLimit> & <upperLimit> When they request a life insurance quote Then we return an insurance risk of <riskResult> |lowerLimit|upperLimit|riskResult| |18 | 29 | low | |30 | 54 | med | |55 | 65 | high |

Scenario Outline

What if they smoke?

What if they drink alcohol?

What if they have known health issues? What about people under 18 and over 70?

Questions Raised

In order not to insurance under & over aged customers as an insurance company, I want to raise an exception

Additional Stories

We have learnt about our domain

The customer worries about age limits more than anything else

We also know that other items are upcoming but …… we don’t need to develop them until we have that story!

What we have learnt

Having conversations

Is more important thancapturing conversations

Is more important thanautomating conversations

www.slideshare.net/lunivore/behavior-­‐driven-­‐development-­‐11754474  Liz  Keogh  @lunivore

describe(“Customer is between 18 & 29”, function() {  it( “will return an insurance risk of low”, function() {   var customer = new customerBuilder().withAge(24).build();   expect(riskService.generateRiskRating(customer)) .toBe(“low”);   })  })  

describe(“Customer is between 30 & 54”, function() {  it( “will return an insurance risk of med”, function() {   var customer = new customerBuilder().withAge(37).build(); expect(riskService.generateRiskRating(customer)) .toBe(“med”);   })  })

Scenario to TestBox

BDD & TDD

From  Growing  Object-­‐Oriented  Software  by  Nat  Pryce  and  Steve  Freeman

TestBox is a next generation testing framework for ColdFusion that is based on BDD (Behavior Driven Development) for providing a clean obvious

syntax for writing tests. It contains not only a testing framework, runner, assertions and expectations library but also integrates with MockBox for mocking and stubbing. It also supports xUnit style of testing and MXUnit

compatibilities.

What you get!

Test Harness

Test Bundle CFC

• No matter what style, you start with a test bundle CFC

• Inherits from testbox.system.BaseSpec or not!

• URL runner caveat • Get’s lots of methods and properties for testing • TestBox will then execute all tests within 1 or

more bundles

Running your bundles

• Execute bundle (if using inheritance) via the URL – http://mysite/test/bundle.cfc?method=runRemote

• Using the TestBox Class: testbox.system.TestBox – Bundle(s) Runner – Directory Runner – SOAP Runner – HTTP/REST Runner – ANT Runner – Custom Runners

• What’s the output? We call this reporters

Reporters

• ANTJunit : A specific variant of JUnit XML that works with the ANT junitreport task • Codexwiki : Produces MediaWiki syntax for usage in Codex Wiki • Console : Sends report to console • Doc : Builds semantic HTML to produce nice documentation • Dot : Builds an awesome dot report • JSON : Builds a report into JSON • JUnit : Builds a JUnit compliant report • Raw : Returns the raw structure representation of the testing results • Simple : A basic HTML reporter • Text : Back to the 80's with an awesome text report • XML : Builds yet another XML testing report • Tap : A test anything protocol reporter • Min : A minimalistic view of your test reports

Let’s walk through an example

• The life insurance example

• We’ll use a BDD Spec to tell us when we’re done

• We’ll use TDD to drive the design of our code

What the future holds!

• Grunt JS Tasks • NodeJS Runners and Watchers • Sublime Runner • More Reporters • More focus on automation • Gherkins support