How to run all tests with minitest?

RubyMinitest

Ruby Problem Overview


I downloaded source code for a project, found a bug, and fixed it.

Now I want to run tests to find out if I have broken anything.

The Tests are in minitest DSL.

How do I run them all at once?

I searched for applicable rake tasks etc, but I didn't find any.

Ruby Solutions


Solution 1 - Ruby

Here's a link to Rake::TestTask.

There is an example in the page to get you started.
I'll post another one that I'm using right now for a gem:

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = "spec/*_spec.rb"
end

As you can see, I assume that my files are all in /lib and that my specs are in /spec and are named whatever_spec.rb

Hope it helps.

Solution 2 - Ruby

locks' answer is better, but I also wanted to point out that you can also run minitest directly from the command like with the ruby command. To run the tests in the spec/calculator_spec.rb file run:

$ ruby spec/calculator_spec.rb 

Remember to include the following code in the calculator_spec.rb file:

require 'minitest/spec'
require 'minitest/autorun'

To run all tests in the spec/ directory, use the following command (see this post for more details https://stackoverflow.com/questions/12048579/globbing-doesnt-work-with-minitest-only-one-file-is-run?lq=1)

$ for file in spec/*.rb; do ruby $file; done 

Solution 3 - Ruby

This is what Rake::TestTask does under the hood, more or less:

ruby -Ilib -e 'ARGV.each { |f| require f }' ./test/test*.rb

Note: lib & test/test*.rb (above) are the defaults but test & test/*_test.rb, respectively, are more typical.

Source: rake/testtask.rb at c34d9e0 line 169

If you're using JRuby and want to avoid paying the startup cost twice (once for Rake and then once for the subprocess that Rake starts), just use that command.

Solution 4 - Ruby

Here is my entire rakefile, which I put in my top directory:

task :default => :test
task :test do
  Dir.glob('./test/*_test.rb').each { |file| require file}
end

To run all my test files at once, I just type rake. That's it!

Make sure to have require 'minitest/autorun' at the top of each of your Minitest files. Dir.glob definitely DOES work with Minitest.

To get pretty, colored Minitest output, with names of all my test methods, I have the file minitest_helper.rb in my /test directory. (Had to install the gem minitest-reporters):

require 'minitest/reporters'
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
require 'minitest/autorun'

I just had to require_relative './minitest_helper' at the top of each of my test files.

Solution 5 - Ruby

This can also be done via a Makefile.

default:
  echo "Dir.glob('./test/*_test.rb').each { |file| require file}" | ruby

running make will run all your tests.

Solution 6 - Ruby

$ rake test ran out of the rails 5.0 box.

Solution 7 - Ruby

Another way to do this using only Ruby's standard library is with Dir.glob. From within a ruby file, this would look like this:

require "minitest/autorun"

Dir.glob("**/*Test.rb") { |f| require_relative(f) }

Or from the commandline, you can use this command:

ruby -I . -e "require 'minitest/autorun'; Dir.glob('**/*Test.rb') { |f| require(f) }"

Dir.glob('**/*Test.rb') recursively searches the current directory for any file which matches *Test.rb, so we simply take all those files and require or require_relative them. From the commandline, require_relative fails, so we use require but first add the current directory to the $LOAD_PATH through -I .

Solution 8 - Ruby

I realize this is a very old question, but rake test works for me in Rails 4.2, including files under test/ as well as test/integration/, test/unit/, etc.

Solution 9 - Ruby

If you don't have rake, try this:

http://blog.gingergriffis.com/post/85871430778/ruby-how-to-run-all-tests-in-a-directory

# run_all_tests.rb

require 'find'
require 'optparse'

options = {
  :exclude => [],
}

OptionParser.new do |opts|
  opts.on('--exclude Comma Separated String',
          'Test scripts to exclude') do |css|
    options[:exclude] = css.split(',')
  end
end.parse!

commands = []

Find.find(File.dirname(__FILE__)) do |path|
  Find.prune if path =~ /#{__FILE__}$/
    if !File.directory?(path) && (path =~ /(.*)\.rb$/)
      if options[:exclude].none? {|e| path.include?(e)}
        commands << "ruby #{path}"
      end
    end
end
command_string = commands.join(" && ")
exec(command_string)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionguaiView Question on Stackoverflow
Solution 1 - RubylocksView Answer on Stackoverflow
Solution 2 - RubyPowersView Answer on Stackoverflow
Solution 3 - RubyTheoView Answer on Stackoverflow
Solution 4 - RubyRaymond GanView Answer on Stackoverflow
Solution 5 - RubydkinzerView Answer on Stackoverflow
Solution 6 - RubythedanottoView Answer on Stackoverflow
Solution 7 - RubyJustinView Answer on Stackoverflow
Solution 8 - RubyDavid HempyView Answer on Stackoverflow
Solution 9 - RubySedonaView Answer on Stackoverflow