We&#x2019;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&#x2019;s job for them.
We don&#x2019;t do things that are hard and not fun. It&#x2019;s just human nature. We just kind of hide and pretend they&#x2019;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&#x2019;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&#x2019;t test enough. Usually they blame management or marketing for pushing deadlines.
Management has to make deadlines or stuff doesn&#x2019;t get done, and they have outside forces pressuring them to deliver results.
Programmers know that if they rush, they&#x2019;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&#x2019;s language for describing user interactions. It&#x2019;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 &#x201C;why do we need this feature? How will having this feature benefit a user?&#x201D; 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: &#x201C;As a somebody, I want to do something, so that I can do something else. &#x201C;
&#x201C;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&#x201D; is definitely focused on the user.
You describe a scenario using &#x201C;given, when, then&#x201D;.
The scenario describes the feature in more details, explaining how a user will move through the application to achieve the feature&#x2019;s goal.
&#x201C;Given&#x201D; 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 &#x201C;homer&#x201D; And I am a current student And I am on the My Info page and I recived a grade of &#x201C;C-&#x201D; in &#x201C;CS 462&#x201D;
&#x201C;When&#x201D; steps describe what you do during the scenario
When I click &#x201C;Check my grades And I select &#x201C;Current Term&#x201D; And I press &#x201C;View&#x201D;
&#x201C;Then&#x201D; steps describe the outcome you expect.
Then I should see &#x201C;Grades for Homer Simpson&#x201D; And I should see a table with &#x201C;CS 462&#x201D; in row 1, column 1 And I should see a table with &#x201C;C-&#x201D; 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&#x2019;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 &#x201C;Web Application Testing In Ruby&#x201D; 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&#x2019;ll be a hero.
That&#x2019;s how you win.
people ﬁrst, computers second.
And I ﬁll in "Email" with "email@example.com"
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 "firstname.lastname@example.org"
When I open the email click the ﬁrst link in the email
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 ﬁll in "Password" with "homer_rocks"
When I "Password" with "homer_rocks"
Then I should see "Almost done..."
And I press "Start in "Password Conﬁrmation" with
And I ﬁll using your account!"
And I fill in "Password Confirmation" with "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 proﬁle!"
• Internet Explorer (Windows)
• Firefox (Windows and Mac)
• Safari (Mac)
steps to deﬁnitions
Then I should see "Grades for Homer Simpson"
Then /I should see "(.*)"/ do |text|
When I fill in "username" with "bphogan"
When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
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 "http://www.pastie.org/"
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."