Minitest uses Ruby classes, if a Minitest class inherits from another class, it will also inherit its methods causing Minitest to run the parent's tests twice. In some cases, we want them to run them twice, but most of the time, we don't.
Ruby Minitest Analyzer will analyze your Minitest classes and detect any duplicate test run.
This Library is explained in detail in this Post
See the following case.
require "minitest"
require "minitest/autorun"
class ProductParentTest < Minitest::Test
def test_parent
puts __method__
end
end
class ProductTest < ProductParentTest
def test_1
puts __method__
end
def test_2
puts __method__
end
end
How many tests will we run if we run the ProductTest
tests? 4! (yes, four). Try it!
# Running:
test_2
test_parent
test_1
test_parent
Finished in 0.001218s, 3284.0724 runs/s, 0.0000 assertions/s.
4 runs, 0 assertions, 0 failures, 0 errors, 0 skips
The reason is that ProductTest
is a subclass of ProductParentTest
.
Note: I added a new cop to Rubocop that checks for duplicated run tests. The cop has some limitations that this library solves. For example, in the cop, the classes should be in the same file.
More details in this Post
Other scenarios were it doesn't create duplicated tests:
When the class is under a parent Minitest class but it doesn't inherit from it.
class A < Minitest
test xyz do
end
class B < Minitest
test bar do
end
end
class C < Minitest
test foo do
end
end
end
Add this line to your Application's Gemfile:
gem 'ruby_minitest_analyzer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby_minitest_analyzer
- Create a new test
minitest_analyzer.rb
file. - Create a method
require_all_test_files
requiring all the tests files, including the files needed to run the tests e.g.test_helper.rb
. See the example below. - Call
::RubyMinitestAnalyzer.run!(nil)
. - Run the file.
minitest_analyzer.rb example:
# I placed this file within /test
require_relative 'test_helper.rb'
require 'ruby_minitest_analyzer'
def require_all_files
# require test_helpers
require_relative("test_helper")
# require tests classes
Dir[File.expand_path('**/*.rb', __dir__)].each do |f|
require_relative(f)
end
end
require_all_files
::RubyMinitestAnalyzer.run!(nil)
Running the file example:
➜ rails-minitest-analyzer git:(main) ✗ ruby minitest.rb
---------------Setting up---------------
Requiring files...
Total of 6 test classes to analyze.
---------------Setup finished! Ready to analyze the tests---------------
Analyzing!
Analyzed a total of 6 classes.
* Total duplicated tests that can be removed: 12
* Total classes with duplicated tests: 3
Classes that run the tests multiple times:
CLASS NAME | CLASS_TEST_METHODS_COUNT | CLASS_DESCENDANT_COUNT | EXTRA_TESTS_EXECUTIONS_COUNT
----------------|--------------------------|------------------------|------------------------------
GrandParentTest | 1 | 5 | 5
Parent1Test | 2 | 2 | 4
Parent2Test | 3 | 1 | 3
Finished!
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_minitest_analyzer.
The gem is available as open source under the terms of the MIT License.