Tuesday, 7 February 2012

Cucumber Test Case Rails 3

How to write cucumber test case in RAILS 3

Answer is here just follow all my steps...

Step 1:
       To write cucumber test case we need to include the following gem in Gemfile


                                   group :test do
                                        gem 'cucumber-rails'
                                        gem 'database_cleaner
                                   end


Note: database_cleaner gem is used to cleaner the dummy data that we use for testing

Step 2:
       Now to time to generate cucumber by following command
          In your terminal type this command

                               rails g cucumber:install
Step 3:
       Now we can see features folder in your application

       In the folder we can write your own Scenario of the controller or action document in features folder with name related to your controller 

for example

      if you going to write test case for login then use Login.feature
then save it in features/Login.feature (In this blog in use Login.feature for sample)

Step 4:
      In previous version of rails the web_steps.rb,paths.rb and selectors.rb will automatically generated but in rails 3 we have to create or get download from following links


download all above three file and put in features/support folder replace env.rb also

download the above web_steps file and put in to features/step_definitions folder


Note: Web_steps.rb file is common to all the test case (only common function will be there)
Step 5:
           Now time to run your test case by using below command

         bundle exec cucumber features/filename.feature

         If you want to run all the test case using one command

         rake cucumber

step 6:

     If you want to generate pdf for all test case you have to follow this steps
         first install

                  gem 'prawn'

         then use this command

            bundle exec cucumber --f pdf --out sample_pdf  features/file.name.features

(it is single line command so copy fully and use it)

Note :sample_pdf ---->output pdf file name


I here give you the sample  cucumber test case for your reference 


download above file and put it in features folder

download above file and put it in step_definitions folder

Note :This is used for specifying the path of the action 


If you have any  query
mail me at: siva.mca19@gmail.com

Thanks & Regards

Siva K