SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
A    S     T    E    L     S         C        O         N         S   U   L   T   I   N   G




       A NEW LOOK AT TEST-DRIVEN DEVELOPMENT

                                            Dave Astels
                                    dastels@daveastels.com




19 Rand St., Hantsport, Nova Scotia, Canada B0P 1P0 • cell: 902.691.2334 • fax: 902.684.0923 •

                                    w w w. d a v e a s t e l s . c o m
The Problem
Test Driven Development (TDD) has made it to prime time. Big companies are paying big
money to have their programmers trained in how to do TDD. It's a popular topic at confer-
ences... agile and otherwise. There are a score of books available on TDD, mine even won a Jolt
award. So it seems that everything is rosy? Everyone who's doing TDD is fully understanding it
and getting the full benefit, right?

                                                   Fat Chance!

Too few people I talk to really understand what it's really about. That means that many people
who practice TDD are not getting the full benefit from it. What's wrong?

The focus on testing

Well... one thing is that people think it's about testing. That's just not the case.

Sure, there are similarities, and you end up with a nice low level regression suite... but those are
coincidental or happy side effects. So why have things come to this unhappy state of affairs? Why
do so many not get it? Let’s start with a bit of a history lesson. A few years ago I wrote an arti-
cle on TDD for The Coad Letter 1. In it I gave some background on TDD (from Ron Jeffries):

         quot;XP originally had the rule to test everything that could possibly break. Now, however, the
         practice of testing in XP has evolved into Test-Driven Development.quot;

So way back when, they were talking about writing tests. And that's probably why it has the
testing centric vocabulary, and that is why people think it's about testing! What else would they
think when they have to talk about TestCases & TestSuites, & Tests.. and have to name
methods starting with quot;testquot;. Granted, not all the xUnit frameworks have these requirements.
 The new version of jUnit (version 4) gets away from the naming conventions and reliance on
subclassing TestCase but the rest of the vocabulary stays and the annotations used are test-
centric

The thing is, when the evolution to TDD happened what we ended up with was a different kind
of animal... not just a slight tweak on the original. The original XP folks were writing tests for
everything that could break, then they started writing the tests first. Eventually we ended up at
TDD where we write a small test to describe a small bit of new functionality, then code it, then




1   http://bdn.borland.com/article/0,1410,29690,00.html “What is Test-Driven Development?”


                                                         1
write the next small test, and so on. But TDD is not the endpoint a lot of people seem to think
it is... it's just a stepping-stone.

Units

Also, the idea of quot;unitquot; is a major problem. First of all it's a vague term, and second it implies a
structural division of the code (i.e. people think that they have to test methods or classes). We
shouldn't be thinking about units... we should be thinking about facets of behaviour.

Thinking about unit testing leads us to divide tests in a way that reflects the structural arrange-
ment of the code. For example, having text classes and production classes in a 1-1 relationship

That's not what we want... we want behavioural divisions.. we want to work at a level of granu-
larity much smaller than that of the typical unit test. As I've said before when talking about TDD,
we should be working with very small, focused pieces of behaviour... one small aspect of a single
method. Things like quot;after the add() method is called with an object when the list is empty, there
should be one thing in the listquot;. The method is being called in a very specific context, often with
very specific argument, and with a very specific outcome. I even went so far as to write a blog
entry titled “One Assert Per Test”2.

So, there you have it. A fabulous idea... wrapped in packaging that causes people to think from a
testing point of view.

                                             The Result
Why is this a problem? Let's think for a minute about how people often think about testing.

Programmers often think quot;I'm not going to write all those tests.quot;, quot;It's really simple code, it doesn't
need to be testedquot;, quot;testing is a waste of timequot;, or quot;I've done this (loop/data retrieval/functionalty, etc)
millions of times.quot;.

Project managers often think quot;we test after the code is donequot;, quot;that's what we have a testing person
forquot;, or quot;we can't spend that time nowquot;.

So with people thinking about testing, it's easy to come up with all sorts of negative reactions
and reasons not to do it... especially when time gets short and the pressure's on.




2   http://blog.daveastels.com/?p=3


                                                     2
