View as Markdown

RSpec Integration with Mergify

Report your test results from RSpec to Mergify


This guide explains how to integrate RSpec with Test Engine using the rspec-mergify gem. Once installed, test results are automatically uploaded to Test Engine without any extra workflow changes.

  • CRuby 3.1 to 4.0. JRuby, TruffleRuby, development builds of Ruby (ruby-head) and jobs that install gems with Bundler’s force_ruby_platform are not supported: the suite runs, but nothing reaches Test Engine, and the plugin says why after the run. Use a JUnit report upload for those jobs, with rspec_junit_formatter writing the report.

  • Ruby 3.1 or later, and rspec-core 3.12 or later. When some jobs of your CI matrix run an older Ruby, add the gem only for Ruby 3.1 and later (for example gem 'rspec-mergify' if RUBY_VERSION >= '3.1'), require it from spec/spec_helper.rb with the same condition rather than from .rspec, and check that a frozen bundle install still passes on the older Ruby.

  • A platform the mergify-ci-core gem ships a precompiled extension for. Bundler installs that gem alongside rspec-mergify, and its platform gems cover Ruby 3.1 through 4.0 on Linux x86_64 and aarch64 (glibc 2.30 or newer: Debian 11+, Ubuntu 20.04+, RHEL 9+, Amazon Linux 2023; or musl, where Ruby 3.1 also needs apk add libgcc), on macOS arm64 and x86_64, and on Windows x64 (UCRT). Anywhere else, including glibc older than 2.30 (RHEL, Rocky, AlmaLinux and Oracle Linux 8, Amazon Linux 2, CentOS 7), the extension cannot load: the gem still installs and your suite still runs, but nothing reaches Test Engine. On those systems, pin the last pure-Ruby release instead: gem 'rspec-mergify', '0.1.4'.

You need to install the rspec-mergify gem to automatically upload your test results to Test Engine.

Add the gem to your Gemfile:

group :test do
gem 'rspec-mergify'
end

Then run:

Terminal window
bundle install

Use the Bundler version your Gemfile.lock was written with (its BUNDLED WITH line), or Bundler rewrites that line.

Bundler does not load rspec-mergify by itself. The gem’s entry file is rspec_mergify, which Bundler.require does not look for, and a plain bundle exec rspec does not call Bundler.require at all. Require it in your .rspec file:

--require rspec_mergify

Or add require 'rspec_mergify' at the top of spec/spec_helper.rb. Without it, RSpec runs as usual and nothing reaches Test Engine.

Alternatively, install it directly:

Terminal window
gem install rspec-mergify

The plugin uploads results only when the CI environment variable is set, or when MERGIFY_CI_ENABLE is on. GitHub Actions, CircleCI and Buildkite set CI for you. Jenkins does not: set CI=true, or MERGIFY_CI_ENABLE=true, in the stage that runs your tests. If your tests behave differently when CI is set (some test suites skip or add tests on CI), set MERGIFY_CI_ENABLE=true instead of CI, so the tests that run stay the same.

CI counts as set unless it is empty or a false value (false, no, off, 0, f or n), so true, 1 and a CI system’s name (Woodpecker sets CI to woodpecker) all count. MERGIFY_CI_ENABLE is stricter: only true, yes, on, 1, t or y turn it on. Case does not matter for either.

Without MERGIFY_TOKEN the plugin uploads nothing and your tests run as usual. Pull requests from forks and Dependabot pull requests on GitHub Actions get no repository secrets, so they report nothing: no guard is needed in the workflow. (The RSpec gem logs an authentication error when the variable is set but empty, as on those pull requests; the tests are not affected.) To have Dependabot pull requests report too, store the key as a Dependabot secret as well (gh secret set MERGIFY_TOKEN --app dependabot --repo <owner>/<repository>).

Every test is identified by its name and the name of the CI job that ran it. By default the job name is the job’s identifier in your CI:

CIDefault job name
GitHub Actionsthe job id (GITHUB_JOB), recorded with the workflow name (GITHUB_WORKFLOW)
CircleCICIRCLE_JOB, the expanded job name of each matrix leg
Buildkitethe step label (BUILDKITE_LABEL)
JenkinsJOB_NAME, also used as the pipeline name

