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

tldr

> 测试质量
Open source

A Ruby test framework for people who don't have time for slow tests

64.3K stars0 likes8 views
WebsiteGitHub

About

A Ruby test framework for people who don't have time for slow tests

TLDR - Testing for rubyists who want fast feedback

TLDR is a suspiciously-delightful testing framework for Ruby.

As a test library, TLDR is largely API-compatible with Minitest. As a test runner, TLDR boasts a few features RSpec's CLI still doesn't have.

The library, command line interface, and every decision in-between was prioritized to maximize productivity to promote fast feedback loops. Some highlights:

  • Numerous ways to run specific tests: path (foo_test.rb), line number (foo_test.rb:13), name (--name test_foo), or regex pattern (-n /_foo/)
  • Parallel test execution by default, as well as controls for serial execution of thread-unsafe tests
  • Continuously run after every file change with --watch
  • An optional timer to enforce your tests never get slow with --timeout
  • A --fail-fast flag that aborts the run as soon as a failure is encountered
  • Running your most-recently-edited test before all the others (see --prepend)
  • Delightful diffs when assertions fail, care of super_diff

We hope you'll give it a try!

Getting started

You can either read the rest of this README and learn about TLDR passively, or you can just clone this TLDR demo repo and work through its README as you play with its tests and run them with various options.

JUST IN CASE YOU'RE ALREADY SKIMMING THIS, I said stop reading and clone this interactive repo if you're a hands-on learner.

Install

You know the drill. gem install tldr or add it to your Gemfile:

gem "tldr"

Project setup

By default, TLDR expects your tests to be in test/ with filenames that match test_*.rb or *_test.rb and will require a test/helper.rb if you define one.

Configuring TLDR

CLI Options

…

Setting defaults in .tldr.yml

The tldr CLI will look for a .tldr.yml file in the root of your project that can set all the same options supported by the CLI. You can specify a custom YAML location with --config some/path.yml if you want it to live someplace else.

Any options found in the dotfile will override TLDR's defaults, but can still be overridden by the tldr CLI or a TLDR::Config object when running tests programmatically.

Here's an example project that specifies a .tldr.yml file as well as some internal tests demonstrating its behavior.

Writing your tests

If you've ever seen a Minitest test, then you already know how to write TLDR tests. Rather than document how to write tests, this section just highlights the ways TLDR tests differ from Minitest tests.

First, instead of inheriting from Minitest::Test, TLDR test classes should descend from (wait for it) the TLDR base class:

ruby
class MyTest < TLDR
  def test_looks_familiar
    assert true
  end
end

Second, if your tests depend on a test helper, it will be automatically loaded by TLDR if you name it test/helper.rb. That means you don't need to add require "helper" to the top of every test. If you want to name the helper something else, you can do so with the --helper option:

tldr --helper test/test_helper.rb

Third, TLDR offers fewer features:

  • No built-in mock library (use mocktail, maybe!)
  • No "spec" API
  • No benchmark tool
  • No bisect script

And that's it! You officially know how to write TLDR tests.

Running your tests

Because TLDR ships with a CLI, it offers a veritable plethora of ways to run your tests.

Running your tests

Once installed, running all your tests is just five keystrokes away:

tldr

This assumes your tests are stored in test/. It will also add lib/ to Ruby's load paths and require test/helper.rb before your tests, if it exists.

Running TLDR with Rake

TLDR ships with a minimal rake task that simply shells out to the tldr CLI by default. If you want to run TLDR with Rake, you can configure the task by setting flags on an env var named TLDR_OPTS or in a .tldr.yml file.

All your Rakefile needs is require "tldr/rake" and you can run the task individually like this:

$ rake tldr

# Or, with options in TLDR_OPTS
$ TLDR_OPTS="--no-parallel" rake tldr

Here's an example Rakefile that runs both TLDR and Standard Ruby as the default task:

ruby
require "standard/rake"
require "tldr/rake"

task default: ["tldr", "standard:fix"]

One situation where you'd want to invoke TLDR with Rake is when you have multiple test suites that you want to be able to easily run separately (this talk discussed a few reasons why this can be useful).

To create a custom TLDR Rake task, you can instantiate TLDR::Task like this, which allows you to define its TLDR::Config configuration in code:

ruby
require "tldr/rake"

TLDR::Task.new(name: :safe_tests, config: TLDR::Config.new(
  paths: FileList["safe/**/*_test.rb"],
  helper_paths: ["safe/helper.rb"],
  load_paths: ["lib", "safe"]
))

The above will create a second Rake task named safe_tests running a different set of tests than the default tldr task. Here's an example from TLDR's test suite.

Running tests continuously with --watch

The tldr CLI includes a --watch option that will watch for changes in any of the configured load paths (["test", "lib"] by default) and then execute your tests each time a file is changed. To keep the output up-to-date and easy to scan, it will also clear your console before each run.

Note that this feature requires you have fswatch installed and on your PATH

