> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trunk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# RSpec

> A guide for generating Trunk-compatible test reports for RSpec using Trunk's RSpec plugin

You can automatically [detect and manage flaky tests](../../../detection/) in your projects running RSpec by integrating with Trunk. This document explains how to use Trunk's RSpec plugin to upload test results to Trunk.

## Setup steps

Work through the steps below in order. Once you've finished the last one, you'll be ready to move on to [configure uploads in CI](../../ci-providers/).

<Steps>
  <Step title={<a href="#installing-the-plugin">Set up and install Trunk's RSpec plugin</a>} />

  <Step title={<a href="#disable-retries">Disable retries for better detection accuracy</a>} />

  <Step title={<a href="#try-it-locally">Test uploads locally</a>} />
</Steps>

<Info>
  Using the plugin is the best way to accurately detect flaky RSpec tests.

  You can also [manually generate and upload](./manual-uploads) test results in RSpec, however, **manual RSpec uploads are not recommended.**
</Info>

## Installing the plugin

Trunk detects flaky tests by analyzing test results automatically uploaded from your CI jobs. You can do this for your Rspec tests using Trunk's RSpec plugin.

To install the plugin in your project, add the `rspec_trunk_flaky_tests` gem to your `Gemfile`:

```shell title="Gemfile" theme={null}
gem "rspec_trunk_flaky_tests"
```

Install the plugin:

```sh theme={null}
bundle install
```

Then, load the plugin in `spec_helper.rb`:

```shell title="spec/spec_helper.rb" theme={null}
require "trunk_spec_helper"
```

### Disable Retries

You need to disable automatic retries if you previously enabled them. Retries compromise the accurate detection of flaky tests.

If you have a step in CI to rerun failed tests with the `--only-failures` option, or you're using a package like [rspec-retry](https://github.com/NoRedInk/rspec-retry), remember to disable them.

### Versions and Updating the Plugin

You can find the Gem for `rspec_trunk_flaky_tests` [here](https://rubygems.org/gems/rspec_trunk_flaky_tests), along with its version history. This plugin is periodically updated with improved support and bug fixes. If you're encountering something unexpected, first try:

```
bundle update rspec_trunk_flaky_tests
```

## Environment Variables

These optional environment variables can be set in your project to change the behavior of the RSpec plugin.

### Repository metadata variables:

| Argument                       | Description                                                                               |
| ------------------------------ | ----------------------------------------------------------------------------------------- |
| `TRUNK_REPO_ROOT`              | Path to repository root                                                                   |
| `TRUNK_REPO_URL`               | Repository URL (e.g., [https://github.com/org/repo.git](https://github.com/org/repo.git)) |
| `TRUNK_REPO_HEAD_SHA`          | HEAD commit SHA                                                                           |
| `TRUNK_REPO_HEAD_BRANCH`       | HEAD branch name                                                                          |
| `TRUNK_REPO_HEAD_COMMIT_EPOCH` | HEAD commit timestamp (seconds since epoch)                                               |
| `TRUNK_REPO_HEAD_AUTHOR_NAME`  | HEAD commit author name                                                                   |

### Configuration variables:

| Argument                          | Description                                               |
| --------------------------------- | --------------------------------------------------------- |
| `TRUNK_CODEOWNERS_PATH`           | Path to CODEOWNERS file                                   |
| `TRUNK_VARIANT`                   | Variant name for test results (e.g., 'linux', 'pr-123')   |
| `TRUNK_DISABLE_QUARANTINING`      | Set to 'true' to disable quarantining                     |
| `TRUNK_ALLOW_EMPTY_TEST_RESULTS`  | Set to 'true' to allow empty results                      |
| `TRUNK_DRY_RUN`                   | Set to 'true' to save bundle locally instead of uploading |
| `TRUNK_USE_UNCLONED_REPO`         | Set to 'true' for uncloned repo mode                      |
| `TRUNK_LOCAL_UPLOAD_DIR`          | Directory to save test results locally (disables upload)  |
| `DISABLE_RSPEC_TRUNK_FLAKY_TESTS` | Set to 'true' to completely disable Trunk                 |

## Try It Locally

### Test Upload

Before modifying your CI jobs to automatically upload test results to Trunk, try uploading a single test run manually.

You make an upload to Trunk using the following command:

```sh theme={null}
TRUNK_ORG_URL_SLUG=<TRUNK_ORG_URL_SLUG> \
TRUNK_API_TOKEN=<TRUNK_ORG_TOKEN> \
bundle exec rspec
```

You can find your Trunk organization URL slug and token in the **Settings** or by following these [instructions](/flaky-tests/get-started/ci-providers/otherci#id-1.-store-a-trunk_token-secret-in-your-ci-system).

After your upload, you can verify that Trunk has received and processed it successfully in the **Uploads** tab. Warnings will be displayed if the test report uploaded by the plugin has issues.

<Frame>
  <img className="block dark:hidden" src="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/data-uploads-light.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=6f68ed972b84c450784f8e1bd6840dc5" alt="" width="2560" height="1800" data-path="assets/_shared/data-uploads-light.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/data-uploads-dark.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=e5c9def4c127b684368246f169484d8a" alt="" width="2560" height="1800" data-path="assets/_shared/data-uploads-dark.png" />
</Frame>

<Info>
  You do not need to download the `trunk-analytics-cli` when using the Trunk RSpec plugin. Uploads are handled for you as long as you have set `TRUNK_ORG_URL_SLUG` and `TRUNK_API_TOKEN`.
</Info>

## Next Steps

Configure your CI to upload test runs to Trunk. Find the guides for your CI framework below:

<Columns cols={3}>
  <Card title="Atlassian Bamboo" href="/flaky-tests/get-started/ci-providers/atlassian-bamboo" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/atlassian-bamboo.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=509de3813771912addc536f1a9da39f9" width="1600" height="1000" data-path="assets/_shared/atlassian-bamboo.png" />

  <Card title="Azure DevOps Pipelines" href="/flaky-tests/get-started/ci-providers/azure-devops-pipelines" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/azure.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=ca9fc956b630fa258a9303701fd0c486" width="1600" height="1000" data-path="assets/_shared/azure.png" />

  <Card title="BitBucket Pipelines" href="/flaky-tests/get-started/ci-providers/bitbucket-pipelines" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/bitbucket.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=e1ffac7bbb707e669e39b99ed1b4c06f" width="1600" height="1000" data-path="assets/_shared/bitbucket.png" />

  <Card title="BuildKite" href="/flaky-tests/get-started/ci-providers/buildkite" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/buildkite.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=e837962388c4ba86e98618e9d46e0275" width="1600" height="1000" data-path="assets/_shared/buildkite.png" />

  <Card title="CircleCI" href="/flaky-tests/get-started/ci-providers/circleci" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/circle-ci.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=7b8ddd92e0e224f932515a46b442deb2" width="1600" height="1000" data-path="assets/_shared/circle-ci.png" />

  <Card title="Drone CI" href="/flaky-tests/get-started/ci-providers/droneci" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/drone.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=cd52584e45cf1cca29fbdd6d937cbc3d" width="1600" height="1000" data-path="assets/_shared/drone.png" />

  <Card title="GitHub Actions" href="/flaky-tests/get-started/ci-providers/github-actions" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/github.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=9f188c818b4496ccd94d2531f0519b2b" width="1600" height="1000" data-path="assets/_shared/github.png" />

  <Card title="GitLab" href="/flaky-tests/get-started/ci-providers/gitlab" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/gitlab.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=0c24d1bc8d892672d10c05e418c43670" width="1600" height="1000" data-path="assets/_shared/gitlab.png" />

  <Card title="Jenkins" href="/flaky-tests/get-started/ci-providers/jenkins" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/jenkins.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=1050e68eefdc912c6b3b940459f834ce" width="1600" height="1000" data-path="assets/_shared/jenkins.png" />

  <Card title="Semaphore" href="/flaky-tests/get-started/ci-providers/semaphoreci" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/semaphore.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=ccc07917600732d2676883fb3bfcbcf1" width="1600" height="1000" data-path="assets/_shared/semaphore.png" />

  <Card title="TeamCity" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/teamcity.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=884de6fa2164c3c38be3bf2ba6923ecf" width="1600" height="1000" data-path="assets/_shared/teamcity.png" />

  <Card title="Travis CI" href="/flaky-tests/get-started/ci-providers/travisci" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/travis.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=d932aa877961f570d15e80f61933ee56" width="1600" height="1000" data-path="assets/_shared/travis.png" />

  <Card title="Other CI Providers" href="/flaky-tests/get-started/ci-providers/otherci" img="https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/other.png?fit=max&auto=format&n=FEDBBs5EPDEY6kRF&q=85&s=2231888b2a96f0a4fb0d2759c25c7b64" width="1600" height="1000" data-path="assets/_shared/other.png" />
</Columns>