So if it's not about testing, what's it about?
It's about figuring out what you are trying to do before you run off half-cocked to try to do it.
You write a specification that nails down a small aspect of behaviour in a concise, unambiguous,
and executable form. It's that simple. Does that mean you write tests? No. It means you write
specifications of what your code will have to do. It means you specify the behaviour of your
code ahead of time. But not far ahead of time. In fact, just before you write the code is best be-
cause that's when you have as much information at hand as you will up to that point. Like well
done TDD, you work in tiny increments... specifying one small aspect of behaviour at a time,
then implementing it.

When you realize that it's all about specifying behaviour and not writing tests, your point of
view shifts. Suddenly the idea of having a Test class for each of your production classes is ridicu-
lously limiting. And the thought of testing each of your methods with its own test method (in a
1-1 relationship) will be laughable.

                                  Sapir-Whorf hypothesis
First some background, courtesy of Wikipedia3:

      The axiom that language has controlling effects upon thought can be traced to Wil-
      helm von Humboldt's essay quot;Über das vergleichende Sprachstudiumquot;, and the notion
      has been largely assimilated into Western thought. Karl Kerenyi began his 1976 Eng-
      lish language translation of Dionysus with this passage:

      quot;The interdependence of thought and speech makes it clear that languages are not so
      much a means of expressing truth that has already been established as means of discov-
      ering truth that was previously unknown. Their diversity is a diversity not of sounds and
      signs but of ways of looking at the world.quot;


The hypothesis upon which I am basing my argument is named after the linguist and anthro-
pologist Edward Sapir and his colleague and student Benjamin Whorf. It states that

      “there is a systematic relationship between the grammatical categories of the language a
      person speaks and how that person both understands the world and behaves in it.”

For my purposes that means that the language you use shapes how you think... and if you want
to change how you think it can help to first change your language.

3http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis



                                                       3
So what to do?
First stop thinking in terms of tests. As Bob Martin has been saying for a few years “Specifica-
tion, not Verification”. What Bob means is that verification (aka testing) is all about making sure
(i.e. verifying) that your code functions correctly while specification is all about defining what it
means (i.e. specifying) for your code to function correctly.



Using something like xUnit makes this hard, due to it’s testing-centric language, so we need to
start with a new framework for specifying behaviour. Dan North of ThoughtWorks has started
the jBehave project to do just this. I and a few others are responsible for a behaviour specifica-
tion framework for Ruby called rSpec, which we will explore in more detail below.

                         A Behaviour Specification Framework
So what does a behaviour specification look like? Well, a first pass will look and work a lot like
xUnit since:

• it works quite well enough

• everyone is familiar with it

A major difference is vocabulary. Instead of subclassing TestCase, you subclass Context.
Instead of writing methods that start with test, you start them with should. It would be
preferable that you don’t have to worry about a naming pattern so you can choose the most
appropriate name. Instead of doing verification with assertions (e.g. assertEquals(ex-
                actual)) you specify post conditions with something like shouldBeE-
pected,
qual(actual, expected).

In Smalltalk, and Ruby, this can be even more natural if we embed the framework into the class
library (a common approach in Smalltalk, by the way). You could write something like the exam-
ples shown in the following table:

              JUNIT                             SBSPEC                          RSPEC

 assertEquals(expected, actual)   actual shouldEqual: expected    actual.should_equal expected

 assertNull(result)               result shouldBeNil              result.should_be_nil




                                                       4
JUNIT                          SBSPEC                              RSPEC

 try {                         [2 / 0] shouldThrow: DivideByZero   {2 / 0}.should_throw DivideByZero
    2 / 0;
    fail();
 } catch (DivideByZero ex) {
 }


                                        What Now?
As mentioned above, Dan North has started the jBehave project to create a jUnit replacement
for behaviour specification that you can download and experiment with now.

If you are using Ruby, you can get our rSpec framework and start using it. At the time of this
writing rSpec provides the following methods which can be called on any object... take note of
that.. ANY OBJECT:

  ‣     should_equal(expected)

  ‣     should_not_equal(expected)

  ‣     should_be_same_as(expected)

  ‣     should_not_be_same_as(expected)

  ‣     should_match(expected)

  ‣     should_not_match(expected)

  ‣     should_be_nil()

  ‣     should_not_be_nil()

  ‣     should_be_empty()

  ‣     should_not_be_empty()

  ‣     should_include(sub)

  ‣     should_not_include(sub)

  ‣     should_be_true()

  ‣     should_be_false()



                                                 5
