Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa13094

Browse files
committed
+ Added TestCase.exclude and load_excludes for programmatic filtering of tests.
Refactored unit tests into metametameta to share assert_report, setup, and teardown [git-p4: depot-paths = "//src/minitest/dev/": change = 6785]
1 parent 4b0fd91 commit fa13094

5 files changed

Lines changed: 190 additions & 45 deletions

File tree

Manifest.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ design_rationale.rb
77
lib/hoe/minitest.rb
88
lib/minitest/autorun.rb
99
lib/minitest/benchmark.rb
10+
lib/minitest/excludes.rb
1011
lib/minitest/mock.rb
1112
lib/minitest/pride.rb
1213
lib/minitest/spec.rb
1314
lib/minitest/unit.rb
15+
test/metametameta.rb
1416
test/test_minitest_benchmark.rb
17+
test/test_minitest_excludes.rb
1518
test/test_minitest_mock.rb
1619
test/test_minitest_spec.rb
1720
test/test_minitest_unit.rb
21+

lib/minitest/excludes.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require 'minitest/unit'
2+
3+
##
4+
# minitest/excludes.rb extends MiniTest::Unit::TestCase to provide a
5+
# clean API for excluding certain tests you don't want to run under
6+
# certain conditions.
7+
#
8+
# For example, in test/test_xyz.rb you have:
9+
#
10+
# class TestXYZ < MiniTest::Unit::TestCase
11+
# def test_good
12+
# # test that passes
13+
# end
14+
#
15+
# def test_bad
16+
# # test that fails only on jruby
17+
# end
18+
# end
19+
#
20+
# For jruby runs, you can add test/excludes/TestXYZ.rb with:
21+
#
22+
# exclude :test_bad, "Uses ObjectSpace" if jruby?
23+
#
24+
# The file is instance_eval'd on TestXYZ so you can call the exclude
25+
# class method directly. Since it is ruby you can provide any sort
26+
# of conditions you want to figure out if your tests should be
27+
# excluded.
28+
#
29+
# TestCase.exclude causes test methods to call skip with the reason
30+
# you provide. If you run your tests in verbose mode, you'll see a
31+
# full report of the tests you've excluded.
32+
#
33+
# If you want to change where the exclude files are located, you can
34+
# set the EXCLUDE_DIR environment variable.
35+
36+
class MiniTest::Unit::TestCase
37+
ENV['EXCLUDE_DIR'] ||= "test/excludes"
38+
39+
##
40+
# Exclude a test from a testcase. This is intended to be used by
41+
# exclusion files.
42+
43+
def self.exclude name, reason
44+
return warn "Method #{self}##{name} is not defined" unless
45+
method_defined? name
46+
47+
define_method name do
48+
skip reason
49+
end
50+
end
51+
52+
##
53+
# Loads the exclusion file for the class, if any.
54+
55+
def self.load_excludes
56+
@__load_excludes__ ||=
57+
begin
58+
if name and not name.empty? then
59+
file = File.join ENV['EXCLUDE_DIR'], "#{name}.rb"
60+
instance_eval File.read file if File.exist? file
61+
end
62+
true
63+
end
64+
end
65+
66+
class << self
67+
alias :old_test_methods :test_methods # :nodoc:
68+
69+
def test_methods # :nodoc:
70+
load_excludes
71+
old_test_methods
72+
end
73+
end
74+
end

test/metametameta.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'tempfile'
2+
require 'stringio'
3+
require 'minitest/autorun'
4+
5+
class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
6+
def assert_report expected = nil
7+
expected ||= <<-EOM.gsub(/^ {6}/, '')
8+
Run options: --seed 42
9+
10+
# Running tests:
11+
12+
.
13+
14+
Finished tests in 0.00
15+
16+
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
17+
EOM
18+
19+
output = @output.string.dup
20+
output.sub!(/Finished tests in .*/, "Finished tests in 0.00")
21+
output.sub!(/Loaded suite .*/, 'Loaded suite blah')
22+
output.gsub!(/(?:.\/)?test\/[^:]+:\d+/, 'FILE:LINE')
23+
output.gsub!(/\[[^\]]+\]/, '[FILE:LINE]')
24+
assert_equal(expected, output)
25+
end
26+
27+
def setup
28+
super
29+
srand 42
30+
MiniTest::Unit::TestCase.reset
31+
@tu = MiniTest::Unit.new
32+
@output = StringIO.new("")
33+
MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
34+
MiniTest::Unit.output = @output
35+
end
36+
37+
def teardown
38+
super
39+
MiniTest::Unit.output = $stdout
40+
Object.send :remove_const, :ATestCase if defined? ATestCase
41+
end
42+
end

