9

Is it possible to let minitest run only the failed tests via rake task?

Couldnt find anything in internet about this... With rspec or cucumber it worked.. Is there any possibility here?

BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111
  • 2
    The root of the problem is that the state of your tests is not recorded anywhere, so how could possibly rerun the failed tests. It is possible to run single MiniTest tests (see: http://stackoverflow.com/questions/5285711/is-it-possible-to-run-a-single-test-in-minitest), so you could write a custom test runner that records the failed tests to a file and reads that file in under certain run conditions. – Max Sep 20 '14 at 13:36
  • @Max, if your comment were an answer, I'd vote for it :) – Jared Beck Sep 24 '14 at 17:22
  • You could make a plugin to cache the failed tests and have minitest only run those (look at the __run method in https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb). You'll probably want to create a plugin that hooks into the failed tests – Josh Bodah Oct 08 '14 at 12:40

3 Answers3

1

You can check out the guard-minitest gem. I think that's what you are looking for.

mrnovalles
  • 353
  • 3
  • 7
  • 2
    This guard-plugin does run files you change, but it does not keep a list of (failed) tests to rerun every time it runs. – kronn Jun 29 '17 at 15:19
1

The closest thing I could find is this plugin:

https://github.com/ivantsepp/minitest-rerun-options

It outputs command line options for failing tests like this:

Rerun failed tests options:
--name TestExample#test_another_that_will_fail
--name TestExample#test_that_will_fail

so you can append them to your rake test command.

Milos Blasko
  • 614
  • 10
  • 25
1

spring: 'bin/rails test' # option in the Guardfile will run only the changed files.

I have the following config in Guardfile. (The project had a mix of Unit & Spec style tests written)

# all_on_start: false               # run all tests in group on startup, default: true
# all_after_pass: true              # run all tests in group after changed specs pass, default: false
# cli: '--test'                     # pass arbitrary Minitest CLI arguments, default: ''
# test_folders: ['tests']           # specify an array of paths that contain test files, default: %w[test spec]
# include: ['lib']                  # specify an array of include paths to the command that runs the tests
# test_file_patterns: %w[test_*.rb] # specify an array of patterns that test files must match in order to be run, default: %w[*_test.rb test_*.rb *_spec.rb]
# spring: true                      # enable spring support, default: false
# zeus: true                        # enable zeus support; default: false
# drb: true                         # enable DRb support, default: false
# bundler: false                    # don't use "bundle exec" to run the minitest command, default: true
# rubygems: true                    # require rubygems when running the minitest command (only if bundler is disabled), default: false
# env: {}                           # specify some environment variables to be set when the test command is invoked, default: {}
# all_env: {}                       # specify additional environment variables to be set when all tests are being run, default: false
# autorun: false                    # require 'minitest/autorun' automatically, default: true
options = {
  spring: 'bin/rails test', # NOTE: true = run all tests on every run
  all_on_start: false,
  all_after_pass: false
}
guard :minitest, options do
  # with Minitest::Unit
  watch(%r{^test/(.*)\/?test_(.*)\.rb$})
  watch(%r{^test/(.*)\/.+_test\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test' }
  watch(%r{^test/.+_test\.rb$})

  # watch('test/test_helper.rb')                        { 'test' }
  watch('config/routes.rb')                           { 'test/routing' }
  watch('app/controllers/application_controller.rb')  { 'test/controllers' }
  watch(%r{^app/(.+)\.rb$})                           { |m| "test/#{m[1]}_test.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "test/#{m[1]}#{m[2]}_test.rb" }
  watch(%r{^lib/(.+)\.rb$})                           { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["test/routing/#{m[1]}_routing_test.rb", "test/#{m[2]}s/#{m[1]}_#{m[2]}_test.rb", "test/system/#{m[1]}_test.rb"] }

  # with Minitest::Spec
  watch(%r{^spec/(.*)_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})         { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
end
Ram on Rails
  • 1,299
  • 11
  • 25