All expectation methods will take an optional, trailing message parameter. Methods setup and
teardown are available for overriding as in xUnit. They work the same and do the same thing.

So what does a behaviour specification look like? Well, here’s the example from my TDD book,
reworked for rSpec:

     require 'spec'
     require 'movie'
     require 'movie_list'

     class EmptyMovieList < Spec::Context

        def setup
          @list = MovieList.new
        end

        def should_have_size_of_0
          @list.size.should_equal 0
        end

        def should_not_include_star_wars
          @list.should_not_include quot;Star Warsquot;
        end

     end

     class OneMovieList < Spec::Context

        def setup
          @list = MovieList.new
          star_wars = Movie.new quot;Star Warsquot;
          @list.add star_wars
        end

        def should_have_size_of_1
          @list.size.should_equal 1
        end

        def should_include_star_wars
          @list.should_include quot;Star Warsquot;
        end

     end




                                              6
Guidelines

Name your Context classes by what they are focused on: EmptyMovieList and One-
MovieList, for example. They contain only specification method directly related to that con-
text.

Name your specification methods by the focus of that specification, e.g.
should_have_size_of_0.

Together, the names of the Context class and expectation method should read well, to tell you
exactly with is going on: EmptyMovieList.should_have_size_of_0. Just think of
how easy it would be to extract a specification document from this.

Your specification methods should be as simple, short, and focused as you can make them. One
expectation... excellent... that should be your holy grail. Simple is good. It’s far better to have
lots of small, simple, focused, understandable classes and methods than fewer large, bloated
classes and long, complex methods.

Summary

• The problem I have with TDD is that its mindset takes us in a different direction... a wrong
  direction.

• We need to start thinking in terms of behavior specifications, not verification tests.

• The value of doing this will be thinking more clearly about each behaviour, relying less on
  testing by class or by method, and having better executable documentation.

• Since TDD is what it is, and everyone isn't about to change their meaning of that name (nor
  should we expect them to or want them to), we need a new name for this new way of work-
  ing, which Dan North has given us... BDD: Behaviour Driven Development.

Acknowledgements

My thanks go out to Steven Baker, Gabriel Bauman, and Aslak Hellesoy for taking the first ver-
sion of this article and starting work on a ruby framework to make the ideas concrete. Thanks
to Kay Pentecost for her ideas on test-avoidance as well as feedback on draft versions.




                                                 7

Más contenido relacionado

Destacado

Why a name is the game: Trancend's guide to brand naming
Why a name is the game: Trancend's guide to brand namingWhy a name is the game: Trancend's guide to brand naming
Why a name is the game: Trancend's guide to brand namingPrathish Nair
 
GOF patterns in Ruby
GOF patterns in RubyGOF patterns in Ruby
GOF patterns in RubyRoss Lawley
 
Anti Patterns2008
Anti Patterns2008Anti Patterns2008
Anti Patterns2008Ross Lawley
 
Tablets e Ingles
Tablets e  InglesTablets e  Ingles
Tablets e Inglesguest8cbf9b
 
TrancendOrb Brand Consulting & Design 2015-16 deck of work
TrancendOrb Brand Consulting & Design 2015-16 deck of work TrancendOrb Brand Consulting & Design 2015-16 deck of work
TrancendOrb Brand Consulting & Design 2015-16 deck of work Prathish Nair
 
Learning To Love Forms Webvisions 07 21033
Learning To Love Forms Webvisions 07 21033Learning To Love Forms Webvisions 07 21033
Learning To Love Forms Webvisions 07 21033Ross Lawley
 
Trancend|Orb - Adding value to corporate brands
Trancend|Orb - Adding value to corporate brandsTrancend|Orb - Adding value to corporate brands
Trancend|Orb - Adding value to corporate brandsPrathish Nair
 

Destacado (11)

Why a name is the game: Trancend's guide to brand naming
Why a name is the game: Trancend's guide to brand namingWhy a name is the game: Trancend's guide to brand naming
Why a name is the game: Trancend's guide to brand naming
 
Learntolive
LearntoliveLearntolive
Learntolive
 
Juggling
JugglingJuggling
Juggling
 
