DESCRIPTION:

"I had a class with Jim Weirich on testing last week and we were
allowed to choose our testing frameworks. Kirk Haines and I were
paired up and we cracked open the code for a few test
frameworks...
I MUST say that minitest is *very* readable / understandable
compared to the 'other two' options we looked at. Nicely done and
thank you for helping us keep our mental sanity."
-- Wayne E. Seguin

minitest/test is a small and incredibly fast unit testing framework. It
provides a rich set of assertions to make your tests clean and readable.

minitest/spec is a functionally complete spec engine. It hooks onto
minitest/test and seamlessly bridges test assertions over to spec
expectations.

minitest/benchmark is an awesome way to assert the performance of your
algorithms in a repeatable manner. Now you can assert that your newb
co-worker doesn't replace your linear algorithm with an exponential
one!

minitest/pride shows pride in testing and adds coloring to your test
output. I guess it is an example of how to write IO pipes too. :P

minitest/test is meant to have a clean implementation for language
implementors that need a minimal set of methods to bootstrap a working test
suite. For example, there is no magic involved for test-case discovery.

"Again, I can't praise enough the idea of a testing/specing
framework that I can actually read in full in one sitting!"
-- Piotr Szotkowski

minitest doesn't reinvent anything that ruby already provides, like:
classes, modules, inheritance, methods. This means you only have to learn
ruby to use minitest and all of your regular OO practices like
extract-method refactorings still apply.

FEATURES/PROBLEMS:

minitest/autorun - the easy and explicit way to run all your tests.

minitest/test - a very fast, simple, and clean test system.

minitest/spec - a very fast, simple, and clean spec system.

minitest/mock - a simple and clean mock/stub system.

minitest/benchmark - an awesome way to assert your algorithm's
performance.

minitest/pride - show your pride in testing!

Incredibly small and fast runner, but no bells and whistles.

Written by squishy human beings. Software can never be perfect. We will all
eventually die.

RATIONALE:

See design_rationale.rb to see how specs and tests work in minitest.

SYNOPSIS:

Unit tests

Define your tests as methods beginning with test_.

require"minitest/autorun"classTestMeme<Minitest::Testdefsetup@meme=Meme.newenddeftest_that_kitty_can_eatassert_equal"OHAI!",@meme.i_can_has_cheezburger?enddeftest_that_it_will_not_blendrefute_match/^no/i,@meme.will_it_blend?enddeftest_that_will_be_skippedskip"test this later"endend

Or add them to your specs. If you make benchmarks optional, you'll need
to wrap your benchmarks in a conditional since the methods won't be
defined. In minitest 5, the describe name needs to match
/Bench(mark)?$/.

Mocks

“Mocks are pre-programmed with expectations which form a specification of
the calls they are expected to receive. They can throw an exception if they
receive a call they don't expect and are checked during verification to
ensure they got all the calls they were expecting.”

classMemeAskerdefinitialize(meme)@meme=memeenddefask(question)method=question.tr("","_")+"?"@meme.__send__(method)endendrequire"minitest/autorun"describeMemeAsker,:askdodescribe"when passed an unpunctuated question"doit"should invoke the appropriate predicate method on the meme"do@meme=Minitest::Mock.new@meme_asker=MemeAsker.new@meme@meme.expect:will_it_blend?,:return_value@meme_asker.ask"will it blend"@meme.verifyendendend

**Multi-threading and Mocks**

Minitest mocks do not support multi-threading if it works, fine, if it
doesn't you can use regular ruby patterns and facilities like local
variables. Here's an example of asserting that code inside a thread is
run:

deftest_called_inside_threadcalled=falsepr=Proc.new{called=true}thread=Thread.new(&pr)thread.joinassertcalled,"proc not called"end

Writing Extensions

To define a plugin, add a file named minitest/XXX_plugin.rb to your
project/gem. That file must be discoverable via ruby's LOAD_PATH (via
rubygems or otherwise). Minitest will find and require that file using
Gem.find_files. It will then try to call plugin_XXX_init
during startup. The option processor will also try to call
plugin_XXX_options passing the OptionParser instance and the
current options hash. This lets you register your own command-line options.
Here's a totally bogus example:

Adding custom reporters

Minitest uses composite reporter to output test results using multiple
reporter instances. You can add new reporters to the composite during the
init_plugins phase. As we saw in plugin_bogus_init above, you
simply add your reporter instance to the composite via
<<.

AbstractReporter defines the API for reporters. You may
subclass it and override any method you want to achieve your desired
behavior.

Worker is a SimpleDelegate which in 1.9+ is a subclass of BasicObject.
Expectations are put on Object (one level down) so the Worker
(SimpleDelegate) hits method_missing and delegates down to the
Object.new instance. That object doesn't respond to work
so the test fails.

You can bypass SimpleDelegate#method_missing by extending the
worker with Minitest::Expectations. You can either do that in
your setup at the instance level, like:

How to share code across test classes?

Remember, describe simply creates test classes. It's just
ruby at the end of the day and all your normal Good Ruby Rules (tm) apply.
If you want to extend your test using setup/teardown via a module, just
make sure you ALWAYS call super. before/after automatically call super for
you, so make sure you don't do it twice.

How to run code before a group of tests?

Use a constant with begin…end like this:

describeBlahdoSETUP=begin# ... this runs once when describe Blah starts
end# ...
end

This can be useful for expensive initializations or sharing state.
Remember, this is just ruby code, so you need to make sure this technique
and sharing state doesn't interfere with your tests.

Why am I seeing uninitialized constant MiniTest::Test (NameError)?

Are you running the test with Bundler (e.g. via bundle exec )?
If so, in order to require minitest, you must first add the gem
'minitest' to your Gemfile and run bundle. Once
it's installed, you should be able to require minitest and run your
tests.

Prominent Projects using Minitest:

arel

journey

mime-types

nokogiri

rails (active_support et al)

rake

rdoc

…and of course, everything from seattle.rb…

Developing Minitest:

Minitest's own tests require UTF-8 external encoding.

This is a common problem in Windows, where the default external Encoding is
often CP850, but can affect any platform. Minitest can run test suites
using any Encoding, but to run Minitest's own tests you must have a
default external Encoding of UTF-8.

If your output is something other than UTF-8, you can set the RUBYOPTS env
variable to a value of '-Eutf-8'. Something like:

RUBYOPT='-Eutf-8' ruby -e 'puts Encoding.default_external'

Check your OS/shell documentation for the precise syntax (the above will
not work on a basic Windows CMD prompt, look for the SET command). Once
you've got it successfully outputing UTF-8, use the same setting when
running rake in Minitest.

Minitest's own tests require GNU (or similar) diff.

This is also a problem primarily affecting Windows developers. PowerShell
has a command called diff, but it is not suitable for use with Minitest.

If you see failures like either of these, you are probably missing diff
tool:

DO NOTE: There is a serious problem with the way that ruby 1.9/2.0 packages
their own gems. They install a gem specification file, but don't
install the gem contents in the gem path. This messes up Gem.find_files and
many other things (gem which, gem contents, etc).

Just install minitest as a gem for real and you'll be happier.

LICENSE:

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.