unity_test_summary.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # ==========================================
  2. # Unity Project - A Test Framework for C
  3. # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
  4. # [Released under MIT License. Please refer to license.txt for details]
  5. # ==========================================
  6. #!/usr/bin/ruby
  7. #
  8. # unity_test_summary.rb
  9. #
  10. require 'fileutils'
  11. require 'set'
  12. class UnityTestSummary
  13. include FileUtils::Verbose
  14. attr_reader :report, :total_tests, :failures, :ignored
  15. def initialize(opts = {})
  16. @report = ''
  17. @total_tests = 0
  18. @failures = 0
  19. @ignored = 0
  20. end
  21. def run
  22. # Clean up result file names
  23. results = @targets.map {|target| target.gsub(/\\/,'/')}
  24. # Dig through each result file, looking for details on pass/fail:
  25. failure_output = []
  26. ignore_output = []
  27. results.each do |result_file|
  28. lines = File.readlines(result_file).map { |line| line.chomp }
  29. if lines.length == 0
  30. raise "Empty test result file: #{result_file}"
  31. else
  32. output = get_details(result_file, lines)
  33. failure_output << output[:failures] unless output[:failures].empty?
  34. ignore_output << output[:ignores] unless output[:ignores].empty?
  35. tests,failures,ignored = parse_test_summary(lines)
  36. @total_tests += tests
  37. @failures += failures
  38. @ignored += ignored
  39. end
  40. end
  41. if @ignored > 0
  42. @report += "\n"
  43. @report += "--------------------------\n"
  44. @report += "UNITY IGNORED TEST SUMMARY\n"
  45. @report += "--------------------------\n"
  46. @report += ignore_output.flatten.join("\n")
  47. end
  48. if @failures > 0
  49. @report += "\n"
  50. @report += "--------------------------\n"
  51. @report += "UNITY FAILED TEST SUMMARY\n"
  52. @report += "--------------------------\n"
  53. @report += failure_output.flatten.join("\n")
  54. end
  55. @report += "\n"
  56. @report += "--------------------------\n"
  57. @report += "OVERALL UNITY TEST SUMMARY\n"
  58. @report += "--------------------------\n"
  59. @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
  60. @report += "\n"
  61. end
  62. def set_targets(target_array)
  63. @targets = target_array
  64. end
  65. def set_root_path(path)
  66. @root = path
  67. end
  68. def usage(err_msg=nil)
  69. puts "\nERROR: "
  70. puts err_msg if err_msg
  71. puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
  72. puts " result_file_directory - The location of your results files."
  73. puts " Defaults to current directory if not specified."
  74. puts " Should end in / if specified."
  75. puts " root_path - Helpful for producing more verbose output if using relative paths."
  76. exit 1
  77. end
  78. protected
  79. def get_details(result_file, lines)
  80. results = { :failures => [], :ignores => [], :successes => [] }
  81. lines.each do |line|
  82. src_file,src_line,test_name,status,msg = line.split(/:/)
  83. line_out = ((@root && (@root != 0)) ? "#{@root}#{line}" : line ).gsub(/\//, "\\")
  84. case(status)
  85. when 'IGNORE' then results[:ignores] << line_out
  86. when 'FAIL' then results[:failures] << line_out
  87. when 'PASS' then results[:successes] << line_out
  88. end
  89. end
  90. return results
  91. end
  92. def parse_test_summary(summary)
  93. if summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
  94. [$1.to_i,$2.to_i,$3.to_i]
  95. else
  96. raise "Couldn't parse test results: #{summary}"
  97. end
  98. end
  99. def here; File.expand_path(File.dirname(__FILE__)); end
  100. end
  101. if $0 == __FILE__
  102. #parse out the command options
  103. opts, args = ARGV.partition {|v| v =~ /^--\w+/}
  104. opts.map! {|v| v[2..-1].to_sym }
  105. #create an instance to work with
  106. uts = UnityTestSummary.new(opts)
  107. begin
  108. #look in the specified or current directory for result files
  109. args[0] ||= './'
  110. targets = "#{ARGV[0].gsub(/\\/, '/')}**/*.test*"
  111. results = Dir[targets]
  112. raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
  113. uts.set_targets(results)
  114. #set the root path
  115. args[1] ||= Dir.pwd + '/'
  116. uts.set_root_path(ARGV[1])
  117. #run the summarizer
  118. puts uts.run
  119. rescue Exception => e
  120. uts.usage e.message
  121. end
  122. end