GOF patterns in Ruby
GOF patterns in RubyGOF patterns in Ruby
GOF patterns in Ruby
 
Anti Patterns2008
Anti Patterns2008Anti Patterns2008
Anti Patterns2008
 
Tablets e Ingles
Tablets e  InglesTablets e  Ingles
Tablets e Ingles
 
TrancendOrb Brand Consulting & Design 2015-16 deck of work
TrancendOrb Brand Consulting & Design 2015-16 deck of work TrancendOrb Brand Consulting & Design 2015-16 deck of work
TrancendOrb Brand Consulting & Design 2015-16 deck of work
 
Learning To Love Forms Webvisions 07 21033
Learning To Love Forms Webvisions 07 21033Learning To Love Forms Webvisions 07 21033
Learning To Love Forms Webvisions 07 21033
 
Couch Db
Couch DbCouch Db
Couch Db
 
Trancend|Orb - Adding value to corporate brands
Trancend|Orb - Adding value to corporate brandsTrancend|Orb - Adding value to corporate brands
Trancend|Orb - Adding value to corporate brands
 
Kanban Vs Scrum
Kanban Vs ScrumKanban Vs Scrum
Kanban Vs Scrum
 

Similar a B D D Intro

Test-Driven Development
 Test-Driven Development  Test-Driven Development
Test-Driven Development Amir Assad
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven DevelopmentRabble .
 
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRYLizzyManz
 
On Selecting JavaScript Frameworks (Women Who Code 10/15)
On Selecting JavaScript Frameworks (Women Who Code 10/15)On Selecting JavaScript Frameworks (Women Who Code 10/15)
On Selecting JavaScript Frameworks (Women Who Code 10/15)Zoe Landon
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best PracticesSupun Dissanayake
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic RevisitedAdam Keys
 
A Grand Unified Theory of Software
A Grand Unified Theory of SoftwareA Grand Unified Theory of Software
A Grand Unified Theory of Softwarevinod_dinakaran
 
Specification by example - course summary
Specification by example - course summarySpecification by example - course summary
Specification by example - course summaryJakub Holy
 
Pairing w developers_stpconpics
Pairing w developers_stpconpicsPairing w developers_stpconpics
Pairing w developers_stpconpicsLanette Creamer
 
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
Hey You Got Your TDD in my SQL DB by Jeff McKenzieHey You Got Your TDD in my SQL DB by Jeff McKenzie
Hey You Got Your TDD in my SQL DB by Jeff McKenzieQA or the Highway
 
Session 2 into to qualitative research intro
Session 2   into to qualitative research introSession 2   into to qualitative research intro
Session 2 into to qualitative research introAngela Ferrara
 
jon-on reasearch.ppt
jon-on reasearch.pptjon-on reasearch.ppt
jon-on reasearch.pptSumit Roy
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven DevelopmentMichael Denomy
 
UX Research - Discussion Guide basics
UX Research - Discussion Guide basicsUX Research - Discussion Guide basics
UX Research - Discussion Guide basicsChris Spalton
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsSteven Smith
 

Similar a B D D Intro (20)

Test-Driven Development
 Test-Driven Development  Test-Driven Development
Test-Driven Development
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven Development
 
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
 
exploratory-testing - Read only not hidden
exploratory-testing - Read only not hiddenexploratory-testing - Read only not hidden
exploratory-testing - Read only not hidden
 
On Selecting JavaScript Frameworks (Women Who Code 10/15)
On Selecting JavaScript Frameworks (Women Who Code 10/15)On Selecting JavaScript Frameworks (Women Who Code 10/15)
On Selecting JavaScript Frameworks (Women Who Code 10/15)
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best Practices
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic Revisited
 
A Grand Unified Theory of Software
A Grand Unified Theory of SoftwareA Grand Unified Theory of Software
A Grand Unified Theory of Software
 
Pair programming
Pair programmingPair programming
Pair programming
 
Specification by example - course summary
Specification by example - course summarySpecification by example - course summary
Specification by example - course summary
 
Pairing w developers_stpconpics
Pairing w developers_stpconpicsPairing w developers_stpconpics
Pairing w developers_stpconpics
 
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
Hey You Got Your TDD in my SQL DB by Jeff McKenzieHey You Got Your TDD in my SQL DB by Jeff McKenzie
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
 