Set MERGIFY_TEST_JOB_NAME when that default is not one stable name per set of tests:

  • GitHub Actions matrix: every leg shares one job id. Set it per leg from the matrix values, for example MERGIFY_TEST_JOB_NAME: test (${{ matrix.python-version }}, ${{ matrix.os }}).

  • Two test runs in one job: set a distinct name on each step.

  • Reusable workflows: a called workflow reports under the caller’s workflow name, so two called workflows whose jobs share an id (often test) collide. Set a distinct name in each.

  • Jenkins multibranch pipelines: each branch and each pull request keeps its own test history, and no setting changes that. JOB_NAME includes the branch or pull request (my-repo/PR-123) and is also the pipeline name tests are filed under; MERGIFY_TEST_JOB_NAME replaces only the job name. Do not set it for this purpose.

  • Shards of one test suite are the same tests split across machines: give all shards the same name. Never put a shard index in it.

Tests inside tox, Docker or Docker Compose

Section titled Tests inside tox, Docker or Docker Compose

tox, docker run and Docker Compose start the tests in an environment that does not inherit the CI job’s variables. Pass through MERGIFY_TOKEN, CI, MERGIFY_TEST_JOB_NAME if you set it, and every variable the plugin reads to identify the run. tox’s pass_env accepts globs (GITHUB_*); Docker Compose, docker run -e and the Buildkite Docker Compose plugin need each name:

GitHub Actions: GITHUB_ACTIONS GITHUB_BASE_REF GITHUB_EVENT_NAME GITHUB_EVENT_PATH
GITHUB_HEAD_REF GITHUB_JOB GITHUB_REF_NAME GITHUB_REF_TYPE GITHUB_REPOSITORY
GITHUB_REPOSITORY_ID GITHUB_RUN_ATTEMPT GITHUB_RUN_ID GITHUB_SERVER_URL
GITHUB_SHA GITHUB_WORKFLOW RUNNER_NAME
CircleCI: CIRCLECI CIRCLE_BRANCH CIRCLE_BUILD_URL CIRCLE_JOB
CIRCLE_REPOSITORY_URL CIRCLE_SHA1 CIRCLE_WORKFLOW_ID
Buildkite: BUILDKITE BUILDKITE_AGENT_NAME BUILDKITE_BRANCH BUILDKITE_BUILD_ID
BUILDKITE_BUILD_URL BUILDKITE_COMMIT BUILDKITE_LABEL BUILDKITE_PIPELINE_SLUG
BUILDKITE_PULL_REQUEST BUILDKITE_PULL_REQUEST_BASE_BRANCH BUILDKITE_REPO
BUILDKITE_RETRY_COUNT BUILDKITE_STEP_KEY
Jenkins: JENKINS_URL BUILD_ID BUILD_URL JOB_NAME NODE_NAME GIT_URL GIT_BRANCH
GIT_COMMIT CHANGE_TARGET

Pass them by name, never by writing a value into a file. In a container on GitHub Actions, also mount the file GITHUB_EVENT_PATH points to, at the same path: without it the run is recorded against the merge commit instead of the pull request’s head commit.

Depending on its version and persona, zizmor can report secrets-outside-env on the MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }} line. Run the repository’s own zizmor configuration on the changed workflow, and if it reports the finding, follow the convention the repository already uses for it: a deployment environment on the job, or an inline # zizmor: ignore[secrets-outside-env] comment.

Your workflow should run your tests as usual while exporting the secret MERGIFY_TOKEN as an environment variable.

Add the following to the GitHub Actions step running your tests:

env:
MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}

For example:

- name: Run RSpec Tests 🧪
env:
MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
run: bundle exec rspec

Set MERGIFY_TOKEN in the environment of the agents running your tests. The step itself then needs no Mergify-specific configuration:

steps:
- label: "Run RSpec Tests 🧪"
command: bundle exec rspec

The gem automatically collects your test results and sends them to Test Engine.

The gem applies quarantine inside the RSpec run. When a quarantined example fails, the gem marks it pending with the message Test is quarantined from Mergify Test Insights, so it does not count as a failure and does not change RSpec’s exit code. The exit code of your test step already accounts for quarantine, so the step needs nothing more:

  • Do not add continue-on-error: true. The recipes that upload a JUnit report with the mergifyio/gha-mergify-ci action need it because the action decides the job’s result after the tests. Here nothing does, so it would let every real failure through.

  • There is no step id to set and no test_step_outcome to pass: both belong to that action, which this setup does not use.

