3
* http://rubyforge.org/projects/bfts
7
minitest provides a complete suite of testing facilities supporting
8
TDD, BDD, mocking, and benchmarking.
10
minitest/unit is a small and incredibly fast unit testing framework.
11
It provides a rich set of assertions to make your tests clean and
14
minitest/spec is a functionally complete spec engine. It hooks onto
15
minitest/unit and seamlessly bridges test assertions over to spec
18
minitest/benchmark is an awesome way to assert the performance of your
19
algorithms in a repeatable manner. Now you can assert that your newb
20
co-worker doesn't replace your linear algorithm with an exponential
23
minitest/mock by Steven Baker, is a beautifully tiny mock object
26
minitest/pride shows pride in testing and adds coloring to your test
29
minitest/unit is meant to have a clean implementation for language
30
implementors that need a minimal set of methods to bootstrap a working
31
test suite. For example, there is no magic involved for test-case
36
* minitest/autorun - the easy and explicit way to run all your tests.
37
* minitest/unit - a very fast, simple, and clean test system.
38
* minitest/spec - a very fast, simple, and clean spec system.
39
* minitest/mock - a simple and clean mock system.
40
* minitest/benchmark - an awesome way to assert your algorithm's performance.
41
* minitest/pride - show your pride in testing!
42
* Incredibly small and fast runner, but no bells and whistles.
46
See design_rationale.rb to see how specs and tests work in minitest.
50
Given that you'd like to test the following class:
53
def i_can_has_cheezburger?
64
require 'minitest/autorun'
66
class TestMeme < MiniTest::Unit::TestCase
71
def test_that_kitty_can_eat
72
assert_equal "OHAI!", @meme.i_can_has_cheezburger?
75
def test_that_it_will_not_blend
76
refute_match /^no/i, @meme.will_it_blend?
82
require 'minitest/autorun'
89
describe "when asked about cheeseburgers" do
90
it "must respond positively" do
91
@meme.i_can_has_cheezburger?.must_equal "OHAI!"
95
describe "when asked about blending possibilities" do
97
@meme.will_it_blend?.wont_match /^no/i
104
Add benchmarks to your regular unit tests. If the unit tests fail, the
105
benchmarks won't run.
107
# optionally run benchmarks, good for CI-only work!
108
require 'minitest/benchmark' if ENV["BENCH"]
110
class TestMeme < MiniTest::Unit::TestCase
111
# Override self.bench_range or default range is [1, 10, 100, 1_000, 10_000]
112
def bench_my_algorithm
113
assert_performance_linear 0.9999 do |n| # n is a range value
121
Or add them to your specs. If you make benchmarks optional, you'll
122
need to wrap your benchmarks in a conditional since the methods won't
127
bench_performance_linear "my_algorithm", 0.9999 do |n|
135
outputs something like:
137
# Running benchmarks:
139
TestBlah 100 1000 10000
140
bench_my_algorithm 0.006167 0.079279 0.786993
141
bench_other_algorithm 0.061679 0.792797 7.869932
143
Output is tab-delimited to make it easy to paste into a spreadsheet.
153
method = question.tr(" ","_") + "?"
158
require 'minitest/autorun'
160
describe MemeAsker do
162
@meme = MiniTest::Mock.new
163
@meme_asker = MemeAsker.new @meme
167
describe "when passed an unpunctuated question" do
168
it "should invoke the appropriate predicate method on the meme" do
169
@meme.expect :will_it_blend?, :return_value
170
@meme_asker.ask "will it blend"
177
=== Customizable Test Runner Types:
179
MiniTest::Unit.runner=(runner) provides an easy way of creating custom
180
test runners for specialized needs. Justin Weiss provides the
181
following real-world example to create an alternative to regular
184
class MiniTestWithHooks::Unit < MiniTest::Unit
191
def _run_suites(suites, type)
200
def _run_suite(suite, type)
210
module MiniTestWithTransactions
211
class Unit < MiniTestWithHooks::Unit
212
include TestSetupHelper
216
setup_nested_transactions
217
# load any data we want available for all tests
221
teardown_nested_transactions
227
MiniTest::Unit.runner = MiniTestWithTransactions::Unit.new
231
* Ruby 1.8, maybe even 1.6 or lower. No magic is involved.
235
sudo gem install minitest
237
On 1.9, you already have it. To get newer candy you can still install
238
the gem, but you'll need to activate the gem explicitly to use it:
241
gem 'minitest' # ensures you're using the gem, and not the built in MT
242
require 'minitest/autorun'
244
# ... usual testing stuffs ...
250
Copyright (c) Ryan Davis, Seattle.rb
252
Permission is hereby granted, free of charge, to any person obtaining
253
a copy of this software and associated documentation files (the
254
'Software'), to deal in the Software without restriction, including
255
without limitation the rights to use, copy, modify, merge, publish,
256
distribute, sublicense, and/or sell copies of the Software, and to
257
permit persons to whom the Software is furnished to do so, subject to
258
the following conditions:
260
The above copyright notice and this permission notice shall be
261
included in all copies or substantial portions of the Software.
263
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
264
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
265
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
266
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
267
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
268
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
269
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.