minitest-reporters 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ require 'ansi'
2
+ require 'builder'
3
+ require 'fileutils'
4
+ module MiniTest
5
+ module Reporters
6
+ # A reporter for writing JUnit test reports
7
+ # Intended for easy integration with CI servers - tested on JetBrains TeamCity
8
+ #
9
+ # Inspired by ci_reporter (see https://github.com/nicksieger/ci_reporter)
10
+ # Also inspired by Marc Seeger's attempt at producing a JUnitReporter (see https://github.com/rb2k/minitest-reporters/commit/e13d95b5f884453a9c77f62bc5cba3fa1df30ef5)
11
+ # Also inspired by minitest-ci (see https://github.com/bhenderson/minitest-ci)
12
+ class JUnitReporter
13
+ include MiniTest::Reporter
14
+
15
+ def initialize(reports_dir = "test/reports", backtrace_filter = MiniTest::BacktraceFilter.default_filter)
16
+ @backtrace_filter = backtrace_filter
17
+ @reports_path = File.join(Dir.getwd, reports_dir)
18
+ p "Emptying #{@reports_path}"
19
+ FileUtils.remove_dir(@reports_path) if File.exists?(@reports_path)
20
+ FileUtils.mkdir_p(@reports_path)
21
+ end
22
+
23
+ def after_suites(suites, type)
24
+ p "Writing XML reports to #{@reports_path}"
25
+ runner.report.each do |suite, tests|
26
+ suite_result = analyze_suite(suite, tests)
27
+
28
+ xml = Builder::XmlMarkup.new(:indent => 2)
29
+ xml.instruct!
30
+ xml.testsuite(:name => suite, :skipped => suite_result[:skip_count], :failures => suite_result[:failure_count], :errors => suite_result[:error_count], :tests => suite_result[:test_count], :assertions => suite_result[:assertion_count]) do
31
+ tests.each do |test, test_runner|
32
+ xml.testcase(:name => test_runner.test, :classname => suite, :assertions => test_runner.assertions) do
33
+ xml << xml_message_for(test_runner) if test_runner.result != :pass
34
+ end
35
+ end
36
+ end
37
+ IO.write(filename_for(suite), xml.target!)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def xml_message_for(test_runner)
44
+ # This is a trick lifted from ci_reporter
45
+ xml = Builder::XmlMarkup.new(:indent => 2, :margin => 2)
46
+ def xml.trunc!(txt)
47
+ txt.sub(/\n.*/m, '...')
48
+ end
49
+
50
+ test = test_runner.test
51
+ e = test_runner.exception
52
+
53
+ case test_runner.result
54
+ when :skip
55
+ xml.skipped(:type => test)
56
+ when :error
57
+ xml.error(:type => test, :message => xml.trunc!(e.message)) do
58
+ xml.text!(message_for(test_runner))
59
+ end
60
+ when :failure
61
+ xml.failure(:type => test, :message => xml.trunc!(e.message)) do
62
+ xml.text!(message_for(test_runner))
63
+ end
64
+ end
65
+ end
66
+
67
+ def message_for(test_runner)
68
+ suite = test_runner.suite
69
+ test = test_runner.test
70
+ e = test_runner.exception
71
+
72
+ case test_runner.result
73
+ when :pass then nil
74
+ when :skip then "Skipped:\n#{test}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
75
+ when :failure then "Failure:\n#{test}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
76
+ when :error
77
+ bt = @backtrace_filter.filter(test_runner.exception.backtrace).join "\n "
78
+ "Error:\n#{test}(#{suite}):\n#{e.class}: #{e.message}\n #{bt}\n"
79
+ end
80
+ end
81
+
82
+
83
+ def location(exception)
84
+ last_before_assertion = ''
85
+ exception.backtrace.reverse_each do |s|
86
+ break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
87
+ last_before_assertion = s
88
+ end
89
+ last_before_assertion.sub(/:in .*$/, '')
90
+ end
91
+
92
+ def analyze_suite(suite, tests)
93
+ result = Hash.new(0)
94
+ tests.each do |test, test_runner|
95
+ result[:"#{test_runner.result}_count"] += 1
96
+ result[:assertion_count] += test_runner.assertions
97
+ result[:test_count] += 1
98
+ end
99
+ result
100
+ end
101
+
102
+ def filename_for(suite)
103
+ file_counter = 0
104
+ filename = "TEST-#{suite.to_s[0..240]}.xml" #restrict max filename length, to be kind to filesystems
105
+ while File.exists?(File.join(@reports_path, filename)) # restrict number of tries, to avoid infinite loops
106
+ file_counter += 1
107
+ filename = "TEST-#{suite}-#{file_counter}.xml"
108
+ p "Too many duplicate files, overwriting earlier report #{filename}" and break if file_counter >= 99
109
+ end
110
+ File.join(@reports_path, filename)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -1,5 +1,5 @@
1
1
  module MiniTest
2
2
  module Reporters
3
- VERSION = '0.6.0'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  end
@@ -15,5 +15,6 @@ module MiniTest
15
15
  autoload :RubyMateReporter, "minitest/reporters/ruby_mate_reporter"
16
16
  autoload :RubyMineReporter, "minitest/reporters/rubymine_reporter"
17
17
  autoload :GuardReporter, "minitest/reporters/guard_reporter"
18
+ autoload :JUnitReporter, "minitest/reporters/junit_reporter"
18
19
  end
19
20
  end
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency 'minitest', '~> 2.0'
18
18
  s.add_dependency 'ansi'
19
19
  s.add_dependency 'ruby-progressbar'
20
+ s.add_dependency 'builder'
20
21
 
21
22
  s.add_development_dependency 'mocha'
22
23
  s.add_development_dependency 'maruku'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-reporters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-06 00:00:00.000000000 Z
12
+ date: 2012-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: builder
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: mocha
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -142,6 +158,7 @@ files:
142
158
  - lib/minitest/reporters.rb
143
159
  - lib/minitest/reporters/default_reporter.rb
144
160
  - lib/minitest/reporters/guard_reporter.rb
161
+ - lib/minitest/reporters/junit_reporter.rb
145
162
  - lib/minitest/reporters/progress_reporter.rb
146
163
  - lib/minitest/reporters/ruby_mate_reporter.rb
147
164
  - lib/minitest/reporters/rubymine_reporter.rb
@@ -177,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
194
  version: '0'
178
195
  segments:
179
196
  - 0
180
- hash: -3231465579182661949
197
+ hash: 1544985441330807724
181
198
  required_rubygems_version: !ruby/object:Gem::Requirement
182
199
  none: false
183
200
  requirements:
@@ -186,10 +203,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
203
  version: '0'
187
204
  segments:
188
205
  - 0
189
- hash: -3231465579182661949
206
+ hash: 1544985441330807724
190
207
  requirements: []
191
208
  rubyforge_project: minitest-reporters
192
- rubygems_version: 1.8.21
209
+ rubygems_version: 1.8.22
193
210
  signing_key:
194
211
  specification_version: 3
195
212
  summary: Create customizable MiniTest output formats