test-unit-runner-tap 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ require 'test/unit/ui/tap/base_testrunner'
2
+
3
+ module Test
4
+ module Unit
5
+ module UI
6
+ module Tap
7
+
8
+ # TAP-J report format.
9
+ #
10
+ class JSONTestRunner < Tap::BaseTestRunner
11
+ def initialize(suite, options={})
12
+ require 'json' unless respond_to?(:to_json)
13
+ super(suite, options)
14
+ end
15
+ def tapout_before_suite(suite)
16
+ puts super(suite).to_json
17
+ end
18
+ def tapout_before_case(testcase)
19
+ doc = super(testcase)
20
+ puts doc.to_json if doc
21
+ end
22
+ def tapout_note(note)
23
+ doc = super(note)
24
+ puts doc.to_json if doc
25
+ end
26
+ def tapout_pass(test)
27
+ doc = super(test)
28
+ puts doc.to_json if doc
29
+ end
30
+ def tapout_fail(test)
31
+ doc = super(test)
32
+ puts doc.to_json if doc
33
+ end
34
+ def tapout_omit(test)
35
+ doc = super(test)
36
+ puts doc.to_json if doc
37
+ end
38
+ def tapout_todo(test)
39
+ doc = super(test)
40
+ puts doc.to_json if doc
41
+ end
42
+ def tapout_error(test)
43
+ doc = super(test)
44
+ puts doc.to_json if doc
45
+ end
46
+ def tapout_after_suite(time)
47
+ doc = super(time)
48
+ puts doc.to_json if doc
49
+ end
50
+ end
51
+
52
+ end #module Tap
53
+ end #module UI
54
+ end #module Unit
55
+ end #module Test
@@ -0,0 +1,181 @@
1
+ require 'test/unit/ui/testrunner'
2
+ require 'test/unit/ui/testrunnermediator'
3
+ require 'test/unit/ui/tap/base_testrunner'
4
+
5
+ module Test
6
+ module Unit
7
+ module UI
8
+ module Tap
9
+
10
+ # Outputs test results in traditional TAP format, version 12.
11
+ #
12
+ class PerlTestRunner < BaseTestRunner
13
+ #
14
+ def tapout_before_suite(suite)
15
+ doc = super(suite)
16
+ @i = 0
17
+ puts "1..#{doc['count']}"
18
+ end
19
+
20
+ #
21
+ def tapout_pass(test)
22
+ doc = super(test)
23
+ if doc
24
+ @i += 1
25
+ puts "ok #{@i} - #{doc['label']}(#{@test_case.name})"
26
+ end
27
+ end
28
+
29
+ #
30
+ def tapout_fail(fault)
31
+ doc = super(fault)
32
+ if doc
33
+ @i += 1
34
+ puts "not ok #{@i} - #{doc['label']}(#{@test_case.name})"
35
+ puts subdata(doc, 'FAIL')
36
+ end
37
+ end
38
+
39
+ #
40
+ def tapout_error(fault)
41
+ doc = super(fault)
42
+ if doc
43
+ @i += 1
44
+ puts "not ok #{@i} - #{doc['label']}(#{@test_case.name})"
45
+ puts subdata(doc, 'ERROR')
46
+ end
47
+ end
48
+
49
+ #
50
+ def tapout_omit(test)
51
+ doc = super(fault)
52
+ if doc
53
+ @i += 1
54
+ puts "not ok #{@i} - #{doc['label']}(#{@test_case.name}) # SKIP"
55
+ puts subdata(doc, 'SKIP')
56
+ end
57
+ end
58
+
59
+ #
60
+ def tapout_todo(test)
61
+ doc = super(fault)
62
+ if doc
63
+ @i += 1
64
+ puts "not ok #{@i} - #{doc['label']}(#{@test_case.name}) # TODO"
65
+ puts subdata(doc, 'TODO')
66
+ end
67
+ end
68
+
69
+ #
70
+ def tapout_note(note)
71
+ doc = super(note)
72
+ puts '# ' + doc['text'].gsub("\n", "\n# ")
73
+ end
74
+
75
+ #
76
+ def tapout_after_suite(time)
77
+ puts("# Finished in #{time} seconds.")
78
+ @result.to_s.each_line do |line|
79
+ puts("# #{line}")
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ # TODO: Should this use test-unit's `fault.long_display`?
86
+
87
+ #
88
+ def subdata(doc, type)
89
+ exp = doc['exception']
90
+ exp_class = exp['class']
91
+ message = exp['message']
92
+ backtrace = exp['backtrace']
93
+ file = exp['file']
94
+ line = exp['line']
95
+
96
+ body = []
97
+ body << "%s (%s)" % [type, exp_class]
98
+ body << message.to_s
99
+
100
+ backtrace[0..0].each do |bt|
101
+ body << bt.to_s
102
+ end
103
+
104
+ code_snippet_string(file, line).each_line do |s|
105
+ body << s.chomp
106
+ end
107
+
108
+ backtrace[1..-1].each do |bt|
109
+ body << bt.to_s
110
+ end
111
+
112
+ body = body.join("\n").gsub(/^/, '# ')
113
+ end
114
+
115
+ end
116
+
117
+ # TestUnit's original TAP testrunner.
118
+ #
119
+ # We keep this runner for the time being as a fallback as the new
120
+ # code matures.
121
+ #
122
+ class OldTestRunner < UI::TestRunner
123
+ def initialize(suite, options={})
124
+ super
125
+ @output = @options[:output] || STDOUT
126
+ @n_tests = 0
127
+ @already_outputted = false
128
+ end
129
+
130
+ private
131
+ def attach_to_mediator
132
+ @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
133
+ @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
134
+ @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
135
+ @mediator.add_listener(TestCase::STARTED, &method(:test_started))
136
+ @mediator.add_listener(TestCase::FINISHED, &method(:test_finished))
137
+ end
138
+
139
+ def add_fault(fault)
140
+ puts("not ok #{@n_tests} - #{fault.short_display}")
141
+ fault.long_display.each_line do |line|
142
+ puts("# #{line}")
143
+ end
144
+ @already_outputted = true
145
+ end
146
+
147
+ def started(result)
148
+ @result = result
149
+ puts("1..#{@suite.size}")
150
+ end
151
+
152
+ def finished(elapsed_time)
153
+ puts("# Finished in #{elapsed_time} seconds.")
154
+ @result.to_s.each_line do |line|
155
+ puts("# #{line}")
156
+ end
157
+ end
158
+
159
+ def test_started(name)
160
+ @n_tests += 1
161
+ end
162
+
163
+ def test_finished(name)
164
+ unless @already_outputted
165
+ puts("ok #{@n_tests} - #{name}")
166
+ end
167
+ @already_outputted = false
168
+ end
169
+
170
+ def puts(*args)
171
+ @output.puts(*args)
172
+ @output.flush
173
+ end
174
+ end
175
+
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ # Copyright (c) 2012 Trans & Kouhei Sutou (LGPL v3.0)
@@ -0,0 +1,56 @@
1
+ require 'test/unit/ui/tap/base_testrunner'
2
+
3
+ module Test
4
+ module Unit
5
+ module UI
6
+ module Tap
7
+
8
+ # TAP-Y report format.
9
+ #
10
+ class YAMLTestRunner < Tap::BaseTestRunner
11
+ def initialize(suite, options={})
12
+ require 'yaml' unless respond_to?(:to_yaml)
13
+ super(suite, options)
14
+ end
15
+ def tapout_before_suite(suite)
16
+ puts super(suite).to_yaml
17
+ end
18
+ def tapout_before_case(testcase)
19
+ doc = super(testcase)
20
+ puts doc.to_yaml if doc
21
+ end
22
+ def tapout_note(note)
23
+ doc = super(note)
24
+ puts doc.to_yaml if doc
25
+ end
26
+ def tapout_pass(test)
27
+ doc = super(test)
28
+ puts doc.to_yaml if doc
29
+ end
30
+ def tapout_fail(test)
31
+ doc = super(test)
32
+ puts doc.to_yaml if doc
33
+ end
34
+ def tapout_omit(test)
35
+ doc = super(test)
36
+ puts doc.to_yaml if doc
37
+ end
38
+ def tapout_todo(test)
39
+ doc = super(test)
40
+ puts doc.to_yaml if doc
41
+ end
42
+ def tapout_error(test)
43
+ doc = super(test)
44
+ puts doc.to_yaml if doc
45
+ end
46
+ def tapout_after_suite(time)
47
+ doc = super(time)
48
+ puts doc.to_yaml if doc
49
+ puts "..."
50
+ end
51
+ end
52
+
53
+ end #module Tap
54
+ end #module UI
55
+ end #module Unit
56
+ end #module Test
@@ -0,0 +1,21 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ #require 'test/unit/runner/tap'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../../lib/test/unit/runner/tap')
5
+
6
+ class ExampleTestCase < Test::Unit::TestCase
7
+ def test_error
8
+ raise
9
+ end
10
+
11
+ def test_failing
12
+ assert_equal('1', '2')
13
+ end
14
+
15
+ def test_passing
16
+ sleep 1
17
+ assert_equal('1', '1')
18
+ end
19
+ end
20
+
21
+
data/test/run-test.rb ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $KCODE = "utf8" unless "".respond_to?(:encoding)
6
+
7
+ base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
8
+ lib_dir = File.join(base_dir, "lib")
9
+ test_dir = File.join(base_dir, "test")
10
+
11
+ $LOAD_PATH.unshift(lib_dir)
12
+
13
+ gem 'test-unit' # until require 'test-unit'
14
+
15
+ require 'test/unit'
16
+
17
+ #test_unit_notify_base_dir = File.join(base_dir, "..", "test-unit-notify")
18
+ #test_unit_notify_base_dir = File.expand_path(test_unit_notify_base_dir)
19
+ #if File.exist?(test_unit_notify_base_dir)
20
+ # $LOAD_PATH.unshift(File.join(test_unit_notify_base_dir, "lib"))
21
+ # require 'test/unit/notify'
22
+ #end
23
+
24
+ files = Dir[File.join(test_dir, 'test_*.rb')]
25
+
26
+ exit Test::Unit::AutoRunner.run(true, test_dir, files)
27
+
data/test/test_tap.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ # TODO: I don't think that `test-unit-runner-tap` should be showing up
4
+ # in backtraces, it should be filtered out.
5
+
3
6
  require 'stringio'
