I have just watched a Railscast for Minitest.
What are the pros and cons for using RSpec vs Minitest for testing a rails app? Which features will I lose converting from RSpec to Minitest?
It's small, fast, and it aims to make tests clean and readable. Minitest is the default testing suite used with Rails, so no further setup is required to get it to work. Along with RSpec, it is one of the two most commonly used testing suites used in Ruby.
RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # file to always be loaded, without a need to explicitly require it in any files. # the additional setup, and require it from the spec files that actually need it.
Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.
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)
hooksBenefits of using Minitest:
def test_blah
or it 'blah'
styles.Overall, it's a bit like Sinatra vs. Rails, and I think Minitest and RSpec are both fine choices depending on your needs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With