How to write cucumber test cases? - ruby-on-rails

Can anyone tell me how to write cucumber testcases as am new to this.Suggest me best tutorial togo through for this.
Thanks in Advance.

Rails cast is nice
Nice book for basic setup: The Cucumber Book: Behaviour-Driven Development for Developers and Testers
Also, the Nettuts+ tutorial: Ruby for Newbies: Testing Web Apps with Capybara and Cucumber

This screencast is the best out there. Do checkout part 2 as well! BTW, in cucumber parlance, they are called 'features' where you describe scenarios.
I can personally recommend "The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends" if you want to know more about BDD with Cucumber although I am in no way related to the authour(s) and/or the publisher.

Assuming that you have setup cucumber, I would recommend to use https://github.com/cucumber/cucumber/wiki/Scenario-Outlines

Related

writing test for rubyonrails

I am new to writing test for rubyonrails app like rspect, steak, cucumber etc. How do I start or learn so that it will help me to write beautiful codes?
You can give some simple example or some url where I can understand from zero.
Thanks
http://railscasts.com/episodes/155-beginning-with-cucumber - a good podcast for beginner
The RSpec Book - also very useful
It's a good point to start and then Google help you
1) Start by reading the rails testing guide just to get a feel for testing.
2) Become familiar with the various testing frameworks (Cucumber, rspec, testunit, steak, minitest), watch screencasts (railscasts, TeachMeToCode) and then try and choose one that appeals to you.
3) Write tests!! and get help as you need it from StackOverflow. If you like RSpec then the RSpec book is very helpful, but not essential.
My personal opinion on frameworks:
I started out really liking cucumber but I eventually found it very verbose and I didn't like using it for my integration tests and then RSpec for my unit and functional tests. I now like using just RSpec with Capybara and Shoulda matchers. TestUnit can be even less verbose than RSpec but personally I find RSpec to be the happy medium between writing self documenting code and not being overly verbose.
I've just started with RSpec a few days ago and it was pretty hard (it still is actually). You could start from railstutorial.org. The author follows the TDD principle. More info available at RSpec and Cucumber github page. Be sure to check out the wiki.

Totally confused about rails testing.. which tools are for which jobs?

I'm learning Rails after a long time manually testing my own .NET code,
I'm loving what ive seen but i am SO confused about how it all fits together!
So my questions are
1 - Where would i use:
Rspec
Cucumber
Test Unit
Shoulda
Selenium (not really a ruby thing but more of a web thing ive heard)
I've sort-of been testing my code with some very basic RSpec on my models and using factory girl..
2 - Do i need all of these tools?
For example could i choose cucumber and factory girl and never have to learn rspec or is cucumber a pretty dsl wrapper for rspec and test unit...
3 - Are any of them usable / have a port on .NET as well?
Thanks!
Daniel
My current stack of Testing tools is:
Steak, instead of Cucumber.
Capybara with driver Akephalos, instead of Selenium.
RSpec
Machinist2, instead of factory girl.
https://github.com/cavalle/steak
https://github.com/jnicklas/capybara
http://rspec.info/
https://github.com/notahat/machinist
I learned a lot about testing with the book: The Rspec Book, by the Pragmatic Programmers.
http://pragprog.com/
You have more detailed information in this other question:
Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara)
For 1)
I do use Rspec for unit and Functional testing.
I do use Cucumber for integration testing. Cucumber uses Capybara or Selenium. I like Cucumber because it enables me to write tests with the customers. They feel implicated and thus give sometimes more details about their expectations.
Selenium could be used as a stand alone app to test your web app directly in your browser.
Many other tools exist, it's really a matter of choice. As you said, fixtures are not used anymore, Factory Girl is one of the best way to create testing data sets.
For 2)
You don't need all these tools, of course. You could even write your tests with the native Rails helpers.
But they provide convenient helpers you can take benefit from. So get the one you prefer. Some, like Cucumber, have extensions (like Pickle), to provide even more helpers.
For 3)
The strength of Rspec, Cucumber, Selenium (those I know) is they can be used to test any app.
I'm curious to listen to other's point of view concerning Ajax testing.

What is the best way to test a rails app?

