Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more
Minimal (mostly drop-in) replacement for test-unit.
minitest provides a complete suite of testing facilities supporting TDD, BDD, mocking, and benchmarking.
"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/unit 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/unit 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/mock by Steven Baker, is a beautifully tiny mock (and stub) object framework.
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/unit 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
Comparing to rspec:
rspec is a testing DSL. minitest is ruby. -- Adam Hawkins, "Bow Before MiniTest"
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.
minitest/autorun - the easy and explicit way to run all your tests.
minitest/unit - 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.
See design_rationale.rb to see how specs and tests work in minitest.
Given that you'd like to test the following class:
classMemedefi_can_has_cheezburger?"OHAI!"enddefwill_it_blend?"YES!"endend
require'minitest/autorun'classTestMeme<MiniTest::Unit::TestCasedefsetup@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
require'minitest/autorun'describeMemedobeforedo@meme = Meme.newenddescribe"when asked about cheeseburgers"doit"must respond positively"do@meme.i_can_has_cheezburger?.must_equal"OHAI!"endenddescribe"when asked about blending possibilities"doit"won't say no"do@meme.will_it_blend?.wont_match/^no/iendendend
For matchers support check out:
github.com/zenspider/minitest-matchers
Add benchmarks to your regular unit tests. If the unit tests fail, the benchmarks won't run.
# optionally run benchmarks, good for CI-only work!require'minitest/benchmark'ifENV["BENCH"] classTestMeme<MiniTest::Unit::TestCase# Override self.bench_range or default range is [1, 10, 100, 1_000, 10_000]defbench_my_algorithmassert_performance_linear0.9999do|n|# n is a range value@obj.my_algorithm(n) endendend
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.
describeMemedoifENV["BENCH"] thenbench_performance_linear"my_algorithm", 0.9999do|n|100.timesdo@obj.my_algorithm(n) endendendend
outputs something like:
# Running benchmarks: TestBlah 100 1000 10000 bench_my_algorithm 0.006167 0.079279 0.786993 bench_other_algorithm 0.061679 0.792797 7.869932
Output is tab-delimited to make it easy to paste into a spreadsheet.
classMemeAskerdefinitialize(meme) @meme = memeenddefask(question) method = question.tr(" ","_") +"?"@meme.__send__(method) endendrequire'minitest/autorun'describeMemeAskerdobeforedo@meme = MiniTest::Mock.new@meme_asker = MemeAsker.new@memeenddescribe"#ask"dodescribe"when passed an unpunctuated question"doit"should invoke the appropriate predicate method on the meme"do@meme.expect:will_it_blend?, :return_value@meme_asker.ask"will it blend"@meme.verifyendendendend
deftest_stale_ehobj_under_test = Something.newrefuteobj_under_test.stale?Time.stub:now, Time.at(0) do# stub goes away once the block is doneassertobj_under_test.stale?endend
MiniTest::Unit.runner=(runner) provides an easy way of creating custom test runners for specialized needs. Justin Weiss provides the following real-world example to create an alternative to regular fixture loading:
classMiniTestWithHooks::Unit<MiniTest::Unitdefbefore_suitesenddefafter_suitesenddef_run_suites(suites, type) beginbefore_suitessuper(suites, type) ensureafter_suitesendenddef_run_suite(suite, type) beginsuite.before_suitesuper(suite, type) ensuresuite.after_suiteendendendmoduleMiniTestWithTransactionsclassUnit<MiniTestWithHooks::UnitincludeTestSetupHelperdefbefore_suitessupersetup_nested_transactions# load any data we want available for all testsenddefafter_suitesteardown_nested_transactionssuperendendendMiniTest::Unit.runner = MiniTestWithTransactions::Unit.new
Assertions and expectations for testing Capistrano recipes
Capybara matchers support for minitest unit and spec
Run Minitest suites as Chef report handlers
CI reporter plugin for MiniTest.
Colorize MiniTest output and show failing tests instantly.
Defines contexts for code reuse in MiniTest specs that share common expectations.
Wraps assert so failed assertions drop into the ruby debugger.
Patches MiniTest to allow for an easily configurable output.
Print out emoji for your test passes, fails, and skips.
Clean API for excluding certain tests you don't want to run under certain conditions.
Makes your MiniTest mocks more resilient.
Test notifier for minitest via growl.
Instrument ActiveSupport::Notifications when test method is executed
Store information about speed of test execution provided by minitest-instrument in database
Test notifier for minitest via libnotify.
Provides extensions to minitest for macruby UI testing.
Adds support for RSpec-style matchers to minitest.
Annotate tests with metadata (key-value).
Mongoid assertion matchers for MiniTest
Provides must_not as an alias for wont in MiniTest
Adds support for .predicate? methods
MiniTest integration for Rails 3.x
Capybara integration for MiniTest::Rails
Create customizable MiniTest output formats
redgreen minitest
Adding all manner of shoulds to MiniTest (bad idea)
Minitest::Spec extensions for Rails and beyond
add tags for minitest
Yet another test colorizer.
Get tests results as a TestResult object.
Shoulda style syntax for minitest test::unit.
minitest_tu_shim bridges between test/unit and minitest.
MiniTest matchers for Mongoid.
A pry plugin w/ minitest support. See pry-rescue/minitest.rb.
Authors… Please send me a pull request with a description of your minitest extension.
assay-minitest
capybara_minitest_spec
detroit-minitest
em-minitest-spec
flexmock-minitest
guard-minitest
guard-minitest-decisiv
minitest-activemodel
minitest-ar-assertions
minitest-around
minitest-capybara-unit
minitest-colorer
minitest-deluxe
minitest-extra-assertions
minitest-nc
minitest-rails-shoulda
minitest-spec
minitest-spec-context
minitest-spec-rails
minitest-spec-should
minitest-sugar
minitest_should
mongoid-minitest
spork-minitest
Ruby 1.8, maybe even 1.6 or lower. No magic is involved.
sudogeminstallminitest
On 1.9, you already have it. To get newer candy you can still install the gem, but you'll need to activate the gem explicitly to use it:
require'rubygems'gem'minitest'# ensures you're using the gem, and not the built in MTrequire'minitest/autorun'# ... usual testing stuffs ...
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.
(The MIT License)
Copyright © Ryan Davis, seattle.rb
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.