Session 2 into to qualitative research intro
Session 2   into to qualitative research introSession 2   into to qualitative research intro
Session 2 into to qualitative research intro
 
Intro to TDD
Intro to TDDIntro to TDD
Intro to TDD
 
jon-on reasearch.ppt
jon-on reasearch.pptjon-on reasearch.ppt
jon-on reasearch.ppt
 
Developer disciplines
Developer disciplinesDeveloper disciplines
Developer disciplines
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
UX Research - Discussion Guide basics
UX Research - Discussion Guide basicsUX Research - Discussion Guide basics
UX Research - Discussion Guide basics
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 

Más de Ross Lawley

Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
Kanban For Software Engineering Apr 242
Kanban For Software Engineering Apr 242Kanban For Software Engineering Apr 242
Kanban For Software Engineering Apr 242Ross Lawley
 
How To Fail With Agile
How To Fail With AgileHow To Fail With Agile
How To Fail With AgileRoss Lawley
 
Designing For Web2
Designing For Web2 Designing For Web2
Designing For Web2 Ross Lawley
 
User Experience Pain Free
User Experience Pain FreeUser Experience Pain Free
User Experience Pain FreeRoss Lawley
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 NotesRoss Lawley
 
Wiki Design Luke W
Wiki Design  Luke WWiki Design  Luke W
Wiki Design Luke WRoss Lawley
 
When Interface Design Attacks
When Interface Design AttacksWhen Interface Design Attacks
When Interface Design AttacksRoss Lawley
 

Más de Ross Lawley (9)

Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Kanban For Software Engineering Apr 242
Kanban For Software Engineering Apr 242Kanban For Software Engineering Apr 242
Kanban For Software Engineering Apr 242
 
How To Fail With Agile
How To Fail With AgileHow To Fail With Agile
How To Fail With Agile
 
Designing For Web2
Designing For Web2 Designing For Web2
Designing For Web2
 
User Experience Pain Free
User Experience Pain FreeUser Experience Pain Free
User Experience Pain Free
 
L R U G - JRuby
L R U G - JRubyL R U G - JRuby
L R U G - JRuby
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 Notes
 
Wiki Design Luke W
Wiki Design  Luke WWiki Design  Luke W
Wiki Design Luke W
 
When Interface Design Attacks
When Interface Design AttacksWhen Interface Design Attacks
When Interface Design Attacks
 

Último

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