4
- require 'test/unit/ui/tap/testrunner'
7
+ require 'test/unit/ui/tap/perl_testrunner'
5
8
 
6
9
  class TestTap < Test::Unit::TestCase
7
10
  def test_run
@@ -14,24 +17,26 @@ class TestTap < Test::Unit::TestCase
14
17
  def test_fail; assert_equal(3, 1 - 2); end; fail_line = __LINE__
15
18
  end
16
19
  output = StringIO.new
17
- runner = Test::Unit::UI::Tap::TestRunner.new(test_case.suite,
18
- :output => output)
20
+ runner = Test::Unit::UI::Tap::PerlTestRunner.new(test_case.suite, :output => output)
19
21
  result = runner.start; start_line = __LINE__
20
22
  assert_equal(<<-EOR, output.string.gsub(/[\d\.]+ seconds/, "0.001 seconds"))
21
23
  1..2
22
- not ok 1 - test_fail(): <3> expected but was
23
- # Failure:
24
- # test_fail()
25
- # [#{__FILE__}:#{fail_line}:in `test_fail'
26
- # #{File.expand_path(__FILE__+'/../..')}/lib/test/unit/ui/tap/testrunner.rb:27:in `start'
27
- # #{__FILE__}:#{start_line}:in `test_run']:
24
+ not ok 1 - test_fail()
25
+ # FAIL (Test::Unit::Failure)
28
26
  # <3> expected but was
