© Hive Studios 2011
Behaviour Driven Development Visual Studio 2010 & SpecFlow
Ivan Pavlović, Hive Studios
Visual C# MVP, Certified ScrumMaster
http://msforge.net/blogs/paki
http://twitter.com/ipavlovi
© Hive Studios 2011
Agenda• A little bit of slides and a lot of examples
• What is BDD• Language• Tools• Examples
© Hive Studios 2011
Basic Test Types• Unit testing• Acceptance / Customer testing• Integration testing• Performance testing• Regression testing
© Hive Studios 2011
Behaviour Driven Development• One of agile techniques• Improves communication between
developers and domain experst• Example driven specification
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 (http://dannorth.net/)
© Hive Studios 2011
Gherkin (kornišon)• Domain Specific Language• Natural human language• Simple syntax– Feature– Background– Scenario, Scenario Outline– Given, When, Then
• Localized to 35+ languages
© Hive Studios 2011
Real exampleFeature: Serve coffee In order to earn money Customers should be able to buy coffee at all times
Scenario: Buy last coffee Given there are 1 coffees left in the machine And I have deposited 1$ When I press the coffee button Then I should be served a coffee
© Hive Studios 2011
Tools• Tools are file generators that consumes
*.feature files and creates test fixtures
• Cucumber – Ruby, http://cukes.info/
• SpecFlow – .NET, http://specflow.org– Nunit, MSTest, MBUnit…
• …and many others
© Hive Studios 2011
How it works1. BA or user writes a feature and scenarios
and agree with dev that it is doable2. SpecFlow generates one test per scenario3. Developer implements step definitions4. Developer writes just enough code to
make tests green5. Move on to the next scenario
© Hive Studios 2011
Step Definition Mapping … 7: Given some precondition 8: And some other precondition 9: When some action by the actor10: Then some testable outcome is achieved
[Given(@„some precondition")]public void SomePrecondition() { … do something… }
[Given(@„some other precondition")]public void SomePrecondition() { … do something… }
[When(@„some action by the actor")]public void SomePrecondition() { … do something… }
[Then(@”some verifiable result”)]Public void VerifyResult() { … do assert… }
TEST RUN
© Hive Studios 2011
DEMO
© Hive Studios 2011
Arguments - RegEx… 7: Given I have 5 apples 8: And I eat 2 of them 9: When someone asks how many apples I do have 10: Then I should answer “3 apples”
[Given(@„I have (.*) apples")]public void SomePrecondition(int numberOfApples) { … do something… }
[Given(@„ I eat (.*) of them")]public void SomePrecondition(int numberOfEatenApples) { … do something… }
[When(@„ someone asks how many apples I do have")]public void SomePrecondition() { … do something… }
[Then(@” I should answer \“(.*) apples\””)]Public void VerifyResult(int expected) { … do assert of expected … }
© Hive Studios 2011
DEMO
© Hive Studios 2011
Table-like ParametersScenario: Posting a valid entry Given I am on the posting page
And I have filled out the form as follows | Label | Value | | Your name | Jakob | | Your comment | Das ist gut! | When I click the button labelled "Post" Then I should be on the guestbook page And the guestbook entries includes the following | Name | Comment | Posted date | | Jakob | Das ist gut! | (within last minute) |
© Hive Studios 2011
Scenario outlineScenario: TC1 Add two numbers
Given I have entered 1 into the calculatorAnd I have entered 2 into the calculatorWhen I press addThen the result should be 3 on the screen
Scenario: TC2 Add two numbersGiven I have entered 2 into the calculatorAnd I have entered 2 into the calculatorWhen I press addThen the result should be 4 on the screen
© Hive Studios 2011
Scenario outlineScenario Outline: TC5Given I have entered <x> into the calculatorAnd I have entered <y> into the calculatorWhen I press addThen the result should be <result> on the screen
Scenarios: addition| x | y | result|| 1 | 2 | 3 || 2 | 2 | 4 || 3 | -3 | 0 |
© Hive Studios 2011
DEMO
© Hive Studios 2011
What to Put into Step Definitions
• It is your call• Depend on what you want to test
1. Public Interfaces / Components2. WebUI using browser automation
• WatiN, Selenium
3. WebUI using Request/Respons• Http Get/Post
4. Win UI using automation• White / Windows UI Automation
© Hive Studios 2011
Resources• Specflow
• http://specflow.org
• Gherkin• https://
github.com/aslakhellesoy/cucumber/wiki/gherkin
• White• http://white.codeplex.com/
• WatiN• http://watiN.sourceforge.net
© Hive Studios 2011
MSForge.NET
Šta je MS Forge?MSForge.net je lokalni web portal i mesto okupljanja svih Windows, SQL Server i .NET fanova, kao i svih onih koji žele da rade sa ovim tehnologijama. MSForge.net daje podršku i omogućava Web prisustvo lokalnim User grupama.
Ko čini MS Forge?MSForge.net okuplja ljude koji se profesionalno ili iz hobija bave Microsoft tehnologijama. Člansto na portalu i u User grupama je besplatno i dobrodošli su svi koji žele da saznaju nešto novo i da svoja znanja podele sa drugima.
Kako MS Forge može da pomognePokretačka ideja koja stoji iza MSForge.net i user grupa je razmena znanja. Odgovore na pitanja možete potražiti kako online (na blogivima ili na forumu) tako i na sastancima User grupa o kojima ćete biti obavešteni putem MSForge portala.
Priključi se!Radu MSForge.net portala i User grupa možete i sami da doprinesete. Deljenjem znanja i iskustava na blogovima ili forumima doprinećete ne samo napretku naše online zajednice, već ćete mnogima omogućiti da unaprede svoja znanja. Ukoliko imate interesantnu ideju za predavanje kontaktirajte moderatora User grupe.