Xirux Nefer
Xirux Nefer

Reputation: 830

Minitest: Programmatically access name and time a test took to run

At the moment, when using minitest, if you do:

bundle exec rake TESTOPTS='--verbose'

you get an output like this:

Text you typed in the describe#test_0001_test description = 0.11 s = .
Text you typed in the describe#test_0003_another test description = 0.10 s = .
...etc.

I want to have acces to this programmatically, so that I can select the slowest tests, sort them by the time they took to run, and print them out to stdout in any format I want. Ideally I would define a new Rake task for this and then run something like:

bundle exec rake mytask

or something.

However, I can't seem to find anything online on how to access this information programmatically. I searched about custom reporters, but apparently you have to monkey-patch Minitest for that, and I don't want to do that. The other option is to install the minitest-reporters gem, but I don't want nor need all that functionality, what I want to do is very simple. I've also read through the code in the Minitest repo, but couldn't wrap my head around what to inherit from if I wanted to create my own class, and what to access in order to get the time spent running and the name of the test.

Is there any way to have access to this information programmatically? For example accessing the reports produced by minitest once all tests have finished running? How do you do it, those of you who write custom reporters without requiring a gem or monkey-patching minitest? I feel like this should be an easy thing to do.

Upvotes: 0

Views: 490

Answers (1)

blowmage
blowmage

Reputation: 8984

You can write your own reporter (no need to monkey-patch, or use minitest-reporters), but there is an easier way. The verbose output is formatted in such a way that you can parse it using sort:

bundle exec rake TESTOPTS='--verbose' | sort -t = -k 2 -g

Upvotes: 1

Related Questions