B D D Intro

  • 1. A S T E L S C O N S U L T I N G A NEW LOOK AT TEST-DRIVEN DEVELOPMENT Dave Astels dastels@daveastels.com 19 Rand St., Hantsport, Nova Scotia, Canada B0P 1P0 • cell: 902.691.2334 • fax: 902.684.0923 • w w w. d a v e a s t e l s . c o m
  • 2. The Problem Test Driven Development (TDD) has made it to prime time. Big companies are paying big money to have their programmers trained in how to do TDD. It's a popular topic at confer- ences... agile and otherwise. There are a score of books available on TDD, mine even won a Jolt award. So it seems that everything is rosy? Everyone who's doing TDD is fully understanding it and getting the full benefit, right? Fat Chance! Too few people I talk to really understand what it's really about. That means that many people who practice TDD are not getting the full benefit from it. What's wrong? The focus on testing Well... one thing is that people think it's about testing. That's just not the case. Sure, there are similarities, and you end up with a nice low level regression suite... but those are coincidental or happy side effects. So why have things come to this unhappy state of affairs? Why do so many not get it? Let’s start with a bit of a history lesson. A few years ago I wrote an arti- cle on TDD for The Coad Letter 1. In it I gave some background on TDD (from Ron Jeffries): quot;XP originally had the rule to test everything that could possibly break. Now, however, the practice of testing in XP has evolved into Test-Driven Development.quot; So way back when, they were talking about writing tests. And that's probably why it has the testing centric vocabulary, and that is why people think it's about testing! What else would they think when they have to talk about TestCases & TestSuites, & Tests.. and have to name methods starting with quot;testquot;. Granted, not all the xUnit frameworks have these requirements. The new version of jUnit (version 4) gets away from the naming conventions and reliance on subclassing TestCase but the rest of the vocabulary stays and the annotations used are test- centric The thing is, when the evolution to TDD happened what we ended up with was a different kind of animal... not just a slight tweak on the original. The original XP folks were writing tests for everything that could break, then they started writing the tests first. Eventually we ended up at TDD where we write a small test to describe a small bit of new functionality, then code it, then 1 http://bdn.borland.com/article/0,1410,29690,00.html “What is Test-Driven Development?” 1
  • 3. write the next small test, and so on. But TDD is not the endpoint a lot of people seem to think it is... it's just a stepping-stone. Units Also, the idea of quot;unitquot; is a major problem. First of all it's a vague term, and second it implies a structural division of the code (i.e. people think that they have to test methods or classes). We shouldn't be thinking about units... we should be thinking about facets of behaviour. Thinking about unit testing leads us to divide tests in a way that reflects the structural arrange- ment of the code. For example, having text classes and production classes in a 1-1 relationship That's not what we want... we want behavioural divisions.. we want to work at a level of granu- larity much smaller than that of the typical unit test. As I've said before when talking about TDD, we should be working with very small, focused pieces of behaviour... one small aspect of a single method. Things like quot;after the add() method is called with an object when the list is empty, there should be one thing in the listquot;. The method is being called in a very specific context, often with very specific argument, and with a very specific outcome. I even went so far as to write a blog entry titled “One Assert Per Test”2. So, there you have it. A fabulous idea... wrapped in packaging that causes people to think from a testing point of view. The Result Why is this a problem? Let's think for a minute about how people often think about testing. Programmers often think quot;I'm not going to write all those tests.quot;, quot;It's really simple code, it doesn't need to be testedquot;, quot;testing is a waste of timequot;, or quot;I've done this (loop/data retrieval/functionalty, etc) millions of times.quot;. Project managers often think quot;we test after the code is donequot;, quot;that's what we have a testing person forquot;, or quot;we can't spend that time nowquot;. So with people thinking about testing, it's easy to come up with all sorts of negative reactions and reasons not to do it... especially when time gets short and the pressure's on. 2 http://blog.daveastels.com/?p=3 2
  • 4. So if it's not about testing, what's it about? It's about figuring out what you are trying to do before you run off half-cocked to try to do it. You write a specification that nails down a small aspect of behaviour in a concise, unambiguous, and executable form. It's that simple. Does that mean you write tests? No. It means you write specifications of what your code will have to do. It means you specify the behaviour of your code ahead of time. But not far ahead of time. In fact, just before you write the code is best be- cause that's when you have as much information at hand as you will up to that point. Like well done TDD, you work in tiny increments... specifying one small aspect of behaviour at a time, then implementing it. When you realize that it's all about specifying behaviour and not writing tests, your point of view shifts. Suddenly the idea of having a Test class for each of your production classes is ridicu- lously limiting. And the thought of testing each of your methods with its own test method (in a 1-1 relationship) will be laughable. Sapir-Whorf hypothesis First some background, courtesy of Wikipedia3: The axiom that language has controlling effects upon thought can be traced to Wil- helm von Humboldt's essay quot;Über das vergleichende Sprachstudiumquot;, and the notion has been largely assimilated into Western thought. Karl Kerenyi began his 1976 Eng- lish language translation of Dionysus with this passage: quot;The interdependence of thought and speech makes it clear that languages are not so much a means of expressing truth that has already been established as means of discov- ering truth that was previously unknown. Their diversity is a diversity not of sounds and signs but of ways of looking at the world.quot; The hypothesis upon which I am basing my argument is named after the linguist and anthro- pologist Edward Sapir and his colleague and student Benjamin Whorf. It states that “there is a systematic relationship between the grammatical categories of the language a person speaks and how that person both understands the world and behaves in it.” For my purposes that means that the language you use shapes how you think... and if you want to change how you think it can help to first change your language. 3http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis 3
  • 5. So what to do? First stop thinking in terms of tests. As Bob Martin has been saying for a few years “Specifica- tion, not Verification”. What Bob means is that verification (aka testing) is all about making sure (i.e. verifying) that your code functions correctly while specification is all about defining what it means (i.e. specifying) for your code to function correctly. Using something like xUnit makes this hard, due to it’s testing-centric language, so we need to start with a new framework for specifying behaviour. Dan North of ThoughtWorks has started the jBehave project to do just this. I and a few others are responsible for a behaviour specifica- tion framework for Ruby called rSpec, which we will explore in more detail below. A Behaviour Specification Framework So what does a behaviour specification look like? Well, a first pass will look and work a lot like xUnit since: • it works quite well enough • everyone is familiar with it A major difference is vocabulary. Instead of subclassing TestCase, you subclass Context. Instead of writing methods that start with test, you start them with should. It would be preferable that you don’t have to worry about a naming pattern so you can choose the most appropriate name. Instead of doing verification with assertions (e.g. assertEquals(ex- actual)) you specify post conditions with something like shouldBeE- pected, qual(actual, expected). In Smalltalk, and Ruby, this can be even more natural if we embed the framework into the class library (a common approach in Smalltalk, by the way). You could write something like the exam- ples shown in the following table: JUNIT SBSPEC RSPEC assertEquals(expected, actual) actual shouldEqual: expected actual.should_equal expected assertNull(result) result shouldBeNil result.should_be_nil 4
  • 6. JUNIT SBSPEC RSPEC try { [2 / 0] shouldThrow: DivideByZero {2 / 0}.should_throw DivideByZero 2 / 0; fail(); } catch (DivideByZero ex) { } What Now? As mentioned above, Dan North has started the jBehave project to create a jUnit replacement for behaviour specification that you can download and experiment with now. If you are using Ruby, you can get our rSpec framework and start using it. At the time of this writing rSpec provides the following methods which can be called on any object... take note of that.. ANY OBJECT: ‣ should_equal(expected) ‣ should_not_equal(expected) ‣ should_be_same_as(expected) ‣ should_not_be_same_as(expected) ‣ should_match(expected) ‣ should_not_match(expected) ‣ should_be_nil() ‣ should_not_be_nil() ‣ should_be_empty() ‣ should_not_be_empty() ‣ should_include(sub) ‣ should_not_include(sub) ‣ should_be_true() ‣ should_be_false() 5
  • 7. All expectation methods will take an optional, trailing message parameter. Methods setup and teardown are available for overriding as in xUnit. They work the same and do the same thing. So what does a behaviour specification look like? Well, here’s the example from my TDD book, reworked for rSpec: require 'spec' require 'movie' require 'movie_list' class EmptyMovieList < Spec::Context def setup @list = MovieList.new end def should_have_size_of_0 @list.size.should_equal 0 end def should_not_include_star_wars @list.should_not_include quot;Star Warsquot; end end class OneMovieList < Spec::Context def setup @list = MovieList.new star_wars = Movie.new quot;Star Warsquot; @list.add star_wars end def should_have_size_of_1 @list.size.should_equal 1 end def should_include_star_wars @list.should_include quot;Star Warsquot; end end 6
  • 8. Guidelines Name your Context classes by what they are focused on: EmptyMovieList and One- MovieList, for example. They contain only specification method directly related to that con- text. Name your specification methods by the focus of that specification, e.g. should_have_size_of_0. Together, the names of the Context class and expectation method should read well, to tell you exactly with is going on: EmptyMovieList.should_have_size_of_0. Just think of how easy it would be to extract a specification document from this. Your specification methods should be as simple, short, and focused as you can make them. One expectation... excellent... that should be your holy grail. Simple is good. It’s far better to have lots of small, simple, focused, understandable classes and methods than fewer large, bloated classes and long, complex methods. Summary • The problem I have with TDD is that its mindset takes us in a different direction... a wrong direction. • We need to start thinking in terms of behavior specifications, not verification tests. • The value of doing this will be thinking more clearly about each behaviour, relying less on testing by class or by method, and having better executable documentation. • Since TDD is what it is, and everyone isn't about to change their meaning of that name (nor should we expect them to or want them to), we need a new name for this new way of work- ing, which Dan North has given us... BDD: Behaviour Driven Development. Acknowledgements My thanks go out to Steven Baker, Gabriel Bauman, and Aslak Hellesoy for taking the first ver- sion of this article and starting work on a ruby framework to make the ideas concrete. Thanks to Kay Pentecost for her ideas on test-avoidance as well as feedback on draft versions. 7