Is it possible to run a single test in MiniTest?

后端 未结 13 1642
旧时难觅i
旧时难觅i 2020-12-12 10:29

I can run all tests in a single file with:

rake test TEST=path/to/test_file.rb

However, if I want to run just one test in that file, how wo

13条回答
  •  余生分开走
    2020-12-12 11:11

    There are 2 ways to do it:

    1. Run tests 'manually' (see Andrew Grimm's answer).
    2. Hack Rake::TestTask target to use a different tests loader.

    Rake::TestTask (from rake 0.8.7) theoretically is able to pass additional options to MiniTest::Unit with a "TESTOPTS=blah-blah" command line option, for example:

    % rake test TEST=test/test_foobar.rb TESTOPTS="--name test_foobar1 -v"
    

    In practice, the option --name (a filter for test names) won't work, due to rake internals. To fix that you'll need to write a small monkey patch in your Rakefile:

    # overriding the default rake tests loader
    class Rake::TestTask
      def rake_loader
        'test/my-minitest-loader.rb'
      end
    end
    
    # our usual test terget 
    Rake::TestTask.new {|i|
      i.test_files = FileList['test/test_*.rb']
      i.verbose = true 
    }
    

    This patch requires you to create a file test/my-minitest-loader.rb:

    ARGV.each { |f|
      break if f =~ /^-/
      load f
    }
    

    To print all possible options for Minitest, type

    % ruby -r minitest/autorun -e '' -- --help

提交回复
热议问题