I am new to testing in Rails. I have decided to learn Test Driven Development and hence I'm researching on how to go about it in rails.
I started out with Test::Unit and soon came to know that better tools and testing frameworks available. Things I have heard of are
Shoulda
Mocha
Rspec
Cucumber
Factory Girl
Now I am very confused as to how to go ahead. What is the best combination of these tools I need to learn? Also where can I find resources to learn these?
I am building the app in Rails 3.0
What is the best way to test a rails app?
Test first.
More seriously, you've listed a number of different tools which are designed to do different things.
Shoulda is a test framework for running tests and a set of matchers and assertions for writing tests.
Mocha is a mocking and stubbing library.
Rspec is a test framework which includes mocking tools and matchers.
Cucumber is a framework for writing functional tests.
Factory Girl is a framework for building the domain objects you use in your tests.
Of those either Shoulda or Rspec might replace Test::Unit while the others all provide tools for different areas of testing. Which one you need depends on what you think will make writing tests easier and more effective.
For someone used to Test::Unit unit tests and interested in learning additional tools I would suggest the following.
If you have an existing project using Test::Unit consider adding Mocha's mocking and stubbing and see if that allows you to write focused unit tests more easily. If you already have fixtures in place consider using Factory Girl to generate factories instead as you add new model objects and again see if you find that makes your tests easier to manage and maintain. If you have a solid set of unit tests in place already consider using Cucumber to write some higher level functional tests to begin capturing user stories in tests and testing your app end to end.
If you are starting a new project then start with Rspec and Factory Girl. Rspec will introduce you to a very different style of testing than Test::Unit but you'll still be writing fairly familiar unit tests and it provides stubs, mocks, and matchers which will let you dive into testing without an explosion of too many new gems in your project. Factory Girl will give you a convenient way to build domain objects as your tests require them. Between those two you should be able to pick up a bunch of new testing tricks while still working with your usual unit testing workflow.
Have you checked the Michael Hartl's RoR 3 tutorial?
Ruby on Rails 3 Tutorial by Michael Hartl
He has integrated a good Rails 3 tutorial with some basics usage of RSpec and Factory Girl.
Personally I use Cucumber, RSpec and Factory Girl in my applications. As for a resource, I'm writing a book called Rails 3 in Action which goes through using these tools (and Rails itself, of course) to develop an application.

How do you develop outside-in Rails app using Cucumber & RSpec?

I just get started using BDD in Rails application, but I'm not sure what are best practices and workflows? And what other things that I really need for testing for my project such as step definitions, controllers, models, and views? Do I need to test all of those?
I generally think of Cucumber as a way to do integration testing on your application. Combined with Webrat, you can test user workflows, views and so on in a great way. For unit tests, you'll want to go down to a lower level and test your models just with rspec. You may also want to do some functional tests on the controllers, and I probably wouldn't use Cucumber for that either.
Here are a couple of videos:
http://confreaks.com/videos/72-mwrc2009-bdd-with-cucumber
http://rubyconf2008.confreaks.com/rspec-and-cucumber.html
Ryan Bates has some good Railscasts on these topics:
Beginning with Cucumber
Webrat
More on Cucumber
This may be a matter of taste, but having tried out Rspec I prefer using the built-in Rails testing framework along with a gem called Shoulda. In my opinion, that combination lets you write much clearer, more succinct and understandable tests than Rspec by far. But not everyone would agree.
Shoulda's contexts let you organize your tests into logical hierarchies which really helps when you're trying to test all the possible paths some crazy, branching situation, like user logs in with right pw, wrong pw, right pw but registration not confirmed, etc.
In addition be sure to install the ZenTest gem. That lets you just execute the command $ autotest and your tests will run automatically every time you change a file.

What is the Path to Learn BDD on Ruby On Rails?

I want to start BDD on Ruby On Rails what should I learn?
I don't know anything about BDD, RSpec or Cucumber. What is the best way to learn? Tutorials? Something that cover things like 'What behavior I should test?' etc.
thanks!
RSpec specific; I keep these links for reference (might be advance for beginners). But just wanna share it with you. (Some of it probably already outdated but I think still worth a read)
Rspec Link Fest - links to other references
RSpec in Controllers:
Testing your Application Controller with rSpec
Testing Controllers with rspec
Using Rspec on Controllers
RSpec in Models:
stub_model by Dave Chelimsky
Do we really need Controller and View tests? - some discussion
RSpec in Views:
RSpec testing views for escaped HTML
Rspec Stories (now Cucumber):
Understanding RSpec Stories - a Tutorial
Rspec on Windows:
RSpec, autotest and Snarl on Windows - dunno if this still relevant
p/s: Some are from 2007, I'm not sure if the stuffs are outdated or not, but these are the some of my references used to learn RSpec. Hope it helps
Start with two articles by Dan North: Introducing BDD, followed closely What's in a Story?
Start at the top, RSpec home page:
http://rspec.info/

Resources