Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rspec-tracer

> 后端框架
Open source

RSpec Tracer is a specs dependency analyzer, flaky tests detector, tests accelerator, and coverage reporter tool.

173 stars0 likes0 views
WebsiteGitHub

About

RSpec Tracer is a specs dependency analyzer, flaky tests detector, tests accelerator, and coverage reporter tool.

rspec-tracer

Test-dependency intelligence for RSpec: detect flaky tests, map code coupling, and, when you are ready, re-run only what changed.

rspec-tracer records the inputs it can observe for each RSpec example (Ruby files, file I/O, Rails templates and queries, declared globs and ENV branches) and turns that record into a flaky-test detector, a per-example dependency map, and optional CI acceleration. The flaky report and the dependency map never skip a test: value before trust.

Measured, not promised. In the maintainer's May 2026 field test (rspec-tracer 2.0.0.pre.1, Apple M2 Max), Mastodon's spec/lib (1,234 examples, Rails 8.1.3, Ruby 3.4.8) ran in 116.4 s cold and 17.0 s warm: 6.85x, with the tracer's own recording overhead included in both runs. "Cold" here is the tracer's own first run, not plain RSpec: recording costs roughly 1.6x plain RSpec on this repository's Rails benchmark fixture (approximate), and no tracer-free Mastodon baseline was measured, so your gain over a suite without the tracer will be smaller than cold minus warm. Suite shape matters: across the same field tests, warm-run skip rates ranged from 31% (a suite carrying 177 pre-existing failures: failed examples are never skipped) to 100%. Measure your own suite before you rely on these numbers.

Conservative by design, honest about its limits. The tracer never skips failed, flaky, or pending examples, nor any example whose recorded inputs changed; when a recorded input is ambiguous, it re-runs. Real blind spots exist (runtime metaprogramming and monkey-patches are invisible to it; the tracks: DSL is the escape hatch), and the soundness model catalogues the known blind spots. Shadow mode (run everything, report what would have been skipped) lands in v2.0.0.rc.2, so you can verify the tracer on your own data before trusting it with a single skip.

Your data never leaves your infrastructure. Everything runs inside your test process and your CI cache: no SaaS backend, no telemetry, no per-seat pricing.

Quick start

Requires Ruby 3.1+ and rspec-core 3.12+ (Rails 7.0+ when using Rails; Rails 8.0 needs Ruby 3.2+; JRuby 9.4 supported). Every CI-gated Ruby is supported until at least six months past its upstream EOL; see Maintenance.

ruby
# Gemfile: 2.0 is in pre-release; pin explicitly until 2.0.0 final
gem 'rspec-tracer', '= 2.0.0.rc.1', group: :test, require: false

Add the tracer's working directories to your .gitignore before the first run:

rspec_tracer.lock
rspec_tracer_cache/
rspec_tracer_coverage/
rspec_tracer_report/
ruby
# Top of spec_helper.rb / rails_helper.rb, before any application
# code. With SimpleCov, start SimpleCov first: load order is part
# of the contract.
require 'rspec_tracer'
RSpecTracer.start

Run the suite twice. The second run skips the examples whose recorded inputs are unchanged and prints a per-reason breakdown of what re-ran; open rspec_tracer_report/index.html to audit every per-example decision. Not ready to let it skip anything? Set RSPEC_TRACER_RUN_ALL_EXAMPLES=true (or run_all_examples true inside the RSpecTracer.configure block in .rspec-tracer); every example still runs while the flaky report and the dependency maps accumulate data.

Setting up CI? docs/CI_RECIPES.md has copy-paste cache recipes for CircleCI, GitLab CI, Buildkite, and Heroku CI, plus a link to the canonical GitHub Actions workflow.

Table of contents

  • Quick start
  • What it saves
  • How it works
  • Per-example tracks: DSL
  • Rails quick start
  • Configuring CI
  • Remote cache backends
  • Pluggable storage
  • Command-line tools
  • SimpleCov interop
  • FAQ + comparison
  • Reports
  • Maintenance
  • Documentation + coverage
  • Help and community
  • Section anchor map (1.x -> 2.0)

What it saves

All timings are wall-clock and measured, not projected: maintainer field tests, May 2026, Apple M2 Max, rspec-tracer 2.0.0.pre.1. In every row, "cold" is the tracer's own first, recording run: it costs more than plain RSpec (roughly 1.6x on this repository's Rails benchmark fixture; approximate), and no tracer-free baseline was measured for these projects, so your savings versus a suite without the tracer will be smaller than cold minus warm.

Project measured Examples Cold run Warm run Warm skip rate Saved per warm run
Mastodon spec/lib (Rails 8.1.3, Ruby 3.4.8) 1,234 116.4 s 17.0 s 98% (1,212 skipped) ~99 s
thoughtbot/clearance (Rails 8 engine + dummy app) 258 11.1 s 1.6 s 100% ~9.5 s
publify (carries 177 pre-existing failures; failed examples are never skipped) 257 6.7 s 4.8 s 31% ~1.9 s

The Mastodon warm run is the zero-change best case; in the same field test, editing one file re-ran 68 of 1,234 examples in 19.0 s. Laptop rows understate CI savings: CI runners are slower than the development machine, so the seconds saved per run are typically larger there; the skip ratios are what transfer.

Worked examples (every input is an assumption you should replace with your own). For any suite:

(cold seconds - warm seconds) x runs/day / 60 x $/CI-minute x 30 = $/month per full-suite job

