# RSpec

You can automatically [detect and manage flaky tests](https://docs.trunk.io/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.

### Checklist

By the end of this guide, you should achieve the following before proceeding to the [next steps](#next-step) to configure your CI provider.

* [ ] Set up and installed Trunk's RSpec plugin
* [ ] Disable retries for better detection accuracy
* [ ] Test uploads locally

{% hint style="info" %}
Using the plugin is the best way to accurately detect flaky RSpec tests.

You can also [manually generate and upload](https://docs.trunk.io/flaky-tests/get-started/frameworks/rspec/manual-uploads) test results in RSpec, however, **manual RSpec uploads are not recommended.**
{% endhint %}

### 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`:

{% code title="Gemfile" %}

```shell
gem "rspec_trunk_flaky_tests"
```

{% endcode %}

Install the plugin:

```sh
bundle install
```

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

{% code title="spec/spec\_helper.rb" %}

```shell
require "trunk_spec_helper"
```

{% endcode %}

#### 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 for more robust support and bug fixes, and if you're encountering something unexpected, we first encourage you to:

```
bundle update rspec_trunk_flaky_tests
```

### Environment Variables

These optoinal 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>) |
| `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
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](https://docs.trunk.io/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.

<figure><picture><source srcset="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-8ce83f8ce288a56d53ef28e6e10aa0a9a00a8e73%2Fdata-uploads-dark.png?alt=media" media="(prefers-color-scheme: dark)"><img src="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-3c7ab35af710be9d4e4bf0976a1d848436a04077%2Fdata-uploads-light.png?alt=media" alt=""></picture><figcaption></figcaption></figure>

### Next Steps

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

<table data-view="cards" data-full-width="false"><thead><tr><th></th><th data-hidden></th><th data-hidden data-card-target data-type="content-ref"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><strong>Atlassian Bamboo</strong></td><td></td><td><a href="../ci-providers/atlassian-bamboo">atlassian-bamboo</a></td><td></td></tr><tr><td><strong>Azure DevOps Pipelines</strong></td><td></td><td><a href="../ci-providers/azure-devops-pipelines">azure-devops-pipelines</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-e25fd26aa79fb9b2eed0d2dcd87b1a5914dff23d%2Fazure.png?alt=media">azure.png</a></td></tr><tr><td><strong>BitBucket Pipelines</strong></td><td></td><td><a href="../ci-providers/bitbucket-pipelines">bitbucket-pipelines</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-f7bb3441ba62da7dc26dd780ad0ea8b12c6a9d5b%2Fbitbucket.png?alt=media">bitbucket.png</a></td></tr><tr><td><strong>BuildKite</strong></td><td></td><td><a href="../ci-providers/buildkite">buildkite</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-13bce185a370e3ad7386afc527575e6049f6c90c%2Fbuildkite.png?alt=media">buildkite.png</a></td></tr><tr><td><strong>CircleCI</strong></td><td></td><td><a href="../ci-providers/circleci">circleci</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-29c2601d74f7bb842edccefda703e1c93cafe386%2Fcircle-ci.png?alt=media">circle-ci.png</a></td></tr><tr><td><strong>Drone CI</strong></td><td></td><td><a href="../ci-providers/droneci">droneci</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-8e6723931dcb17d4c7c040feb2139b68e76ce9a9%2Fdrone.png?alt=media">drone.png</a></td></tr><tr><td><strong>GitHub Actions</strong></td><td></td><td><a href="../ci-providers/github-actions">github-actions</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-39bf1729c8521770c91ff6f969a0cad8557eda90%2Fgithub.png?alt=media">github.png</a></td></tr><tr><td><strong>Gitlab</strong></td><td></td><td><a href="../ci-providers/gitlab">gitlab</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-159f50caada873e4a221f14845fb411ddb134a3d%2Fgitlab.png?alt=media">gitlab.png</a></td></tr><tr><td><strong>Jenkins</strong></td><td></td><td><a href="../ci-providers/jenkins">jenkins</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-f23f9fe36af0f5d1941285bfc528aa9a5fff1b1b%2Fjenkins.png?alt=media">jenkins.png</a></td></tr><tr><td><strong>Semaphore</strong></td><td></td><td><a href="../ci-providers/semaphoreci">semaphoreci</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-f86249ac586cebdc0e2f7854126051b7e51a6e9c%2Fsemaphore.png?alt=media">semaphore.png</a></td></tr><tr><td><strong>TeamCity</strong></td><td></td><td><a href="https://github.com/trunk-io/docs/blob/main/.gitbook/includes/broken-reference/README.md">https://github.com/trunk-io/docs/blob/main/.gitbook/includes/broken-reference/README.md</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-b3d36f21bb85001be0fa7ec1ba648f0c169202b7%2Fteamcity.png?alt=media">teamcity.png</a></td></tr><tr><td><strong>Travis CI</strong></td><td></td><td><a href="../ci-providers/travisci">travisci</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-4d1125ee24d6189434ed3ca83f411a3dc6e1ca12%2Ftravis.png?alt=media">travis.png</a></td></tr><tr><td><strong>Other CI Providers</strong></td><td></td><td><a href="../ci-providers/otherci">otherci</a></td><td><a href="https://577236045-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F61Ep9MrYBkJa0Yq3zS1s%2Fuploads%2Fgit-blob-3d5eaefed10f481da930070b192d96463c765041%2Fother.png?alt=media">other.png</a></td></tr></tbody></table>
