---
title: RSpec
description: Learn how to integrate Allure with RSpec to generate rich, interactive test reports. Follow step-by-step setup, test execution, and report generation guidance.
---

# Getting started with Allure RSpec

[![Allure RSpec gem latest version](https://img.shields.io/gem/v/allure-rspec?style=flat "Allure RSpec gem latest version")](https://rubygems.org/gems/allure-rspec)

Generate beautiful HTML reports using [Allure Report](https://allurereport.org/docs/) and your [RSpec](https://rspec.info/) tests.

Info:
Check out the example project at [github.com/allure-examples/allure-rspec-example](https://github.com/allure-examples/allure-rspec-example) to see Allure RSpec in action.

## Setting up

### 1. Prepare your project

1. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the [installation instructions](/docs/v2/install/).

1. Add Allure RSpec to your project's `Gemfile`. For example:

   ```ruby
   source 'https://rubygems.org'

   gem 'allure-rspec', '~> 2.23.0'
   gem 'rspec', '~> 3.12'
   ```

1. Open a terminal, go to the project directory and install the dependencies from the `Gemfile`. For example, if you use [Bundler](https://bundler.io/):

   ```bash
   cd /home/user/myproject
   bundle install
   ```

1. In the project's [`.rspec`](https://rspec.info/documentation/3.12/rspec-core/#store-command-line-options-rspec) file, specify `AllureRspecFormatter` as the formatter for RSpec.

   ```plain
   --format AllureRspecFormatter
   ```

### 2. Run tests

Run your RSpec tests same way as your would run them usually. For example:

```bash
bundle exec rspec
```

This will save necessary data into `reports/allure-results` or other directory, according to the [Configuration](/docs/rspec-configuration/). If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.

### 3. Generate a report

Finally, convert the test results into an HTML report. This can be done by one of two commands:

- `allure generate` processes the test results and saves an HTML report into the `allure-report` directory. To view the report, use the `allure open` command.

- `allure serve` creates the same report as `allure generate`, then automatically opens the main page of the report in a web browser.

## Writing tests

The Allure RSpec integration extends the standard reporting features of RSpec by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:

- **Metadata Annotation**: Enhance test reports with [descriptions, links, and other metadata](#specify-description-links-and-other-metadata).
- **Test Organization**: Structure your tests into clear hierarchies for better readability and organization [organize tests](#organize-tests).
- **Step Division**: Break down tests into smaller [test steps](#divide-a-test-into-steps) for easier understanding and maintenance.
- **Parametrized Tests**: Clearly describe the parameters for [parametrized tests](#describe-parametrized-tests) to specify different scenarios.
- **Attachments**: Automatically capture [screenshots and other files](#attach-screenshots-and-other-files) during test execution.
- **Test Selection**: Use a test plan file to [select which tests to run](#select-tests-via-a-test-plan-file), allowing for flexible test execution.
- **Environment Details**: Include comprehensive [environment information](#environment-information) to accompany the test report.

### Adding Metadata

Allure allows you to enrich your reports with a variety of [metadata](/docs/v2/readability/#description-links-and-other-metadata). This additional information provides context and details for each test, enhancing the report's usefulness. Refer to the [metadata reference section](/docs/rspec-reference/#metadata) for an exhaustive list of what can be added.

```ruby
describe 'Test my website' do
  it 'test authentication', issue: 'AUTH-123' do
    Allure.description_html 'This test attempts to log into the website using a login and a password.'
    Allure.label 'owner', 'John Doe'
    # ...
  end
end
```

### Organize tests

As described in [Improving navigation in your test report](/docs/v2/navigation/), Allure supports multiple ways to organize tests into hierarchical structures.

To specify a test's location in the [behavior-based hierarchy](/docs/v2/navigation/#behavior-based-hierarchy):

**Runtime API:**
```ruby
describe 'Test my website' do
  it 'test authentication' do
    Allure.epic 'Web interface'
    Allure.feature 'Essential features'
    Allure.story 'Authentication'
    # ...
  end
end
```

**RSpec metadata:**
```ruby
describe 'Test my website' do
  it 'test authentication',
     epic: 'Web interface',
     feature: 'Essential features',
     story: 'Authentication' do
    # ...
  end
end
```

To specify a test's location in the [suite-based hierarchy](/docs/v2/navigation/#suite-based-hierarchy):

```ruby
describe 'Test my website' do
  it 'test authentication' do
    Allure.label 'parentSuite' 'Web interface'
    Allure.suite 'Essential features'
    Allure.label 'subSuite', 'Authentication'
    # ...
  end
end
```

### Divide a test into steps

Allure RSpec provides three ways of [creating steps and sub-steps](/docs/steps/): “annotated steps”, “block-based steps” and “no-op steps”, see the [reference](/docs/rspec-reference/#test-steps).

**Annotated steps:**
```ruby
describe 'Test my website' do
  it 'test authentication' do
    Steps.step_1
    Steps.step_2
  end
end

class Steps
  extend AllureStepAnnotation

  step 'Step 1'
  def self.step_1
    step_1_1
    step_1_2
  end

  step 'Step 1.1'
  def self.step_1_1
    # ...
  end

  step 'Step 1.2'
  def self.step_1_2
    # ...
  end

  step 'Step 2'
  def self.step_2
    step_2_1
    step_2_2
  end

  step 'Step 2.1'
  def self.step_2_1
    # ...
  end

  step 'Step 2.2'
  def self.step_2_2
    # ...
  end
end
```

**Block-based steps and no-op steps:**
```ruby
describe 'Test my website' do
  it 'test authentication' do
    Allure.run_step 'Step 1' do

      # ...
      Allure.step name: 'Step 1.1', status: :passed

      # ...
      Allure.step name: 'Step 1.2', status: :passed
    end

    Allure.run_step 'Step 2' do

      # ...
      Allure.step name: 'Step 2.1', status: :passed

      # ...
      Allure.step name: 'Step 2.2', status: :passed
    end
  end
end
```

### Describe parametrized tests

With Allure RSpec, it is very easy to implement the [parametrized tests](/docs/v2/readability/#parametrized-tests) pattern, i.e. to run the same test logic with different test data. To do so, just write the test inside a loop and use the variable parameters in both its title and its body.

To display a parameter value in the test report, pass it to the `Allure.parameter()` function.

```ruby
describe 'Test my website' do
  auth_data = [
    ['johndoe', 'qwerty'],
    ['johndoe@example.com', 'qwerty'],
  ]
  auth_data.each do |login, password|
    it "Test authentication as '#{login}'" do
      Allure.parameter 'Login', login
      Allure.parameter 'Password', password
      # ...
    end
  end
end
```

### Attach screenshots and other files

In Allure reports, you have the ability to [attach various types of files](/docs/attachments/), which can greatly enhance the comprehensibility of the report. A common practice is to attach screenshots that capture the state of the user interface at specific moments during test execution.

For detailed instructions on how to implement attachments, refer to the [attachments section in the Allure RSpec reference](/docs/rspec-reference/#attachments).

```ruby
describe 'Test my website' do
  it 'test authentication' do
    # ...
    Allure.add_attachment name: 'Screenshot',
                          source: File.new('/path/to/image.png'),
                          type: Allure::ContentType::PNG
    Allure.add_attachment name: 'Data',
                          source: 'This is the file content.',
                          type: Allure::ContentType::TXT
  end
end
```

### Select tests via a test plan file

If the `ALLURE_TESTPLAN_PATH` environment variable is defined and points to an existing file, RSpec will only run tests listed in this file.

Here's an example of running tests according to a file named `testplan.json`:

**MacOS/Linux:**
```bash
export ALLURE_TESTPLAN_PATH=testplan.json
bundle exec rspec
```

**Windows:**
```plain
setx ALLURE_TESTPLAN_PATH "testplan.json"
bundle exec rspec
```

### Environment information

For the main page of the report, you can collect various information about the environment in which the tests were executed. To do so, specify the information in the [`environment_properties`](/docs/rspec-configuration/#environment-properties) configuration parameter.

For example, it is a good idea to use this to remember the OS version and Ruby version. This may help the future reader investigate bugs that are reproducible only in some environments.

Images: /images/ruby/environment-allure3.png, /images/ruby/environment-allure2.png

```ruby
require 'allure-rspec'

AllureRspec.configure do |config|
  config.environment_properties = {
    os_platform: RbConfig::CONFIG['host_os'],
    ruby_version: RUBY_VERSION,
  }
end
```

Note that if your launch includes multiple RSpec runs (see [How it works](/docs/how-it-works/)), Allure RSpec will only save the environment information from the latest run.
