behaviour-driven development

26
Behaviour-Driven Development Kerry Buckley

Upload: kerry-buckley

Post on 10-May-2015

3.233 views

Category:

Technology


0 download

DESCRIPTION

Slides from a talk to Web21C SDK team.

TRANSCRIPT

Page 1: Behaviour-Driven Development

Behaviour-Driven Development

Kerry Buckley

Page 2: Behaviour-Driven Development

Evolution

Page 3: Behaviour-Driven Development

Dirty Hacking

Page 4: Behaviour-Driven Development

Automated Testing

Page 5: Behaviour-Driven Development

Automated Testingclass Adder def add a, b a + b endend

class AdderTest < Test::Unit::TestCase def test_adder adder = Adder.new assert_equal 4, adder.add(2, 2) assert_equal 2, adder.add(4, -2) endend

Page 6: Behaviour-Driven Development

Are You Really Testing Your Code?

class Adder def add a, b a + b endend

class AdderTest < Test::Unit::TestCase def test_adder assert_equal 4, 2 + 2 endend

Page 7: Behaviour-Driven Development

Test-First Development

Page 8: Behaviour-Driven Development

Test-First Development

Failingtests

Start Done

Writecode

Writetests

Page 9: Behaviour-Driven Development

Test-Driven Development

Page 10: Behaviour-Driven Development

Test-Driven Development

Failingtest

Cleancode

All tests pass

Refactor

Page 11: Behaviour-Driven Development

State-Basedclass DongleTest < Test::Unit::TestCase def test_wibble # Set up test inputs dongle = Dongle.new dongle.addString("foo") dongle.addRemoteResource("http://foo.com/bar") # Exercise functionality under test dongle.wibble! # Verify results are as expected dongle.answer.should == 42 endend

Page 12: Behaviour-Driven Development

Bottom-Up

Page 13: Behaviour-Driven Development

Behaviour-Driven Development

Page 14: Behaviour-Driven Development

Behaviour-Driven Development

Verification Specification

State-based Interaction-based

Bottom-up Outside-in

Testing tool Design tool

Invention Discovery

Page 15: Behaviour-Driven Development

More Descriptive Test Names

class AdderTest < Test::Unit::TestCase def test_should_add_two_positive_numbers assert_equal 4, Adder.new.add(2, 2) end def test_should_add_a_positive_and_a_negative_number assert_equal 2, Adder.new.add(4, -2) endend

Page 16: Behaviour-Driven Development

Matchers

describe "An adder" do it "should add two positive numbers" do Adder.new.add(2, 2).should == 4 end it "should add a positive and a negative number" do Adder.new.add(4, -2).should == 2 endend

Page 17: Behaviour-Driven Development

Generated Documentation

$ spec -f s adder_spec.rb

An adder- should add two positive numbers- should add a positive and a negative number

Finished in 0.005493 seconds

2 examples, 0 failures

Page 18: Behaviour-Driven Development

More Matcher Examples

@string.should == "foo"

@array.should_not be_empty

@hash.should have_key(:foo)

@object.should be_an_instance_of String

lambda { @stack.pop }.should raise_error(StackUnderflowError)

Page 19: Behaviour-Driven Development

Top-Down

Page 20: Behaviour-Driven Development

Interaction-Based

Page 21: Behaviour-Driven Development

Mock Objects

•Stand-ins for collaborating objects

•Mock the interface, not a specific object

•Verify that expected calls are made

•Not stubs!

•For your code only!

Page 22: Behaviour-Driven Development

Classicists v Mockists

Page 23: Behaviour-Driven Development

Mock Objects

MockMock MockMock

Page 24: Behaviour-Driven Development

Boundary Objects

Page 25: Behaviour-Driven Development

Integration Testing

Page 26: Behaviour-Driven Development

Further ReadingIntroducing BDD (Dan North)http://dannorth.net/introducing-bdd

BDD Introductionhttp://behaviour-driven.org/Introduction

Mock Roles, Not Objects (Freeman, Mackinnon, Pryce, Walnes)http://www.jmock.org/oopsla2004.pdf

BDD in Ruby (Dave Astels)http://blog. daveastels.com/files/BDD_Intro.pdf