Here's what that might look like with the --emoji flag enabled:

Running tests programmatically

If you'd rather use TLDR by running Ruby files instead of the tldr CLI, you can simply require "tldr/autorun" (just like require "minitest/autorun").

Given a file test/some_test.rb:

ruby
require "tldr/autorun"

class SomeTest < TLDR
  def test_truth
    assert true
  end
end

You can then run the test by passing ruby the file:

$ ruby test/some_test.rb

Any CLI options you add will still be parsed, as well (e.g. ruby my_test.rb --emoji will work).

If you want to be explicit about setting the Kernel.at_exit hook, or if you want to configure TLDR with code, you can invoke TLDR.at_exit! directly:

ruby
require "tldr"
TLDR::Run.at_exit! TLDR::Config.new(emoji: true)

Failing with style

Failing fast and first

If we just want to know if the build passes, we want to know as fast as possible. Ever see a test fail and then sit around waiting for the whole suite to finish running anyway? Why wait? Turn on --fail-fast and abort the test run the instant a failure is encountered:

tldr --fail-fast

Additionally, you might notice the top of each run will show you a command you can use to execute the same run, like this:

Command: bundle exec tldr --fail-fast --prepend "test/calculator_test.rb"

That's because TLDR will look at the file system and move your most-recently-edited test file to the front of the queue with --prepend. When used in conjunction with --fail-fast, you'll fail extra fast, because the most likely test to fail is the one you're actively working on.

Enforcing a testing --timeout

We initially developed TLDR because we wanted a test runner that supported suite-wide time limits as a first-class feature. When test suites become slow, people run them (much) less often. And once a developer gets in the habit of only running tests occasionally, it's not long before they only run them before push, and then only the ones they're immediately working on, and then they just wait for CI to run them in a pull request. And if you don't run all your tests very often, you don't feel any pain when you make your tests (or the code its testing) slower.

Each time you write code and don't run your tests, you're making an assumption that whatever code you just wrote works flawlessly. If that assumption is correct, you saved however much time it takes to run your tests. But every time that assumption is incorrect, you've just extended the amount of time before discovering that you broke something. If that's a few minutes later, that might only cost a few minutes of rework. If you only run tests once or twice a day, you might have to undo hours of work to fix it.

We originally came up with the idea of TLDR on a livestream, joking that only an unconfigurable 1.8 second time limit would prevent test suites from ballooning in duration over time. As of 1.0, we've made the timeout configurable, but we still think it's a good idea to enable it with the --timeout option:

tldr --timeout

Not only does a timeout keep us running the whole suite frequently (it'll never take more than 1.8 seconds, after all), but even if the suite begins to exceed our self-imposed timeout, TLDR's random test order and parallel execution means that—so long as you keep running that partial suite frequently—you'll still be running ALL your tests many more times than if they waited until some arbitrary checkpoint to run them.

When enabled, --timeout will set the timer to 1.8 seconds. But you can set whatever time limit you like. The right value is depends on your individual capacity for paying attention. Basically, "however long you're willing to wait without caving and running your tests less often."

Examples:

# A badass 200ms timeout
tldr --timeout 0.2

# A miserable-sounding 20 second timeout
tldr --timeout 20

If we've won you over towards this way of working, we suggest creating a .tldr.yml file in the root of your project and specifying your desired timeout.

For TLDR Classic™ and a 1.8s timeout:

yaml
# .tldr.yml
timeout: true

Or any number of seconds you like:

yaml
# .tldr.yml
timeout: 0.01

And if you're running with the timeout enabled this way, you can still disable it for any given test run by adding the --no-timeout flag.

Consider timeouts as a success with exit code 0

By default, when TLDR times out it exits with status code 3 to indicate the test suite was aborted. However, if you know that your full test suite is slower than whatever duration you consider to be "fast enough for fast feedback", you can use the --exit-0-on-timeout flag and simply use the timeout to enforce whatever feedback loop budget you set with --timeout.

For example:

# Get feedback within 400ms but don't consider a timeout a failure
tldr --timeout 0.4 --exit-0-on-timeout

This is particularly useful for:

  • Claude Code hooks where you want fast feedback without failing the command
  • Git pre-commit hooks where you want quick test results without blocking commits
  • Development workflows where timeouts are informational rather than failures
  • Any situation where you want a time budget without hard failures

Using TLDR as a Claude Code hook

In AI agent-based coding workflows, it's common to configure hooks that will block the agent from proceeding whenever tests or linters fail. With Claude Code specifically, a hook will only block if i

Issues· 3 open

View all issuesOpen on GitHub
  • #14

    Private method `test' called for nil (NoMethodError)

    bugUpdated Oct 30, 2024
  • #12

    Parallel tests sometimes get locked when the same file is loaded via active_support/dependencies/autoload, causing test timeout

    Updated Dec 29, 2023

> Tags

Rubyassertionstddtest-runnertesting

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedDec 1, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.