A crash cannot pass for a green run either. Quarantine only covers failures inside an example: a spec file that fails to load, a failing before(:suite) or after(:context) hook, or RSpec dying mid-run still exits non-zero and fails the step. The gem uploads results when the run ends, so a process killed outright before then, such as by the out-of-memory killer, sends nothing to Test Engine.

If the gem cannot fetch the quarantine list, it quarantines nothing for that run, and a quarantined example that fails makes the step fail as usual.

The gem reruns examples within the budgets set under Native Flaky Detection:

  • On a pull request, an example the target branch does not have yet is new. The gem runs it again right after its own run, inside the same RSpec run, as many times as fit in the Prevention Budget. When one of those runs fails, the example is flaky and RSpec reports it failed, so the pull request does not merge. The tries a retry plugin such as rspec-retry made inside the example’s first run count too: a failure among them makes the example flaky. See Prevention.

  • On a push, the gem reruns the examples Test Engine knows as unhealthy, within the Detection Budget, to report whether they still are. The run’s result does not change. See Rerunning unhealthy tests.

The gem uploads each run’s results under your CI job’s name. On GitHub Actions, that name is the job’s identifier (GITHUB_JOB), which every leg of a matrix shares. The job name is part of a test’s identity in Test Engine, so when the legs run the same suite on several Ruby versions, their results merge into one test.

Set MERGIFY_TEST_JOB_NAME on each leg and include the matrix value, so each leg reports under its own name:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['3.2', '3.3', '3.4']
steps:
# ...
- name: Run RSpec Tests 🧪
env:
MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
MERGIFY_TEST_JOB_NAME: test (ruby ${{ matrix.ruby-version }})
run: bundle exec rspec

Name each leg after the environment it runs in, never after a shard of the suite: a test can move to another shard when tests are added or removed, so a name per shard would split that test’s history.

Setting MERGIFY_TEST_JOB_NAME on a job that already reports changes its name, so its tests start a new history in Test Engine.

Verify and review in Test Engine

Section titled Verify and review in Test Engine

After pushing these changes, your next CI run reports its RSpec results automatically.

You can then review your test results, including any failures or flaky tests, directly in the Test Engine dashboard.

VariablePurposeDefault
MERGIFY_API_URL

Base URL of the Mergify API. An API URL given on the command line or in a reporter's configuration takes precedence.

https://api.mergify.com
MERGIFY_CI_DEBUG

Set to true to upload nothing and print the session verdict and the spans instead of sending them.

false
MERGIFY_CI_ENABLE

Set to true to turn the reporter on outside CI. In CI, which the CI variable signals, it is on anyway.

false
MERGIFY_TEST_JOB_NAME

Name the job's test results are reported under, instead of the CI job's own name. Set one per environment, never per shard: a name per shard splits a test's history.

MERGIFY_TEST_RETRY_ENABLE

Set to false to have this job retry no failing test, whatever the repository's test retry configuration. On by default, and test retry only retries on a push to the default branch of a repository that configured it: only a recognised no (false, no, off, 0) turns it off.

true
MERGIFY_TOKEN

The Mergify credential: an application key in CI. Without it the test reporters upload nothing. A mergify command takes it when --token is not given, over the credential mergify auth login stored, and mergify auth tells when it does. The mergify stack commands send it to GitHub as their GitHub token, ahead of GITHUB_TOKEN, unless it holds a Mergify user token (mut_…).

MERGIFY_TRACEPARENT

W3C traceparent of a trace the job already started: the test session's spans join that trace instead of starting their own. A malformed value is ignored.

RSPEC_MERGIFY_DEBUG

Deprecated name of MERGIFY_CI_DEBUG, read while that one is unset or empty.

false
RSPEC_MERGIFY_ENABLE

Deprecated name of MERGIFY_CI_ENABLE, read while that one is unset or empty.

false

To check the setup without uploading anything, run your tests with MERGIFY_CI_ENABLE=true MERGIFY_CI_DEBUG=true: the gem prints its Mergify CI: section.

Was this page helpful?