Ruby Cucumber Test

Using cucumber for application testing and using the Ruby code from a previous blog post found here.

Install cucumber,
$ gem install cucumber

create new directory for this test and change to it,
$ mkdir CukeTest
$ cd CukeTest

use cucumber to initialize the directory, it can be done manually, but why waste the effort.
$ cucumber –init

Place Ruby script, renamed to create_project.rb and set to be executable, into the root of CukeTest directory.

Create a feature file, this will be a high level description of what the test is to do, the file will have the .feature extension, e.g. execution.feature

Feature: Successfully execute the create project script,
make sure that a directory has been created with the
correct structure.

Scenario: execute the create project script
          Given the create_project.rb script exists
          When the ./create_project.rb TestDir is given
          Then a new project directory TestDir will be created

Then a steps definition file is created, this a Ruby script that will provide the functionality of the required test steps, and is placed within the /project/features/steps_definitions/ directory.

require 'open3'

Given(/^the create_project.rb script exists$/) do
  assert(true, File.exist?("./create_project.rb"))
end

When(/^the ./create_project.rb TestDir is given$/) do
  Open3.popen3("./create_project.rb ~/Projects/TestDir") { |i,o,e,t|
    exit_status = t.value
  }
end

Then(/^a new project directory TestDir  will be created$/) do
  assert(true, File.directory?("~/Projects/TestDir"))
end

After entering the above the CukeTest directory structure should be:

CukeTest
    /features/
        /step_definitions/
            execution_steps.rb
        /support/
            env.rb
        execution.feature
    create_project.rb

To run the cucumber test, from the CukeTest directory root:
$ cucumber features/execution.feature

If all goes well the following, similar, output will be observed:

Feature: Successfully execute the create project script,
  make sure that a directory has been created with the
  correct structure.

  Scenario: execute the create project script             # features/execution.feature:5
    Given the create_project.rb script exists             # features/step_definitions/execution_steps.rb:3
    When the ./create_project.rb TestDir is given         # features/step_definitions/execution_steps.rb:7
    Then a new project directory TestDir  will be created # features/step_definitions/execution_steps.rb:13

1 scenario (1 passed)
3 steps (3 passed)
0m0.145s

The above code/description does not in any way represent correct and/or accurate definitions of anything related to the cucumber test framework, for definitive documentation and methodologies please visit the Official Cucumber Website.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s