Is it possible to run a single test in MiniTest?

后端 未结 13 1612
旧时难觅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 10:53

    Following will work

    def test_abc
    end
    
    test "hello world"
    end
    

    This can run by

    bundle exec ruby -I test path/to/test -n test_abc
    
    bundle exec ruby -I test path/to/test -n test_hello_word
    
    0 讨论(0)
  • 2020-12-12 10:58

    I'm looking for similar functionality to rspec path/to/file.rb -l 25

    Yup! Use Nick Quaranto's "m" gem. With it you can say:

    m spec/my_spec.rb:25
    
    0 讨论(0)
  • 2020-12-12 11:03

    The command should be:

    % rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v"
    
    0 讨论(0)
  • 2020-12-12 11:04

    Have you tried:

    ruby path/to/test_file.rb --name test_method_name
    
    0 讨论(0)
  • 2020-12-12 11:07

    If you are using MiniTest with Rails 5+ the best way to run all tests in a single file is:

    bin/rails test path/to/test_file.rb
    

    And for a single test (e.g. on line 25):

    bin/rails test path/to/test_file.rb:25
    

    See http://guides.rubyonrails.org/testing.html#the-rails-test-runner

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题