Why use Minitest instead of RSpec?
Although RSpec is really popular, there are a few advantages of using Minitest:
- Minitest is a standard library of Ruby since 1.9.3.
- Minitest is fast and light-weight compared to RSpec.
- Minitest provides both assertion-style and spec-style for writing test.
Minitest-ify your gem!
To add Minitest, we need to do the following:
1. Create a new folder test
The structure of the gem should be:
mygem
+ lib
+ test
+ spec
- foo_spec.rb
- test_helper.rb
- mygem.gemspec
- Rakefile
2. Add dependency to gemspec
# mygem.gemspec
# ...
Gem::Specification.new do |spec|
# ...
spec.add_development_dependency "minitest", ">= 5.8"
spec.add_development_dependency "minitest-reporters", ">= 1.1"
end
3. Add a rake task to Rakefile
# Rakefile
# ...
require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList["test/**/*_spec.rb"]
t.verbose = true
end
task :default => :test
4. Edit test_helper.rb
# test_helper.rb
require "minitest/autorun"
require "minitest/spec"
require "minitest/reporters"
require "mygem"
Minitest::Reporters.use!
5. Edit spec
# foo_spec.rb
require "test_helper"
describe Mygem do
it "does something" do
# ...
end
end
6. Run test
> rake test
That’s it!