Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created October 8, 2009 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floehopper/204868 to your computer and use it in GitHub Desktop.
Save floehopper/204868 to your computer and use it in GitHub Desktop.
require 'stringio'
require 'test/unit/testcase'
require 'minitest/unit'
class TestResult
def self.parse_failure(raw)
matches = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m.match(raw)
return nil unless matches
Failure.new(matches[2], matches[3], [matches[4]], matches[5])
end
def self.parse_error(raw)
matches = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+)\: (.*)\n (.*)\n}m.match(raw)
return nil unless matches
Error.new(matches[2], matches[3], matches[4], matches[5], [matches[6]])
end
class Failure
attr_reader :method, :test_case, :location, :message
def initialize(method, test_case, location, message)
@method, @test_case, @location, @message = method, test_case, location, message
end
end
class Error
attr_reader :method, :test_case, :exception, :message, :location
def initialize(method, test_case, exception, message, location)
@method, @test_case, @exception, @message, @location = method, test_case, exception, message, location
end
end
def initialize(runner, test)
@runner, @test = runner, test
end
def failure_count
@runner.failures
end
def assertion_count
@test._assertions
end
def error_count
@runner.errors
end
def passed?
@test.passed?
end
def failures
@runner.report.map { |puked| TestResult.parse_failure(puked) }.compact
end
def errors
@runner.report.map { |puked| TestResult.parse_error(puked) }.compact
end
def failure_messages
failures.map(&:message)
end
def error_messages
errors.map(&:message)
end
end
test_class = Class.new(Test::Unit::TestCase) do
def test_me
# raise "three\nline\nmessage"
flunk "three\nline\nmessage"
end
end
output = StringIO.new
MiniTest::Unit.output = output
runner = MiniTest::Unit.new
test = test_class.new('test_me')
test.run(runner)
result = TestResult.new(runner, test)
p result.errors
p result.failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment