behavior driven
development in
.NET
MICHAEL MCGUIRE
@MONOCULARVISION
so what’s the
problem?
behavior driven development
“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
BDD History
 Stemmed from confusion over TDD
 Where to start?
 What should I test?
 What should I not test?
 How big should a single test be?
 What should I name my tests?
 Grown to include agile analysis and automated testing
emphasize behavior over testing
 Test method names are sentences
 Keeps test methods focused
 Expressive test name is helpful when a test fails
 Test methods begin with “should”
 Keeps classes focused on doing one thing (Single
Responsibility Principle)
 Acceptance criteria should be executable
 Requirements are behavior too!
TDD
Make It
Pass
Refactor
Write
Failing
Unit Test
BDD is TDD
Make It
Pass
Refactor
Write
Failing
Unit Test
Write Failing
Acceptance
Test
Make It Pass
Automated Acceptance Criteria
 Structured user story format
 In order to <need>
 As a <role>
 I want to <feature>
 Focus put on the business need and benefit
 Scenarios give additional details about feature
 Given-When-Then
 http://www.specflow.org/
 Open source framework for the creation and execution of
automated acceptance tests in .NET
 Uses the Gherkin syntax for defining features and scenarios
 Generates NUnit or MSTest unit tests from feature file
SpecFlow Demo
driving your application
Selenium
 http://seleniumhq.org
 Pros
 Most major browsers supported -
PhantomJS, Chrome, IE, Firefox
and others
 Large community, many language
bindings
 Cons
 Simplistic API
WatiN
 http://watin.org/
 Pros
 Fairly easy to use
 Rich API (specific element types)
 Cons
 Supports only Internet Explorer and
Firefox
 Not a big community (.NET
specific)
Selenium + SpecFlow Demo
additional SpecFlow features
 Hooks
 [Before/After]TestRun
 [Before/After]Feature
 [Before/After]Scenario
 [Before/After]ScenarioBlock
 [Before/After]Step
 Tags/Tag Filtering on Hooks
 Step Argument Conversion
 [Scenario/Feature]Context
 Data Tables
additional considerations
 Setting up your application database for tests
 Attach/Detach database
 Clear/Load database tables
 Database transactions
 Faking/Mocking Database
 Imperative vs. Declarative tests
 http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-
in-user-stories.html
Links
 Behavior-Driven Development with SpecFlow and WatiN:
http://msdn.microsoft.com/en-us/magazine/gg490346.aspx
 Behavior Driven Development (BDD) with SpecFlow and ASP.NET
MVC: http://blog.stevensanderson.com/2010/03/03/behavior-
driven-development-bdd-with-specflow-and-aspnet-mvc/

Behavior Driven Development

  • 1.
  • 2.
  • 4.
    behavior driven development “BDDis 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
  • 5.
    BDD History  Stemmedfrom confusion over TDD  Where to start?  What should I test?  What should I not test?  How big should a single test be?  What should I name my tests?  Grown to include agile analysis and automated testing
  • 6.
    emphasize behavior overtesting  Test method names are sentences  Keeps test methods focused  Expressive test name is helpful when a test fails  Test methods begin with “should”  Keeps classes focused on doing one thing (Single Responsibility Principle)  Acceptance criteria should be executable  Requirements are behavior too!
  • 7.
  • 8.
    BDD is TDD MakeIt Pass Refactor Write Failing Unit Test Write Failing Acceptance Test Make It Pass
  • 9.
    Automated Acceptance Criteria Structured user story format  In order to <need>  As a <role>  I want to <feature>  Focus put on the business need and benefit  Scenarios give additional details about feature  Given-When-Then
  • 10.
     http://www.specflow.org/  Opensource framework for the creation and execution of automated acceptance tests in .NET  Uses the Gherkin syntax for defining features and scenarios  Generates NUnit or MSTest unit tests from feature file
  • 11.
  • 12.
    driving your application Selenium http://seleniumhq.org  Pros  Most major browsers supported - PhantomJS, Chrome, IE, Firefox and others  Large community, many language bindings  Cons  Simplistic API WatiN  http://watin.org/  Pros  Fairly easy to use  Rich API (specific element types)  Cons  Supports only Internet Explorer and Firefox  Not a big community (.NET specific)
  • 13.
  • 14.
    additional SpecFlow features Hooks  [Before/After]TestRun  [Before/After]Feature  [Before/After]Scenario  [Before/After]ScenarioBlock  [Before/After]Step  Tags/Tag Filtering on Hooks  Step Argument Conversion  [Scenario/Feature]Context  Data Tables
  • 15.
    additional considerations  Settingup your application database for tests  Attach/Detach database  Clear/Load database tables  Database transactions  Faking/Mocking Database  Imperative vs. Declarative tests  http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios- in-user-stories.html
  • 16.
    Links  Behavior-Driven Developmentwith SpecFlow and WatiN: http://msdn.microsoft.com/en-us/magazine/gg490346.aspx  Behavior Driven Development (BDD) with SpecFlow and ASP.NET MVC: http://blog.stevensanderson.com/2010/03/03/behavior- driven-development-bdd-with-specflow-and-aspnet-mvc/

Editor's Notes

  • #10 Given-When-Then is acceptance test analog