Top Related Projects
Acceptance test framework for web applications
minitest provides a complete suite of testing facilities supporting TDD, BDD, mocking, and benchmarking.
A library for setting up Ruby objects as test data.
A Ruby static code analyzer and formatter, based on the community Ruby style guide.
Quick Overview
Cucumber-Ruby is a tool for running automated acceptance tests written in plain language. It allows developers and non-technical stakeholders to collaborate on defining and testing application behavior using Gherkin syntax. Cucumber-Ruby executes these specifications as automated tests, bridging the gap between human-readable requirements and executable code.
Pros
- Encourages behavior-driven development (BDD) and collaboration between developers, testers, and business stakeholders
- Supports writing tests in plain language, making them accessible to non-technical team members
- Provides a clear structure for organizing and executing acceptance tests
- Integrates well with other Ruby testing frameworks and tools
Cons
- Learning curve for writing effective Gherkin scenarios and step definitions
- Can lead to test duplication if not managed properly
- May introduce overhead for simple projects or small teams
- Requires discipline to maintain and update scenarios as the project evolves
Code Examples
- Writing a simple Gherkin scenario:
Feature: User login
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
And I click the login button
Then I should be redirected to the dashboard
- Implementing step definitions in Ruby:
Given('I am on the login page') do
visit('/login')
end
When('I enter valid credentials') do
fill_in 'Username', with: 'user@example.com'
fill_in 'Password', with: 'password123'
end
When('I click the login button') do
click_button 'Login'
end
Then('I should be redirected to the dashboard') do
expect(page).to have_current_path('/dashboard')
end
- Using table data in scenarios:
Scenario: Adding multiple products to cart
Given I am on the product page
When I add the following products to my cart:
| Product | Quantity |
| Apple | 2 |
| Banana | 3 |
Then my cart should contain 5 items
Getting Started
To get started with Cucumber-Ruby:
-
Add Cucumber to your Gemfile:
gem 'cucumber'
-
Install the gem:
bundle install
-
Initialize Cucumber in your project:
cucumber --init
-
Write your feature files in the
features
directory using Gherkin syntax. -
Implement step definitions in Ruby files within the
features/step_definitions
directory. -
Run your Cucumber tests:
cucumber
Competitor Comparisons
Acceptance test framework for web applications
Pros of Capybara
- More versatile for web application testing, supporting multiple drivers (Selenium, Rack::Test, etc.)
- Simpler syntax for interacting with web elements and performing common actions
- Better suited for testing modern, dynamic web applications
Cons of Capybara
- Limited to web application testing, while Cucumber can be used for broader behavior-driven development
- Lacks the natural language syntax that Cucumber provides for writing test scenarios
- May require more setup and configuration for complex testing scenarios
Code Comparison
Capybara example:
visit '/login'
fill_in 'Username', with: 'user@example.com'
fill_in 'Password', with: 'password'
click_button 'Log In'
expect(page).to have_content 'Welcome, User!'
Cucumber example:
Given I am on the login page
When I fill in "Username" with "user@example.com"
And I fill in "Password" with "password"
And I click "Log In"
Then I should see "Welcome, User!"
Capybara focuses on programmatic interaction with web elements, while Cucumber emphasizes natural language descriptions of behavior. Capybara is more concise for web-specific testing, but Cucumber offers a more readable format for non-technical stakeholders and supports a wider range of testing scenarios beyond web applications.
minitest provides a complete suite of testing facilities supporting TDD, BDD, mocking, and benchmarking.
Pros of Minitest
- Lightweight and fast, with minimal setup required
- Built-in support for mocking and stubbing
- Integrates seamlessly with Ruby's standard library
Cons of Minitest
- Less expressive syntax compared to Cucumber's Gherkin language
- Limited support for behavior-driven development (BDD) style testing
- Lacks built-in reporting and documentation features
Code Comparison
Minitest example:
require 'minitest/autorun'
class TestExample < Minitest::Test
def test_addition
assert_equal 4, 2 + 2
end
end
Cucumber example:
# features/addition.feature
Feature: Addition
Scenario: Add two numbers
Given I have entered 2 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 4 on the screen
Minitest offers a more concise and straightforward approach to testing, while Cucumber provides a more descriptive and human-readable format using Gherkin syntax. Minitest is better suited for unit testing and quick setups, whereas Cucumber excels in behavior-driven development and creating living documentation for projects.
A library for setting up Ruby objects as test data.
Pros of Factory Bot
- Focused on creating test data, making it more specialized and efficient for this purpose
- Simpler syntax and easier to learn for beginners
- Better integration with Ruby on Rails and ActiveRecord
Cons of Factory Bot
- Limited to test data generation, lacking broader testing capabilities
- Less expressive for non-technical stakeholders compared to Cucumber's Gherkin syntax
- Smaller community and ecosystem compared to Cucumber
Code Comparison
Factory Bot:
FactoryBot.define do
factory :user do
name { "John Doe" }
email { "john@example.com" }
end
end
user = FactoryBot.create(:user)
Cucumber:
Given("a user named John Doe with email john@example.com") do
@user = User.create(name: "John Doe", email: "john@example.com")
end
When("the user logs in") do
# Login logic
end
Factory Bot excels at creating test data with minimal code, while Cucumber provides a more readable, scenario-based approach to testing. Factory Bot is ideal for unit and integration tests, whereas Cucumber shines in behavior-driven development and acceptance testing. The choice between the two depends on the specific testing needs and team preferences.
A Ruby static code analyzer and formatter, based on the community Ruby style guide.
Pros of RuboCop
- Focuses on code style and quality, enforcing Ruby best practices
- Highly configurable with extensive documentation
- Integrates well with various editors and CI tools
Cons of RuboCop
- Can be overly strict, potentially hindering productivity
- Requires initial setup and configuration for optimal use
- May produce false positives in certain scenarios
Code Comparison
RuboCop (enforcing style):
def example_method(param1, param2)
result = param1 + param2
puts "Result: #{result}"
result
end
Cucumber (behavior-driven development):
Given("I have {int} apples") do |count|
@apples = count
end
When("I eat {int} apple(s)") do |eaten|
@apples -= eaten
end
Then("I should have {int} apple(s) left") do |remaining|
expect(@apples).to eq(remaining)
end
Summary
RuboCop is a static code analyzer and formatter, while Cucumber is a behavior-driven development (BDD) tool. RuboCop focuses on enforcing coding standards and best practices, whereas Cucumber facilitates writing executable specifications in plain language. RuboCop is more suited for maintaining code quality and consistency, while Cucumber excels in bridging the gap between technical and non-technical stakeholders in software development.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Cucumber
Cucumber is a tool for running automated tests written in plain language. Because they're written in plain language, they can be read by anyone on your team. Because they can be read by anyone, you can use them to help improve communication, collaboration and trust on your team.
This is the Ruby implementation of Cucumber. Cucumber is also available for JavaScript, Java, and a lot of other languages. You can find a list of implementations here: https://cucumber.io/docs/installation/.
See CONTRIBUTING.md for info on contributing to Cucumber (issues, PRs, etc.).
Everyone interacting in this codebase and issue tracker is expected to follow the Cucumber code of conduct.
Installation
Cucumber for Ruby is a Ruby gem. Install it as you would install any gem: add
cucumber
to your Gemfile:
gem 'cucumber'
then install it:
$ bundle
or install the gem directly:
$ gem install cucumber
Later in this document, bundler is considered being used so all commands are using
bundle exec
. If this is not the case for you, execute cucumber
directly, without
bundle exec
.
Supported platforms
- Ruby 3.2
- Ruby 3.1
- Ruby 3.0
- Ruby 2.7
- TruffleRuby 22.0.0+
- JRuby 9.4+ (with some limitations)
Ruby on Rails
Using Ruby on Rails? You can use cucumber-rails to bring Cucumber into your Rails project.
Usage
Initialization
If you need to, initialize your features
directory with
$ bundle exec cucumber --init
This will create the following directories and files if they do not exist already:
features
âââ step_definitions
âââ support
âââ env.rb
Create your specification
Create a file named rule.feature
in the features
directory with:
# features/rule.feature
Feature: Rule Sample
Rule: This is a rule
Example: A passing example
Given this will pass
When I do an action
Then some results should be there
Example: A failing example
Given this will fail
When I do an action
Then some results should be there
Automate your specification
And a file named steps.rb
in features/step_definitions
with:
# features/step_definitions/steps.rb
Given('this will pass') do
@this_will_pass = true
end
Given('this will fail') do
@this_will_pass = false
end
When('I do an action') do
:no_op
end
Then("some results should be there") do
expect(@this_will_pass).to be true
end
Run Cucumber
$ bundle exec cucumber
To execute a single feature file:
$ bundle exec cucumber features/rule.feature
To execute a single example, indicates the line of the name of the example:
$ bundle exec cucumber features/rule.feature:5
To summarize the results on the standard output, and generate a HTML report on disk:
$ bundle exec cucumber --format summary --format html --out report.html
For more command line options
$ bundle exec cucumber --help
You can also find documentation on the command line possibilities in features/docs/cli.
Documentation and support
- Getting started, writing features, step definitions, and more: https://cucumber.io/docs
- Ruby API Documentation: http://www.rubydoc.info/github/cucumber/cucumber-ruby/
- Community support forum: https://github.com/orgs/cucumber/discussions
- Discord: register for an account
Copyright
Copyright (c) Cucumber Ltd. and Contributors. See LICENSE for details.
Top Related Projects
Acceptance test framework for web applications
minitest provides a complete suite of testing facilities supporting TDD, BDD, mocking, and benchmarking.
A library for setting up Ruby objects as test data.
A Ruby static code analyzer and formatter, based on the community Ruby style guide.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot