Ruby unit testing - access test result (success/failure)?

970 Views Asked by At

I'm using Ruby 2.2. I need to run a unit test and get information if it succeeded or failed. I'm browsing through docs of both test-unit and minitest (suggested gems for unit testing in Ruby 2.2) but I can't seem to find a method that would return or store somewhere information about the test result.

All I need is information whether the test failed/succeeded, and I need to access it from the level of Ruby. I imagine I would have to use a specific method to run the test - so far, I was only able to run a single test by running the test file, not by invoking any method.

Maybe it's just my poor knowledge of Ruby, anyway I would appreciate any help.

2

There are 2 best solutions below

1
On BEST ANSWER

May be you can run the tests using Ruby's ability to run shell command and return results.

Here is an example for test-unit:

test_output = `ruby test.rb --runner console --verbose=progress`

failed_tests = test_output.chomp.split('').count('F')
passed_tests = test_output.chomp.split('').count('.')

puts "P: #{passed_tests}, F: #{failed_tests}"

We are using --verbose=progress option so that we get minimum output. It will look something like below:

.F...F

We count number of F to figure out how many tests failed. For about test output, the sample program will print:

P: 4, F: 2
0
On

Another option is to use passed? method: https://ruby-doc.org/stdlib-2.1.1/libdoc/minitest/unit/rdoc/MiniTest/Unit/TestCase.html#method-i-passed-3F Not sure if it's still available in the latest versions of Ruby, so please check that before using.