29
27
  # <-1>.
28
+ # test/test_tap.rb:17
29
+ # 15 end
30
+ # 16
31
+ # => 17 def test_fail; assert_equal(3, 1 - 2); end; fail_line = __LINE__
32
+ # 18 end
33
+ # 19 output = StringIO.new
34
+ # test/test_tap.rb:21
30
35
  ok 2 - test_success()
31
36
  # Finished in 0.001 seconds.
32
37
  # 2 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
33
38
  EOR
34
- assert_true(result.passed?)
39
+ assert_false(result.passed?) # think this is correct, where as original runner had it wrong
35
40
  end
36
41
  end
37
42
 
data/test/test_tap0.rb ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+
3
+ # TODO: I don't think that `test-unit-runner-tap` should be showing up
4
+ # in backtraces, it should be filtered out.
5
+
6
+ require 'stringio'
7
+ require 'test/unit/ui/tap/perl_testrunner'
8
+
9
+ class TestTap0 < Test::Unit::TestCase
10
+ def test_run
11
+ fail_line = nil
12
+ test_case = Class.new(Test::Unit::TestCase) do
13
+ def test_success
14
+ assert_equal(3, 1 + 2)
15
+ end
16
+
17
+ def test_fail; assert_equal(3, 1 - 2); end; fail_line = __LINE__
18
+ end
19
+ output = StringIO.new
20
+ runner = Test::Unit::UI::Tap::OldTestRunner.new(test_case.suite, :output => output)
21
+ result = runner.start; start_line = __LINE__
22
+ assert_equal(<<-EOR, output.string.gsub(/[\d\.]+ seconds/, "0.001 seconds"))
23
+ 1..2
24
+ not ok 1 - test_fail(): <3> expected but was
25
+ # Failure:
26
+ # test_fail()
27
+ # [#{__FILE__}:#{fail_line}:in `test_fail'
28
+ # #{__FILE__}:#{start_line}:in `test_run']:
29
+ # <3> expected but was
30
+ # <-1>.
31
+ ok 2 - test_success()
32
+ # Finished in 0.001 seconds.
33
+ # 2 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
34
+ EOR
35
+ assert_false(result.passed?)
36
+ end
37
+ end
38
+
data/test/test_tapy.rb ADDED
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+ require 'yaml'
3
+
4
+ class TapYTest < Test::Unit::TestCase
5
+
6
+ def initialize(*args)
7
+ super(*args)
8
+
9
+ @stream = tap_stream_using_format('tapy')
10
+
11
+ # These three documents are the unit tests, which can occur in any order.
12
+ # There is one that shoud have a status of `pass`, another of `fail` and the
13
+ # third of `error`.
14
+ @passing_test = @stream.find{ |d| d['type'] == 'test' && d['status'] == 'pass' }
15
+ @failing_test = @stream.find{ |d| d['type'] == 'test' && d['status'] == 'fail' }
16
+ @erring_test = @stream.find{ |d| d['type'] == 'test' && d['status'] == 'error' }
17
+ end
18
+
19
+ def test_there_should_be_six_sections
20
+ assert_equal 6, @stream.size
21
+ end
22
+
23
+ def test_first_document_should_be_suite
24
+ assert_equal 'suite', @stream.first['type']
25
+ assert_equal 3, @stream.first['count']
26
+ end
27
+
28
+ def test_second_document_should_be_case
29
+ assert_equal 'case', @stream[1]['type']
30
+ assert_equal 'ExampleTestCase', @stream[1]['label']
31
+ assert_equal 0, @stream[1]['level']
32
+ end
33
+
34
+ def test_passing_should_have_correct_label
35
+ assert_equal 'test_passing', @passing_test['label']
36
+ end
37
+
38
+ def test_failing_should_have_correct_label
39
+ assert_equal "test_failing", @failing_test['label']
40
+ end
41
+
42
+ def test_failing_should_hash_correct_exception
43
+ file = "test/fixtures/test_example.rb"
44
+ assert_equal "Test::Unit::Failure", @failing_test['exception']['class']
45
+ assert_equal file, @failing_test['exception']['file']
46
+ assert_equal 12, @failing_test['exception']['line']
47
+ assert_equal "assert_equal('1', '2')", @failing_test['exception']['source']
48
+ end
49
+
50
+ def test_failing_should_have_test_unit_in_backtrace
51
+ @failing_test['exception']['backtrace'].each do |e|
52
+ assert_not_match(/test\/unit/, e)
53
+ end
54
+ end
55
+
56
+ def test_erring_should_have_correct_label
57
+ assert_equal 'test_error', @erring_test['label']
58
+ end
59
+
60
+ def test_erring_should_have_correct_exception
61
+ file = "test/fixtures/test_example.rb"
62
+ assert_equal 'Test::Unit::Error', @erring_test['exception']['class']
63
+ assert_equal file, @erring_test['exception']['file']
64
+ assert_equal 8, @erring_test['exception']['line']
65
+ assert_equal 'raise', @erring_test['exception']['source']
66
+ end
67
+
68
+ def test_erring_should_not_mention_testunit_in_backtrace
69
+ @erring_test['exception']['backtrace'].each do |e|
70
+ assert_not_match(/test\/unit/, e)
71
+ end
72
+ end
73
+
74
+ def test_last_should_be_a_final_document
75
+ assert_equal 'final', @stream.last['type']
76
+ end
77
+
78
+ def test_last_should_have_prpoer_counts
79
+ assert_equal 3, @stream.last['counts']['total']
80
+ assert_equal 1, @stream.last['counts']['error']
81
+ assert_equal 1, @stream.last['counts']['fail']
82
+ assert_equal 1, @stream.last['counts']['pass']
83
+ assert_equal 0, @stream.last['counts']['omit']
84
+ assert_equal 0, @stream.last['counts']['todo']
85
+ end
86
+
87
+ private
88
+
89
+ FIXTURE = File.dirname(__FILE__) + '/fixtures'
90
+
91
+ def tap_stream_using_format(type)
92
+ command = "ruby -Ilib #{FIXTURE}/test_example.rb --runner #{type}"
93
+ output = `#{command}`
94
+
95
+ begin
96
+ #@stream = YAML.load_documents(@out) # b/c of bug in Ruby 1.8
97
+ @stream = (
98
+ s = []
99
+ load_stream(output){ |d| s << d }
100
+ s
101
+ )
102
+ rescue Exception
103
+ puts output
104
+ raise
105
+ end
106
+ end
107
+
108
+ def load_stream(output, &block)
109
+ if RUBY_VERSION < '1.9'
110
+ YAML.load_documents(output, &block)
111
+ else
112
+ YAML.load_stream(output).each(&block)
113
+ end
114
+ end
115
+
116
+ end