At $0.006/CI-minute (illustrative: GitHub-hosted Linux list price at the time of writing; check your provider's current rate) and 50 CI runs/day on one job:

  • Best case (a Mastodon-sized suite saving ~99 s per warm run): ~83 CI-minutes/day, about $15/month per job.
  • Low-skip case (a publify-shaped suite saving ~1.9 s per warm run at 31% skip): about $0.29/month per job.

Multiply by the number of jobs that each run the full suite (for example, a Ruby-version matrix); jobs that split one suite across workers share the saving rather than multiply it.

Conservative assumptions: 50 runs/day and the dollar rate are stated assumptions, not measurements; your skip rate may be far lower (see the 31% row: suites with many failing examples skip less, by design); remote-cache download time on fresh CI workers is not subtracted (measured end-to-end remote-cache gain on clearance was still ~4.6x).

How it works

Each example's observed inputs are digested and stored per example: executed Ruby source (via Coverage), file I/O (via Module#prepend hooks), Rails framework events, declared globs and env vars, and whole-suite invalidators like Gemfile.lock. The next run recomputes the digests and skips examples whose recorded inputs are unchanged. The full input taxonomy lives in ARCHITECTURE.md; what each input kind does and does not guarantee is the soundness model; the Rails config.eager_load precision trade-off is covered by the Rails recipe in COOKBOOK.md.

Per-example tracks: DSL

Annotate any describe / context / example with extra inputs the tracker can't auto-observe — config files baked at boot, env-var branches, non-Ruby deps:

ruby
RSpec.describe AdminController,
               tracks: { files: 'app/policies/**/*.rb',
                         env: 'ROLE_CONFIG' } do
  it 'gates on the feature flag' do
    # ...
  end
end
Key Value shape Semantics
:files String glob OR Array of strings Each matched file is attached as a :declared-kind dep on the example.
:env String name OR Array OR wildcard Each named env var is digested at finalize. Missing env reads as empty.

Wildcards ('RAILS_*', '*_TOKEN', '*') expand against the live ENV at register time; the persisted snapshot only carries concrete keys.

Cascade. Nested groups contribute additively. A parent's tracks: { files: 'a/*' } and a child's tracks: { env: 'B' } both attach to the child's examples; the child does not clobber the parent on any shared key.

Use track_files / track_env for the global case. Files / env vars every test depends on (Gemfile.lock, AUTH_TOKEN, RAILS_ENV) belong in .rspec-tracer:

ruby
RSpecTracer.configure do
  track_files 'config/locales/**/*.yml', 'db/schema.rb', 'Gemfile.lock'
  track_env   'AUTH_TOKEN', 'DATABASE_URL', 'RAILS_*'
end

Rails quick start

ruby
# spec/rails_helper.rb
require 'simplecov'      # if used; load BEFORE rspec_tracer
SimpleCov.start

require 'rspec_tracer'
RSpecTracer.start

require File.expand_path('../config/environment', __dir__)
require 'rspec/rails'
ruby
# .rspec-tracer
RSpecTracer.configure do
  track_rails_defaults
end

track_rails_defaults attaches the common Rails-side declared globs: views, locales, fixtures, factories, helpers, config. Drop a specific glob to hand attribution off to the per-example subscribers instead:

ruby
RSpecTracer.configure do
  # Templates → render_template.action_view subscriber attributes
  # them per-example. Schema → opt-in sql.active_record observer
  # attributes db/schema.rb + db/structure.sql per AR-touching
  # example (read the Narrow AR-schema attribution section before
  # enabling).
  track_rails_defaults except: [:views, :schema]
  track_ar_schema_notifications
end

Supported Rails matrix: 7.0 / 7.1 / 7.2 / 8.0 (8.0 requires Ruby 3.2+). On JRuby use JRUBY_OPTS="--debug -X+O" and the JDBC adapter (gem 'activerecord-jdbcsqlite3-adapter', '~> 71.0', platforms: :jruby for Rails 7.1; track the major to your Rails line).

Narrow AR-schema attribution

track_ar_schema_notifications promises per-example attribution of db/schema.rb via the sql.active_record subscriber. The narrow promise only holds when no per-example AR cleanup mechanism fires queries inside the per-example bucket. Common Rails setups trip this:

  • use_transactional_fixtures = true (Rails default) — per-example BEGIN/COMMIT fires sql.active_record.
  • DatabaseCleaner :truncation / :deletion / :transaction in around hooks — cleanup queries fire inside the bucket.

Either case attributes db/schema.rb to every AR-touching example (safe, but widens invalidation). A boot-time warn fires when the precondition is unmet so you don't discover this from a confused cache-hit-rate chart later.

For genuinely narrow attribution: set use_transactional_fixtures = false and use sequence-based factories (or another non-AR cleanup mechanism that doesn't fire sql.active_record inside the example window).

Working with parallel_tests

Supported out of the box. The tracker writes to per-worker directories and merges at finalize on the elected last worker. If you interrupt a run mid-flight, delete the lock file:

bash
rm -f rspec_tracer.lock && bundle exec parallel_rspec

Configuring CI

Copy-paste cache recipes for CircleCI, GitLab CI, Buildkite, and Heroku CI live in docs/CI_RECIPES.md; the canonical GitHub Actions workflow is .github/workflows/example-tracer-cache.yml. The rest of this section covers the remote-cache rake flow and the cache-key pattern all the recipes share.

The 1.x flow is preserved bit-for-bit. In your project's Rakefile:

ruby
spec = Gem::Specification.find_by_name('rspec-tracer')
load "#{spec.gem_dir}/lib/rspec_tracer/remote_cache/Rakefile"

Then in your CI pipeline:

bash
bundle exec rake rspec_tracer:remote_cache:download
bundle exec rspec
bundle exec rake rspec_tracer:remote_cache:upload

With these env vars:

Issues· 24 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubycoveragecoverage-reportjrubyparallel-tests

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架