From RSpec to Minitest: first impressions

Sun Jun 19, 2022
~600 Words

In testing Ruby/Rails systems I have used (and enjoyed) RSpec. In my current work RSpec is used but the principal testing framework is intended to be Minitest.

Over the years I have heard it said many times that “Minitest is just Ruby” and so it has the advantage that everything you know about organising Ruby code can be applied to your tests, with no need to learn how a DSL such as RSpec approaches this problem.

Initially what I wanted ideally was a book like Effective Testing with RSpec 3, but for Minitest, but it appears that there is no book quite like it.

Asking around, the suggestion comes that there is no particular need for such a book when it comes to Minitest, again as your tests are “just Ruby”. If you are happy with your ability to write Ruby code as part of a system, then if your tests are also “just Ruby” then it seems reasonable that you wouldn’t need any significant guidance on how to write a maintainable test suite.

So far what I’m seeing is that in practise this is mostly true–if you are an experienced Ruby developer and you have any experience at all with XUnit-style testing (in any language) then after a short while of writing and reading tests you will likely get the aha moment that you are indeed just writing Ruby.

Where this can break down slightly however is that if the tests are just Ruby, then we actually have significant latitude when it comes to how to structure our test code in a project. This variability could lead to it being harder to get up to speed in a new project that uses Minitest, if there are no particular agreed-upon patterns to adhere to.

A counterpoint to this however is that broadly it is the XUnit paradigm of testing that will be adhered to, and then as the tests are again just Ruby you can explore and come to understand them in much the same way that you would read and comprehend any Ruby-based system that is new to you.

I had also wanted a good reference book however to give to new hires who have little or no experience in Ruby and specifically Rails. Through discussing this issue with my peers it became clear that the XUnit part is not the issue (as anyone I am likely to work with will have at least had exposure to these ideas already). Instead it’s more that a reference set of examples would be useful for allowing a developer to familiarise themselves with how Ruby and Rails systems are generally tested, with a view to presenting the best practises, at least from one author’s perspective.

With that in mind, the two resources that I will recommend those new to Minitest (irrespective of background) would be firstly the Rails Guide for Testing Rails Applications as it will be kept up to date over time and shows many examples, and then for those who would prefer a book, The Minitest Cookbook by Chris Kottom looks to be the closest resource to my favoured RSpec book. I have only read the sample chapters but based on that and the table of contents I think it looks to be worth the price, particularly for those coming from non-Ruby, non-Rails backgrounds.