2

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.

charlie
  • 45
  • 6
  • You want to run the tests programmatically? I mean, normally test pass/fail is reported to either a file, the console, or both. – Dave Newton Aug 25 '15 at 18:07
  • I would prefer to access it on the level of Ruby. If that's not possible, then file - but I guess that would be in text form, like the result on console? I need to prepare statistics about the number of tests failed, I would prefer to ommit parsing such output. I don't know how to save result to the file either. – charlie Aug 25 '15 at 18:13
  • 1
    It sounds like you might want to consider an existing CI solution to avoid duplicating what they already do, but YMMV. – Dave Newton Aug 25 '15 at 18:27

2 Answers2

1

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
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
0

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.

wondersz1
  • 757
  • 8
  • 15