Story-driven Testing


Published on

Using Cucumber to improve the application testing process

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • We’re going to tlk about story-driven testing with Cucumber.
  • Testing web apps can be frustrating because there are so many places where things can go wrong. Most developers test web sites by running users through the application, but the manual clicking can take time.
  • Developers would rather be coding new features, and users would rather have a product that works well and not have their time wasted doing the developer’s job for them.
  • We don’t do things that are hard and not fun. It’s just human nature. We just kind of hide and pretend they’re not problems.
  • Unfortunately, this leads to shipping bad software.
  • ...then the support calls come, and the angry emails, and the displeased shareholders.
  • Obviously there has to be some kind of workable solution, but what is it?
  • We can make the work easy by automating things. We spend so much time as developers automating processes for our clients that we never stop to think about how we might do that for ourselves and our processes.
  • We can reduce some of our work if we can get the users involved in a real way.
  • Finally, in order for this to work, management has to enforce testing practices. Applications have to be shipped with good test coverage, and time estimates must include the time it takes to write tests.
  • Everyone knows testing isn’t a waste of time. We may argue on how to do it, but everyone does some level of testing as they develop.
  • Unfortunately, every single developer can come up with a list of excuses why they don’t test enough. Usually they blame management or marketing for pushing deadlines.
  • Management has to make deadlines or stuff doesn’t get done, and they have outside forces pressuring them to deliver results.
  • Programmers know that if they rush, they’re going to do crappy work and while deadlines may be met in the short term, what is the cost of the technical debt incurred? Bad code needs to be addressed at some point. A professioal craftsman strives for quality.
  • So we have to be able to test quickly and easily
  • and we have to allow time to make it happen
  • and we can achieve some of this with Cucumber, a testing framework designed with the user experience in mind.
  • This scenario is written using Gherkin, Cucumber’s language for describing user interactions. It’s easy to read and easy to understand. You can sit down with your end users and write these scnearios together and then later use them to test your real application.
  • When thinking about a feature, you and your user must think about what value the feature has.
  • Ask yourselves “why do we need this feature? How will having this feature benefit a user?” The user might be a system administrator, a site owner, a basic user, or an anonymous guest, but you should only think about features that directly impact a user.
  • A feature can best be described by filling in these blanks: “As a somebody, I want to do something, so that I can do something else.
  • “As a nervous student who may not graduate, I want to be able to check my grades in real time so that I know I will get a diploma” is definitely focused on the user.
  • You describe a scenario using “given, when, then”.
  • The scenario describes the feature in more details, explaining how a user will move through the application to achieve the feature’s goal.
  • “Given” steps explain the prerequisites for the test. They let you specify any setup conditions that must have occurred outside of the process.
  • Given I am logged in as “homer” And I am a current student And I am on the My Info page and I recived a grade of “C-” in “CS 462”
  • “When” steps describe what you do during the scenario
  • When I click “Check my grades And I select “Current Term” And I press “View”
  • “Then” steps describe the outcome you expect.
  • Then I should see “Grades for Homer Simpson” And I should see a table with “CS 462” in row 1, column 1 And I should see a table with “C-” in row 1 column 4
  • Step definitions map these sentences to actual code.
  • We use Ruby and regular expressions to process each line in the story.
  • Even though we write these definitions in Ruby code, you can run Cucumber against anything that shows up in your browser, including Flash!
  • One popular way to run Cucumber stories is with WEBRAT. It’s a library mainly used in Rails applications to run a Ruby-based browser.
  • When you install webrat, you get a bunch of ready-made matchers for common tasks like finding items on a page, filling in forms, clicking links, and pressing buttons
  • WATIR, or “Web Application Testing In Ruby” automates Internet Explorer or Firefox and works well with Cucumber.
  • Steps are mapped by using regular expression capture values. The captured values can then be used in the step.
  • You can use multiple parameters too.
  • A small demo automating Pastie.
  • So, stories automate a browser
  • there are many more advanced features of Cucumber though.
  • One major advantage of this is that your features stay with your code. They stay fresh, and they change as your code changes. A new developer can look at these features, run them, and know exactly what the application does.
  • Your non-technical stakeholders, management, and other parties can easily communicate with the developers about features.
  • If you can get features that are well thought out, well planned, and well communicated in front of your developers so they can implement them, you’ll be a hero.
  • That’s how you win.
  • Story-driven Testing

    1. 1. story-driven testing people first, computers second.
    2. 2. testing can be frustrating.
    3. 3. testing isn’t fun.
    4. 4. so we don’t do it...
    5. 5. then things break
    6. 6. and users get mad.
    7. 7. so how do you win?
    8. 8. make tests automated!
    9. 9. get users involved!
    10. 10. and enforce policy.
    11. 11. everyone knows why testing is good.
    12. 12. and everyone has excuses why they don’t test.
    13. 13. it’s the manager’s job to keep projects on schedule
    14. 14. it’s the passionate programmer who fights for quality.
    15. 15. testing needs to be easy (and quick) to do...
    16. 16. and it must be part of the schedule.
    17. 17. domain experts first and code second.
    18. 18. And I fill in "Email" with "" Feature: User Signup In order to help other people learn Rails and get help myself And I press "Sign Up" As an interested community member I want to sign up Then I should receive an email Scenario: Signing up to use the system When I open the email Given I go to the home page And I press "Sign up" Then I fill in "Login" with "homer" activate your Rails Mentors And I should see "Please And I press "Sign account" in the subject And I fill in "Email" with "" Up" When I open the email click the first link in the email When I Then I should receive an email When I click the firstshould see "Almost done..." Then I link in the email Then I should see "Please activate your Rails Mentors account" in the subject When I fill in fill in "Password" with "homer_rocks" When I "Password" with "homer_rocks" Then I should see "Almost done..." And I press "Start in "Password Confirmation" with And I fill using your account!" And I fill in "Password Confirmation" with "homer_rocks" "homer_rocks" Then I should be on the dashboard page And I should see "Your account has been activated. Welcome to Rails Mentors!" And I press "Start using your account!" And I should see "Please complete your profile!" Then I should be on the dashboard page And I should see "Your account has been activated. Welcome to Rails Mentors!" And I should see "Please complete your profile!"
    19. 19. features are value- propositions
    20. 20. focus on features that have real business value.
    21. 21. As a... I want to... So that I can...
    22. 22. Feature: Checking grades As a nervous student who may not graduate I want to be able to check my grades in real time So that I know I will get a diploma
    23. 23. Given Then When
    24. 24. scenarios describe the feature in detail.
    25. 25. Given setups, preconditions, prerequisites
    26. 26. Given I am logged in as "homer" And I am a current student And I am on the My Info page And I received a grade of "C-" in "CS 462"
    27. 27. When describe actions
    28. 28. When I click "Check my grades" And I select "Current term" And I press "View"
    29. 29. Then describes outcomes
    30. 30. Then I should see "Grades for Homer Simpson" And I should see a table with "CS 462" in row 1, column 1 And I should see a table with "C-" in row 1, column 4`
    31. 31. step definitions make this all possible.
    32. 32. step definitions use Ruby code and regular expressions to run stories.
    33. 33. but you can run stories against any web-based application
    34. 34. WEBRAT
    35. 35. webrat is for testing rails applications
    36. 36. webrat provides matchers for cucumber
    37. 37. Web Application Testing In Ruby
    38. 38. WATIR platforms • Internet Explorer (Windows) • Firefox (Windows and Mac) • Safari (Mac)
    39. 39. steps to definitions Then I should see "Grades for Homer Simpson" Then /I should see "(.*)"/ do |text| @browser.text.should include(text) end
    40. 40. multiple parameters When I fill in "username" with "bphogan" When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
    41. 41. a small demo Feature: Share code with others In order to help a student who is in a different state As a good teacher I want to share some code I wrote over the internet Scenario: Create a private paste so it won’t be found by Google Given I go to "" And I fill in "paste[body]" with "This is a simple test case using Cucumber" And I select "Plain text" from "paste_parser_id" And I check "paste[restricted]" When I press "Paste" Then I should see "less than a minute ago" When I click "Edit" Then I should see "This paste will be private."
    42. 42. stories automate a browser
    43. 43. Many more advanced features... but
    44. 44. cucumber lets you keep your features with the code
    45. 45. and it facilitates communication between technical and non-technical people.
    46. 46. you will be a hero.
    47. 47. then you win.
    48. 48. questions go to