Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Acceptance Test Driven Development With Spec Flow And Friends

7,075 views

Published on

Acceptance test driven development in .NET using SpecFlow, WatiN and WebAii.

  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1lite.top/83hwP ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Acceptance Test Driven Development With Spec Flow And Friends

  1. 1. Acceptance Test Driven Development with SpecFlow and Friends <ul><li>Christopher Bartling </li></ul><ul><li>Joel Levandoski </li></ul>
  2. 2. Contact information <ul><li>Christopher Bartling </li></ul><ul><ul><li>[email_address] </li></ul></ul><ul><ul><li>Twitter: @cbartling </li></ul></ul><ul><li>Joel Levandoski </li></ul><ul><ul><li>j [email_address] </li></ul></ul><ul><ul><li>Twitter: @joellevandoski </li></ul></ul>
  3. 3. Administrative stuff <ul><li>Presentation and demo are available on BitBucket.org </li></ul><ul><ul><li>https://bitbucket.org/cebartling/ presentations </li></ul></ul>
  4. 4. Topics <ul><li>Acceptance Test Driven Development (ATDD) </li></ul><ul><li>SpecFlow for .NET </li></ul><ul><ul><li>Behavior Driven Development (BDD) tool </li></ul></ul><ul><li>WatiN and WebAii </li></ul><ul><li>Live demonstration </li></ul><ul><li>Questions </li></ul>
  5. 5. Acceptance Test Driven Development <ul><li>ATDD </li></ul><ul><li>Acceptance tests are executable specifications of desired behavior and functionality of the system </li></ul><ul><ul><li>Expressed in language of the problem domain </li></ul></ul><ul><ul><li>Automated </li></ul></ul><ul><ul><li>Regression suite </li></ul></ul><ul><li>Development is driven from the outside-in </li></ul>
  6. 6. ATDD vs. TDD <ul><li>TDD is extremely valuable, but you need more </li></ul><ul><ul><li>Achieve great unit test coverage and still not deliver value to the customer </li></ul></ul><ul><li>ATDD focuses on complete features and functionality </li></ul><ul><li>ATDD: macro view </li></ul><ul><li>TDD: micro view </li></ul>
  7. 8. SpecFlow for .NET <ul><li>BDD testing framework </li></ul><ul><ul><li>Integrates nicely with Visual Studio </li></ul></ul><ul><li>Acceptance tests manifest themselves as features and scenarios </li></ul><ul><li>SpecFlow generates automated test from features </li></ul><ul><ul><li>SpecFlow tests run as normal xUnit tests </li></ul></ul><ul><ul><li>Visual Studio test runner, build server </li></ul></ul>
  8. 9. Features <ul><li>Describe some piece of functionality of the system </li></ul><ul><li>Maintained in a .feature file </li></ul><ul><li>Plain readable text </li></ul><ul><ul><li>Understandable by all parties, including business </li></ul></ul><ul><ul><li>Gherkin format, popularized by Cucumber </li></ul></ul><ul><li>Features contain one or more scenarios </li></ul>
  9. 10. Feature file example <ul><li>Feature: Calculate Net Present Value </li></ul><ul><li>In order analyze the profitability of a project </li></ul><ul><li>As a project manager </li></ul><ul><li>I want to be able to calculate the project’s Net Present Value </li></ul><ul><li>Scenario: A project is rejected </li></ul><ul><li>Given a project to evaluate </li></ul><ul><li> When data is entered reflecting a failing project ROI scenario </li></ul><ul><li> Then the net present value calculator will reject the project </li></ul>
  10. 11. Scenarios <ul><li>A scenario describes a single acceptance test for a feature </li></ul><ul><li>Most features are composed of multiple scenarios </li></ul><ul><li>SpecFlow generates a test for each scenario </li></ul><ul><ul><li>The name of the test is generated from the scenario title </li></ul></ul>
  11. 12. Scenario example <ul><li>Scenario: A project is rejected when its NPV is negative </li></ul><ul><li>Given a project to evaluate </li></ul><ul><li> When data is entered reflecting a failing ROI scenario </li></ul><ul><li> Then the net present value calculator will reject the project </li></ul>
  12. 13. Scenario steps <ul><li>Used to compose a scenario </li></ul><ul><li>Custom code to automate your application </li></ul><ul><li>Describe preconditions, triggering action, and verification of outputs for the acceptance test </li></ul><ul><ul><li>Given : preconditions </li></ul></ul><ul><ul><li>When : triggering action </li></ul></ul><ul><ul><li>Then : behavior and state verifications </li></ul></ul>
  13. 14. Scenario step example <ul><li>Given a project to evaluate </li></ul><ul><li>is matched to the following step definition binding… </li></ul><ul><li>[ Given ( @&quot;a project to evaluate&quot; )] </li></ul><ul><li>public void GivenAProjectToEvaluate() { </li></ul><ul><li>. . . </li></ul><ul><li>} </li></ul>
  14. 15. Set up and tear down <ul><li>Attributes for before and after events </li></ul><ul><ul><li>TestRun </li></ul></ul><ul><ul><li>Feature </li></ul></ul><ul><ul><li>Scenario, ScenarioBlock </li></ul></ul><ul><ul><li>Step </li></ul></ul><ul><li>Specificity achieved when used with tags </li></ul>
  15. 16. Tags <ul><li>Markers that can be applied to features and scenarios </li></ul><ul><li>Useful for selectively mixing in behavior </li></ul><ul><li>Used to categorize scenarios </li></ul><ul><ul><li>Some unit test frameworks support this categorization tagging </li></ul></ul><ul><li>Custom tags you define </li></ul>
  16. 17. Tag example <ul><li>@WatiN </li></ul><ul><li>Scenario : Save valid sample size mid range </li></ul><ul><li>Given the user enters 10 as sample size </li></ul><ul><li>When the user selects save </li></ul><ul><li>Then the value is stored </li></ul>
  17. 18. Tag example used with BeforeScenario <ul><li>[ BeforeScenario (&quot; WatiN ”)] </li></ul><ul><li>public void BeforeScenarioUsingWatiN() </li></ul><ul><li>{ </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  18. 19. Background <ul><li>Common preconditions for all scenarios in a feature </li></ul><ul><li>Contains one or more scenario steps </li></ul><ul><li>Executed before any other steps in the scenario </li></ul><ul><li>SpecFlow generates a method from the background element in the feature file in the test class </li></ul><ul><ul><li>Invoked from each scenario test case in the test class </li></ul></ul>
  19. 20. Background example <ul><li>Background : </li></ul><ul><li>Given that the welcome page is displayed </li></ul><ul><li>Scenario : Add a comment to a book being reviewed </li></ul><ul><li>. . . </li></ul>
  20. 21. Scenario outlines <ul><li>Data-driven scenarios or scenario templates </li></ul><ul><li>Consists of </li></ul><ul><ul><li>Scenario template specification with data placeholders </li></ul></ul><ul><ul><li>Set of examples providing values for placeholders </li></ul></ul><ul><li>SpecFlow generates parameterized test logic for the scenario outline and individual test method for each example set </li></ul>
  21. 22. Scenario outline example <ul><li>Scenario Outline : Score calculation tables </li></ul><ul><li>Given a new bowling game </li></ul><ul><li>When I roll the following series: < rolls > </li></ul><ul><li>Then my total score should be < total score > </li></ul><ul><li>Examples : </li></ul><ul><li>| game | rolls | total score | </li></ul><ul><li>| beginners game | 2,7,3,4,1,1,5,1,1,1,1,1,1,1,1,1,1,1,5,1 | 40 | </li></ul><ul><li>| one single spare | 2,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 | 29 | </li></ul>
  22. 23. ScenarioContext <ul><li>ScenarioContext.Current.Pending() </li></ul><ul><ul><li>Causes the step definition to return pending </li></ul></ul><ul><ul><li>Used to signal a stubbed step definition </li></ul></ul><ul><li>ScenarioContext.Current dictionary </li></ul><ul><ul><li>Allows you to carry context/state across step definitions participating in scenario </li></ul></ul>
  23. 24. WatiN <ul><li>Open source browser automation tool for IE and Firefox </li></ul><ul><ul><li>Similar to Selenium, WebDriver, Watir </li></ul></ul><ul><li>Browser abstraction </li></ul><ul><ul><li>Find elements using CSS selectors </li></ul></ul><ul><ul><li>Interact with AJAX web elements </li></ul></ul><ul><ul><li>Handle popup dialogs (native and HTML) </li></ul></ul>
  24. 25. WebAii <ul><li>Browser automation tool from Telerik </li></ul><ul><ul><li>Automates both web 2.0 and Silverlight applications </li></ul></ul><ul><li>HTML element wrappers </li></ul><ul><li>WaitForElement(s) support when using AJAX </li></ul><ul><li>Identifying elements using LINQ </li></ul><ul><li>Invoke JavaScript directly from test code </li></ul>
  25. 26. Best practices <ul><li>Write high-level specifications </li></ul><ul><li>Specifications should remain stable over time </li></ul><ul><li>Build a scripting interface for manipulating your system under test (SUT) </li></ul><ul><li>Focus specifications on isolated behaviors </li></ul><ul><li>Think of specifications in Given-When-Then format </li></ul><ul><li>Use the Page Object pattern </li></ul>
  26. 27. Smells <ul><li>Specifications are constantly changing </li></ul><ul><li>Specifications are composed of “sequential command executions” </li></ul><ul><li>Specifications have a lot of instrumentation or fixture code </li></ul><ul><li>Specification examples exhibit same structure </li></ul>
  27. 28. Net Present Value Calculator Demo <ul><li>Used in capital budgeting </li></ul><ul><li>Measures the excess or shortfall of cash flows, in present value terms, once financing terms have been covered </li></ul><ul><li>Demo has two implementations </li></ul><ul><ul><li>ASP.NET MVC </li></ul></ul><ul><ul><li>Silverlight </li></ul></ul>
  28. 29. Questions?
  29. 30. Literature cited <ul><li>http://www.concordion.org/Technique.html </li></ul><ul><li>http://www.telerik.com/automated-testing-tools/webaii-framework- features.aspx </li></ul><ul><li>http://watin.org / </li></ul><ul><li>http://www.specflow.org / </li></ul><ul><li>http://code.google.com/p/selenium/wiki/ PageObjects </li></ul>

×