class Minitest::TestTask - minitest: Ruby Standard Library Documentation

# class Minitest::TestTask

`Minitest::TestTask` is a rake helper that generates several rake tasks under the main test task’s name-space.

    task <name>      :: the main test task
    task <name>:cmd  :: prints the command to use
    task <name>:deps :: runs each test file by itself to find dependency errors
    task <name>:slow :: runs the tests and reports the slowest 25 tests.

Examples:

    Minitest::TestTask.create

The most basic and default setup.

    Minitest::TestTask.create :my_tests

The most basic/default setup, but with a custom name

    Minitest::TestTask.create :unit do |t|
      t.test_globs = ["test/unit/**/*_test.rb"]
      t.warning = false
    end

Customize the name and only run unit tests.

NOTE: To hook this task up to the default, make it a dependency:

    task default: :unit

### Attributes

extra_args[RW]

Extra arguments to pass to the tests. Defaults empty but gets populated by a number of enviroment variables:

N (-n flag)

  a string or regexp of tests to run.

X (-e flag)

  a string or regexp of tests to exclude.

A (arg)

  quick way to inject an arbitrary argument (eg A=–help).

See `process_env`

framework[RW]

The code to load the framework. Defaults to requiring minitest/autorun…

Why do I have this as an option?

libs[RW]

Extra library directories to include. Defaults to %w[lib test .]. Also uses $MT_LIB_EXTRAS allowing you to dynamically override/inject directories for custom runs.

name[RW]

The name of the task and base name for the other tasks generated.

test_globs[RW]

File globs to find test files. Defaults to something sensible to find test files under the test directory.

test_prelude[RW]

Optional: Additional ruby to run before the test framework is loaded.

verbose[RW]

Print out commands as they run. Defaults to Rake’s `trace` (-t flag) option.

warning[RW]

Turn on ruby warnings (-w flag). Defaults to true.

### Public Class Methods

create(name = :test, &block) click to toggle source

Create several test-oriented tasks under `name`. Takes an optional block to customize variables.

    # File minitest-5.25.4/lib/minitest/test_task.rb, line 50
    def self.create name = :test, &block
      task = new name
      task.instance_eval(&block) if block
      task.process_env
      task.define
      task
    end

### Public Instance Methods

make_test_cmd(globs = test_globs) click to toggle source

Generate the test command-line.

    # File minitest-5.25.4/lib/minitest/test_task.rb, line 259
    def make_test_cmd globs = test_globs
      tests = []
      tests.concat Dir[*globs].sort.shuffle # TODO: SEED -> srand first?
      tests.map! { |f| %(require "#{f}") }

      runner = []
      runner << test_prelude if test_prelude
      runner << framework
      runner.concat tests
      runner = runner.join "; "

      args  = []
      args << "-I#{libs.join File::PATH_SEPARATOR}" unless libs.empty?
      args << "-w" if warning
      args << "-e"
      args << "'#{runner}'"
      args << "--"
      args << extra_args.map(&:shellescape)

      args.join " "
    end

process_env() click to toggle source

Extract variables from the environment and convert them to command line arguments. See `extra_args`.

Environment Variables:

MT_LIB_EXTRAS

  Extra libs to dynamically override/inject for custom runs.

N

  Tests to run (string or /regexp/).

X

  Tests to exclude (string or /regexp/).

A

  Any extra arguments. Honors shell quoting.

Deprecated:

TESTOPTS

  For argument passing, use `A`.

N

  For parallel testing, use `MT_CPU`.

FILTER

  Same as `TESTOPTS`.

    # File minitest-5.25.4/lib/minitest/test_task.rb, line 144
    def process_env
      warn "TESTOPTS is deprecated in Minitest::TestTask. Use A instead" if
        ENV["TESTOPTS"]
      warn "FILTER is deprecated in Minitest::TestTask. Use A instead" if
        ENV["FILTER"]
      warn "N is deprecated in Minitest::TestTask. Use MT_CPU instead" if
        ENV["N"] && ENV["N"].to_i > 0

      lib_extras = (ENV["MT_LIB_EXTRAS"] || "").split File::PATH_SEPARATOR
      self.libs[0, 0] = lib_extras

      extra_args << "-n" << ENV["N"]                      if ENV["N"]
      extra_args << "-e" << ENV["X"]                      if ENV["X"]
      extra_args.concat Shellwords.split(ENV["TESTOPTS"]) if ENV["TESTOPTS"]
      extra_args.concat Shellwords.split(ENV["FILTER"])   if ENV["FILTER"]
      extra_args.concat Shellwords.split(ENV["A"])        if ENV["A"]

      ENV.delete "N" if ENV["N"]

      # TODO? RUBY_DEBUG = ENV["RUBY_DEBUG"]
      # TODO? ENV["RUBY_FLAGS"]

      extra_args.compact!
    end