---
title: RSpec Integration with CI Insights
description: Report your test results from RSpec tests to CI Insights
---

import rspecLogo from "../../../images/ci-insights/rspec/logo.svg"
import CommonTroubleshootingTips from "./_common-troubleshooting-tips.mdx"
import GhaMergifyCiQuarantineSetup from "./_gha_mergify_ci_quarantine_setup.mdx"
import IntegrationLogo from "../../../../components/IntegrationLogo.astro"
import MergifyCIUploadStep from "../../../../components/MergifyCIUploadStep.astro"
import CIInsightsSetupNote from "../../../../components/CIInsightsSetupNote.astro"
import MergifyCIUploadStepMatrix from "../../../../components/MergifyCIUploadStepMatrix.astro"

<IntegrationLogo src={rspecLogo} alt="RSpec logo" />

This guide shows how to generate JUnit reports from your RSpec tests and upload
them to **CI Insights** using a GitHub Actions workflow.

## Generate a JUnit Report with RSpec

RSpec supports JUnit XML output through the built-in JUnit formatter. You can
configure RSpec to output JUnit reports using command-line options or
configuration files.

### Using Command Line Options

```bash
bundle exec rspec --format RspecJunitFormatter --out junit.xml
```

### Installing the JUnit Formatter

First, add the JUnit formatter gem to your Gemfile:

```ruby
# Gemfile
group :test do
  gem 'rspec-junit-formatter'
end
```

Then run:

```bash
bundle install
```

### Using RSpec Configuration

Add the formatter to your `.rspec` file:

```text
--format RspecJunitFormatter
--out junit.xml
```

Or configure it in your `spec_helper.rb`:

```ruby
# spec/spec_helper.rb
RSpec.configure do |config|
  config.add_formatter('RspecJunitFormatter', 'junit.xml')
end
```

### Using Multiple Formatters

You can use multiple formatters simultaneously:

```bash
bundle exec rspec --format documentation --format RspecJunitFormatter --out junit.xml
```

### Using Rake Task

You can also create a Rake task in your `Rakefile`:

```ruby
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec_junit) do |t|
  t.rspec_opts = '--format RspecJunitFormatter --out junit.xml'
end
```

Then run:

```bash
bundle exec rake spec_junit
```

## Update Your GitHub Actions Workflow

<CIInsightsSetupNote />

After generating the JUnit report, add a step to upload the results to CI
Insights using the mergifyio/gha-mergify-ci action.

For example, in your workflow file:

```yaml
- name: Run RSpec Tests and Generate JUnit Report
  continue-on-error: true
  run: bundle exec rspec --format RspecJunitFormatter --out junit.xml
```

<MergifyCIUploadStep reportPath="junit.xml" />
<MergifyCIUploadStepMatrix reportPath="junit.xml" />

<GhaMergifyCiQuarantineSetup />

## Verify and Review in CI Insights

After pushing these changes:

1. Your GitHub Actions workflow will execute your RSpec tests.
2. A JUnit report (junit.xml) is generated.
3. The Mergify CI action uploads the report to CI Insights.

You can then review your test results, including any failures or flaky tests,
directly in the [CI Insights
dashboard](https://dashboard.mergify.com/ci-insights/jobs).

## Troubleshooting Tips

<ul>
  <li>Gem Installation: Ensure the `rspec-junit-formatter` gem is properly installed and included in your Gemfile.</li>
  <li>RSpec Configuration: Verify that the formatter is correctly configured in your RSpec setup.</li>

  <CommonTroubleshootingTips />
</ul>
