Include module in all MiniTest tests like in RSpec
minitest does not provide a way to include
or extend
a module into every test class in the same way RSpec does.
Your best bet is going to be to re-open the test case class (differs, depending on the minitest version you're using) and include
whatever modules you want there. You probably want to do this in either your test_helper
or in a dedicated file that lets everyone else know you're monkey-patching minitest. Here are some examples:
For minitest ~> 4 (what you get with the Ruby Standard Library)
module MiniTest
class Unit
class TestCase
include MyHelpers
end
end
end
For minitest 5+module Minitest
class Test
include MyHelperz
end
end
You can then use the included methods in your test:class MyTest < Minitest::Test # or MiniTest::Unit::TestCase
def test_something
help1
# ...snip...
end
end
Hope this answers your question! Using described_class when including a module under test
If your eventual aim is to include
the current described_class
module in a newly created Class
, how about the following workaround?
RSpec.describe NewModule do
let(:test_class) do
Class.new.tap do |klass|
klass.include(described_class)
end
end
specify do
expect(test_class.ancestors).to include described_class # passes
end
end
And here's an example of including the module's methods in an object:module NewModule
def identity
itself
end
end
RSpec.describe NewModule do
let(:one) do
1.tap do |obj|
obj.class.include(described_class)
end
end
specify do
expect(one.identity).to eq 1 # passes
end
end
Note that the include
method for a Class
isn't private
in Ruby v2+. If you're using an older version, you have to use klass_name.send(:include, described_class)
Can minitest do something like rspec --color --format doc?
"How to color unit tests with lib minitest or Test:Unit? is a similar question. Several options are provided there:
- pride
- turn
purdytest
minitest-reporters
Minitest and Rspec
I'm one of the RSpec developers, and have never used minitest, so take my biases into account when reading this answer.
By and large, RSpec's power comes from the fact that it reifies so many testing concepts into first class objects. Where Test::Unit and Minitest use simple methods for making assertions, RSpec uses first-class matcher objects that support negation, self-description and more. RSpec's examples are first-class objects that support rich metadata; minitest/spec compiles it
blocks down into simple methods, which don't support the same sort of rich metadata. RSpec supports specifying shared behaviors using a first-class construct (shared example groups) that accepts arguments; w/ minitest you can use inheritance or a mixin to re-use tests, but it doesn't have the same sort of first-class support. RSpec has an explicit formatter API (and there are many third party formatters that use it); I'm not aware of minitest having the same sort of first-class formatter API.
As somebody who is constantly running tests and practicing TDD all day long, I find the power RSpec gives me to be very useful. Many people find it to be overkill, though, and there is an added cognitive cost to the extra abstractions.
Here are some specific features RSpec has that I believe minitest lacks:
before(:all)
hooks (note this is a power user feature of RSpec that should rarely be used; I've only used it on a few occasions in many years of using RSpec)around(:each)
hooks- Shared example groups
- Shared contexts
- Rich metadata support that can be used to control which examples get run, which example groups shared contexts get included in, which example groups modules get mixed into and more.
- Integrated support for a wide range of mocking features w/ rspec-mocks; Minitest::Mock is far simpler and more limited in comparison.
- RSpec has rspec-fire, which is awesome.
- It's built into the standard library so you don't need to install anything extra.
- It can either be used in
def test_blah
orit 'blah'
styles. - The code base is very small and simple. RSpec, by virtue of it's older age and additional features, is larger in comparison.
- Minitest loads faster than RSpec (it's about 4 files of code compared to RSpec having many files spread across 3 gems)--but note that RSpec is by no means slow; in most of my projects these days, I get test feedback from RSpec in under a second (and often in under 500 ms).
One last thing: if there are specific aspects of Minitest you like better, but other things you like better about RSpec, they can easily be mixed and matched. I wrote a blog post about this, if you're interested.
Related Topics
Ruby: Class C Includes Module M; Including Module N in M Does Not Affect C. What Gives
How to Scrape a Website with The Socksify Gem (Proxy)
What Is Ruby's Stringio Class Really
How Do Erlang Actors Differ from Oop Objects
Ruby/Rails 3.1: Given a Url String, Remove Path
How to Make Fish Shell Use an Rvm Ruby by Default
Error Installing Gems: Cannot Load Such File - Zlib
How to Evaluate a Block Inside a Proc
Question on Ruby Collect Method
Change Time Zone in Pure Ruby (Not Rails)
When Is The Enumerator::Yielder#Yield Method Useful
What Ruby Technique Does Rails Use to Make My Controller Methods Render Views
How to Get Meta Keywords Using Nokogiri
Ruby: How to Escape Url with Square Brackets [ and ]
Good Forms Helpers for Sinatra
Importing CSV Data into a Ruby Array/Variable
Nlp to Classify/Label The Content of a Sentence (Ruby Binding Necesarry)