rubyunit-testingminitestruby-2.2

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


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.


Solution

  • 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