test/test_minitest_excludes.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'test/metametameta'
2+
require 'minitest/excludes'
3+
4+
class TestMiniTestExcludes < MetaMetaMetaTestCase
5+
def test_cls_excludes
6+
srand 42
7+
old_exclude_base = ENV['EXCLUDE_DIR']
8+
9+
@assertion_count = 0
10+
11+
Dir.mktmpdir do |path|
12+
ENV['EXCLUDE_DIR'] = path
13+
File.open File.join(path, "ATestCase.rb"), "w" do |f|
14+
f.puts <<-EOM
15+
exclude :test_test2, "because it is borked"
16+
EOM
17+
end
18+
19+
tc = Class.new(MiniTest::Unit::TestCase) do
20+
def test_test1; assert true end
21+
def test_test2; assert false end # oh noes!
22+
def test_test3; assert true end
23+
end
24+
25+
Object.const_set(:ATestCase, tc)
26+
27+
assert_equal %w(test_test1 test_test2 test_test3), ATestCase.test_methods
28+
29+
@tu.run %w[--seed 42 --verbose]
30+
31+
expected = <<-EOM.gsub(/^ {8}/, '')
32+
Run options: --seed 42 --verbose
33+
34+
# Running tests:
35+
36+
ATestCase#test_test2 = 0.00 s = S
37+
ATestCase#test_test1 = 0.00 s = .
38+
ATestCase#test_test3 = 0.00 s = .
39+
40+
41+
Finished tests in 0.00
42+
43+
1) Skipped:
44+
test_test2(ATestCase) [FILE:LINE]:
45+
because it is borked
46+
47+
3 tests, 2 assertions, 0 failures, 0 errors, 1 skips
48+
EOM
49+
assert_report expected
50+
end
51+
ensure
52+
ENV['EXCLUDE_DIR'] = old_exclude_base
53+
end
54+
end

test/test_minitest_unit.rb

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
require 'stringio'
21
require 'pathname'
3-
require 'minitest/autorun'
2+
require 'test/metametameta'
43

54
module MyModule; end
65
class AnError < StandardError; include MyModule; end
76
class ImmutableString < String; def inspect; super.freeze; end; end
87

9-
class TestMiniTestUnit < MiniTest::Unit::TestCase
8+
class TestMiniTestUnit < MetaMetaMetaTestCase
109
pwd = Pathname.new(File.expand_path(Dir.pwd))
1110
basedir = Pathname.new(File.expand_path("lib/minitest")) + 'mini'
1211
basedir = basedir.relative_path_from(pwd).to_s
@@ -16,38 +15,6 @@ class TestMiniTestUnit < MiniTest::Unit::TestCase
1615
"#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
1716
"#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
1817

19-
def assert_report expected = nil
20-
expected ||= "Run options: --seed 42
21-
22-
# Running tests:
23-
24-
.
25-
26-
Finished tests in 0.00
27-
28-
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
29-
"
30-
output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00")
31-
output.sub!(/Loaded suite .*/, 'Loaded suite blah')
32-
output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:')
33-
output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]')
34-
assert_equal(expected, output)
35-
end
36-
37-
def setup
38-
srand 42
39-
MiniTest::Unit::TestCase.reset
40-
@tu = MiniTest::Unit.new
41-
@output = StringIO.new("")
42-
MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
43-
MiniTest::Unit.output = @output
44-
end
45-
46-
def teardown
47-
MiniTest::Unit.output = $stdout
48-
Object.send :remove_const, :ATestCase if defined? ATestCase
49-
end
50-
5118
def test_class_puke_with_assertion_failed
5219
exception = MiniTest::Assertion.new "Oh no!"
5320
exception.set_backtrace ["unhappy"]
@@ -229,21 +196,23 @@ def test_error
229196

230197
@tu.run %w[--seed 42]
231198

232-
expected = "Run options: --seed 42
199+
expected = <<-EOM.gsub(/^ {6}/, '')
200+
Run options: --seed 42
233201
234-
# Running tests:
202+
# Running tests:
235203
236-
E.
204+
E.
237205
238-
Finished tests in 0.00
206+
Finished tests in 0.00
239207
240-
1) Error:
241-
test_error(ATestCase):
242-
RuntimeError: unhandled exception
243-
FILE:LINE:in `test_error'
208+
1) Error:
209+
test_error(ATestCase):
210+
RuntimeError: unhandled exception
211+
FILE:LINE:in `test_error'
212+
213+
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
214+
EOM
244215

245-
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
246-
"
247216
assert_report expected
248217
end
249218

@@ -621,6 +590,8 @@ def util_expand_bt bt
621590

622591
class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
623592
def setup
593+
super
594+
624595
MiniTest::Unit::TestCase.reset
625596

626597
@tc = MiniTest::Unit::TestCase.new 'fake tc'

0 commit comments

Comments
 (0)