skip to content

With bin/rails test in Rails 8.1, how do you run one test by line, by name, or by folder, and what does a bare run skip?

level: middleimportance: should knowfreq 42%

answer

  1. file:line and file:start-end
  2. a folder expands to *_test.rb
  3. -i takes the test "..." string
  4. -n still works but warns
  5. test/system excluded by default

basics

~20 s

bin/rails test takes paths with :line or :start-end suffixes and folders, filters by name with -i (Rails turns a test description into its method name), warns on -n, and by default skips test/system, which test:all includes.

solid answer

~40 s

`bin/rails test test/models/booking_test.rb:27` runs the test defined on line 27, `:10-20` runs every test whose definition overlaps that range, and several `:line` suffixes or several paths can be combined. A folder argument such as `test/controllers` expands to `**/*_test.rb` inside it. To select by name I use `-i` / `--include`. A plain string is turned into the method name the `test "..." do` macro defines, so `-i "rejects overlapping bookings"` matches `test_rejects_overlapping_bookings`, and `/pattern/` is a regexp. Under Minitest 6, Rails still accepts `-n` / `--name` but prints "Please switch from -n/--name to -i/--include". A bare `bin/rails test` loads `test/**/*_test.rb` minus `test/{system,dummy,fixtures}`. `bin/rails test:all` adds system tests, and `-f` stops at the first failure.

code

bash · 6 lines
bash
bin/rails test test/models/booking_test.rb:27
bin/rails test test/models/booking_test.rb:10-20
bin/rails test test/controllers test/integration/checkout_test.rb
bin/rails test -i "rejects overlapping bookings"
bin/rails test -i /overlap/ -f
bin/rails test:all

go deeper

for a junior

Know file:line, folders and the copy-paste rerun line, and that bin/rails test skips system tests unless you run test:all.

for a middle

Explain how -i patterns are normalised to test_ method names, the line-range syntax, and why Minitest 6 makes Rails warn on -n.

for a senior

Build fast feedback loops with fail-fast, profiling and targeted reruns, and make sure CI's command really runs every test folder.

for a principal

Decide which test commands CI and developers run, so a split between fast local runs and full runs does not leave a folder untested.

## What bin/rails test is **`bin/rails test`** is Rails' runner for Minitest. It sets `RAILS_ENV` to `test` (or the environment you pass with `-e`), runs the `test:prepare` task first unless an argument is a test path, loads the matching test files, and hands them to Minitest with Rails' plugin options added. Its help line says what it is for: "Run tests except system tests". ## Selecting by location - **One file:** `bin/rails test test/models/booking_test.rb`. - **One test by line:** `bin/rails test test/models/booking_test.rb:27` runs the test whose definition covers line 27. - **A line range:** `test/models/booking_test.rb:10-20` runs every test whose definition overlaps that range. - **Several lines or files:** `booking_test.rb:12:40` and `bin/rails test test/controllers test/integration/checkout_test.rb` both work. - **A folder:** any directory argument is expanded to `<dir>/**/*_test.rb`. When a test fails, the reporter prints a ready-made rerun line in the form `bin/rails test test/models/booking_test.rb:27`, so copying it is the fastest way back to one failure. ## Selecting by name Rails' declarative `test "rejects overlapping bookings" do` defines a method called `test_rejects_overlapping_bookings`. Name filters work on those method names: | Flag | Meaning in Rails 8.1 with Minitest 6 | |---|---| | `-i` / `--include PATTERN` | run only tests whose name matches | | `-e` / `--environment ENV` | which Rails environment to boot; Rails strips it before Minitest parses the rest | | `--exclude PATTERN` | Minitest's opposite of include, passed through | | `-n` / `--name PATTERN` | still accepted, but Rails warns "Please switch from -n/--name to -i/--include" | Rails normalises the pattern for you: 1. A string in slashes, like `/overlap/`, is treated as a regular expression. 2. A plain string that does not start with `test_` gets `test_` prepended and its spaces turned into underscores, so `-i "rejects overlapping bookings"` matches the declarative test exactly. 3. When you also give `file:line` filters, Rails combines both, and a test runs if it matches either. ## What a bare run includes and skips - With no paths, the runner globs `test/**/*_test.rb` and **excludes `test/{system,dummy,fixtures}/**`**. The `DEFAULT_TEST` and `DEFAULT_TEST_EXCLUDE` environment variables override those two globs. - `bin/rails test:all` runs everything **including** `test/system`. - Folder shortcuts exist as subcommands: `test:models`, `test:controllers`, `test:integration`, `test:jobs`, `test:mailers` and the others listed in the runner's `TEST_FOLDERS`, plus `test:units` and `test:functionals`. - System tests also have their own subcommand; their setup belongs with the system-test material. ## Useful Rails-only switches - `-f` / `--fail-fast` aborts on the first failure or error. - `-d` / `--defer-output` prints failures at the end instead of inline. - `-b` / `--backtrace` shows the full backtrace instead of the cleaned one. - `--profile [COUNT]` lists the slowest tests, ten by default. ## Why the -n warning appears Minitest 6 renamed its name filter to `-i` / `--include`. Rails' plugin keeps `-n` / `--name` working on Minitest 6 by mapping it to the include option, and prints the warning so scripts and editor integrations move to the new flag. Remember that the runner also prints `Running N tests in a single process (parallelization threshold is 50)` when you select only a handful of tests. That is expected: small selections skip the parallel workers. ## A typical debugging loop 1. Run the folder you changed: `bin/rails test test/models`. 2. Copy the rerun line the reporter prints for the first failure and run just that test. 3. Fix, then widen back out with `-i /overlap/` to run every related test by name. 4. Finish with a bare `bin/rails test`, and `bin/rails test:all` if the change touched pages that system tests cover. Keeping that order means the slow, broad runs happen once, at the end, instead of after every edit.

  • Why does bin/rails test -i "rejects overlapping bookings" find a test written with the test macro?
    `ActiveSupport::Testing::Declarative#test` defines `test_rejects_overlapping_bookings`, replacing whitespace with underscores. Rails' runner applies the same transformation to a plain-string filter that does not already start with `test_`, so the string matches the generated method name exactly.
  • Your bin/rails test run passes locally, but CI also runs browser tests. Which command covers both?
    `bin/rails test:all`, which loads `test/**/*_test.rb` including `test/system`. A bare `bin/rails test` excludes `test/system`, `test/dummy` and `test/fixtures` by default, so system tests only run when asked for explicitly.

  • A bare bin/rails test run also runs everything in test/system.
  • In bin/rails test, -e excludes tests matching a pattern.
  • -n is the only way to filter Rails tests by name.
  • -i "rejects overlapping bookings" cannot match a test written with the test macro.
  • bin/rails test file.rb:27 runs every test from line 27 to the end.

Assumes Rails 8.1 with Minitest 6, where -i/--include is the name filter and Rails warns on -n/--name.