Capturing output with UnitTest in Ruby


You have some ruby code, a rake task for example, that outputs some results in the standard output, but you would like to test it in your unit tests.
MiniTest has capture_io and capture_subprocess_io, but there is nothing similar in UnitTest.


Looking at the code of the above MiniTest assertions you can create your own assertion to be used in your tests.

Create a new file under your test folder called MyAssertions and use the following code (taken from MiniTest), for your capture_output assertion:

Then include it in your test_helper.rb file with:

and use it in your tests like: