With Minitest 6, how do you run one test file, one test method, or only the tests matching a pattern?
answer
- ruby -Ilib:test path/to_test.rb
- -i / --include, old -n still works
- string or /regexp/, Class#method too
- rake test N=... X=... A=...
- minitest path_test.rb:42
basics
~20 sRun the file with ruby -Ilib:test, and filter tests with --include (alias -n) using a name or a /regexp/, or --exclude. Under rake use N=, X= or A=; Minitest 6's minitest command also accepts file.rb:line.
solid answer
~40 sA Minitest file runs on its own: `ruby -Ilib:test test/fine_test.rb`, because `minitest/autorun` runs every loaded test at exit. Arguments after the file go to Minitest: `--include test_caps_fine` (short `-i`; the older `-n`/`--name` still works) keeps tests whose name, or `ClassName#name`, equals the string, and `-i "/cap|waive/"` treats a slash-wrapped string as a regexp. `--exclude` (`-e`, alias `-x`) removes matches. Under `Minitest::TestTask`, `rake test N=/cap/` and `X=` map to those flags and `A="--seed 42 -v"` passes anything else. Minitest 6 also ships a `minitest` command that takes paths and `file.rb:line`, so `minitest test/fine_test.rb:42` runs the test defined on that line. A filter that matches nothing prints `Nothing ran for filter`.
code
bash · 5 linesruby -Ilib:test test/fine_test.rb -i "/caps/" -v
ruby -Ilib:test test/fine_test.rb -e "/slow/"
minitest test/fine_test.rb:42
rake test N="FineCalculatorTest#test_caps_fine"
rake test A="--seed 4242 -v"go deeper
Recall ruby -Ilib:test with a file path, and the include filter with a method name or a /regexp/ to run one test.
Explain exact-string versus regexp matching, ClassName#method targeting, rake's N, X and A variables, and the minitest command's file:line form.
Show you can make CI fail loudly on filters that match nothing and keep rake task globs from silently skipping test directories.
Decide which entry point a team standardises on for local reruns and CI so that commands in failure reports can be pasted straight back.
## Three ways in A Minitest suite is plain Ruby, so there is more than one front door: | entry point | example | notes | |---|---|---| | `ruby` | `ruby -Ilib:test test/fine_test.rb -v` | `minitest/autorun` runs the loaded tests at exit | | `rake test` | `rake test N=/cap/` | task defined by `Minitest::TestTask` or `Rake::TestTask` | | `minitest` | `minitest test/fine_test.rb:42` | command added in Minitest 6.0 | `-Ilib:test` puts `lib` and `test` on the load path so that `require "fine_calculator"` and `require "test_helper"` resolve. The `minitest` command adds both itself. ## Filtering by name Minitest's own options are parsed from the arguments that follow the file: - **`-i PATTERN` / `--include PATTERN`**: keep only matching tests. Minitest 6 renamed the old `-n`/`--name` option to `--include`; `-n` and `--name` are still accepted but no longer shown in `--help`, and the History file says they will be removed. - **`-e PATTERN` / `--exclude PATTERN`** (alias `-x`): drop matching tests. - **Matching rules**: a pattern written as `/.../` becomes a `Regexp`; any other string must equal the method name. Both forms are also tried against `ClassName#method_name`, so `-i "FineCalculatorTest#test_caps_fine"` targets one test in one class. - **Spec names**: `it "caps the fine"` becomes a method called `test_0001_caps the fine`, so a regexp such as `-i "/caps the fine/"` is the practical way to select a spec. If a filter matches nothing, the run succeeds with zero tests and warns `Nothing ran for filter: ...`, with a spelling suggestion when `did_you_mean` can find one. On CI that silent success is worth guarding against. ## Running by line The `minitest` command (the former minitest-sprint gem, now built in) accepts paths, globs and `path:line` arguments. `minitest test/fine_test.rb:42` finds the test method whose source spans line 42 and runs only that; several lines can be joined with commas. When no test covers the line it lists the nearest ones. This is the fastest way to rerun a failure copied from an editor. ## Through rake `Minitest::TestTask.create` (from `require "minitest/test_task"`) defines `rake test` plus helpers, and reads environment variables: 1. `N=` or `I=` becomes `-i`, and `X=` or `E=` becomes `-x`. 2. `A=` passes any extra arguments with shell quoting, for example `rake test A="--seed 42 -v"`. 3. `MT_LIB_EXTRAS=` prepends extra load-path entries. 4. `TESTOPTS` and `FILTER` still work but print a deprecation warning pointing to `A`. Its defaults load `test/**/test_*.rb` and `test/**/*_test.rb` with `lib`, `test` and `.` on the load path and warnings on (`-w`). It also defines `rake test:cmd` (print the command), `rake test:isolated` (run each file alone), `rake test:slow` (slowest 25 tests) and, since 6.0.1, `rake test:fu` (only tests with `FU` in their name). Rake's own `Rake::TestTask` is different: it defaults to the pattern `test/test*.rb` and only `lib` on the load path, and selects files with `TEST=path` and options with `TESTOPTS=`. A project that switches between the two can silently stop loading `test/models/fine_test.rb`. ## Common mistakes - Passing `-n test_caps` expecting a prefix match: a plain string must equal the whole name; use `-n /test_caps/`. - Forgetting the shell: `-i /cap|waive/` without quotes is split by the shell's pipe. - Putting Minitest flags before the file with `ruby`, where Ruby itself parses them. - Assuming `rake test` and `Rake::TestTask` pick up the same files.
- With Minitest, why does -n test_caps run nothing when the method is test_caps_fine?A plain string must equal the method name, or `ClassName#method`, exactly; only a slash-wrapped pattern such as `-n /test_caps/` becomes a regexp. Minitest then warns `Nothing ran for filter: test_caps` and may suggest the closest name.
- With Minitest::TestTask, how do you pass a seed or verbose flag through rake test?Use the `A` variable, which is split with shell quoting and appended after `--`: `rake test A="--seed 4242 -v"`. The older `TESTOPTS` still works but warns and points to `A`. `N` and `X` are shortcuts for the include and exclude filters.
saying these in an interview costs you the question
- Minitest can only run the whole suite through rake.
- -n test_caps matches every test whose name starts with test_caps.
- --include is a new name for the random seed option.
- Rake::TestTask and Minitest::TestTask pick up the same test files by default.
- A filter that matches nothing makes the run fail.