Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Defined in:
- lib/test/unit/subsets.rb
Overview
These subsets facilitate testing by using the ENV variables specified on the command line to indicate which tests to run. The ENV variables are set by rake, so this code implicitly assumes that you’re running your tests through rake.
Class Method Summary collapse
-
.match_platform?(*platforms) ⇒ Boolean
Returns true if RUBY_PLATFORM matches one of the specfied platforms.
- .original_suite ⇒ Object
-
.require_platform(*platforms) ⇒ Object
Causes a whole test suite to require one of the listed platforms in order to run.
- .suite ⇒ Object
Instance Method Summary collapse
-
#acase_test(*array) ⇒ Object
Acase tests take an array of testcases.
-
#benchmark_test(length = 10, &block) ⇒ Object
Subset test declaration for benchmark tests – type: BENCHMARK Prints ‘b’ unless run.
-
#case_test(hash, &block) ⇒ Object
Case tests take a hash of testcases and expected values.
-
#extended_test(&block) ⇒ Object
Subset test declaration for extended tests – type: EXTENDED Prints ‘x’ unless run.
-
#platform_test(*platforms, &block) ⇒ Object
Platform-specific test.
-
#prompt_test(*array, &block) ⇒ Object
Subset test declaration for prompt tests – type: PROMPT Prints ‘p’ unless run.
Class Method Details
.match_platform?(*platforms) ⇒ Boolean
Returns true if RUBY_PLATFORM matches one of the specfied platforms. Use the prefix ‘non_’ to specify any plaform but the specified platform (ex: ‘non_mswin’)
Some common platforms:
- mswin
-
Windows
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/test/unit/subsets.rb', line 30 def match_platform?(*platforms) platforms.each do |platform| platform.to_s =~ /^(non_)?(.*)/ non = true if $1 match_platform = !RUBY_PLATFORM.index($2).nil? return false unless (non && !match_platform) || (!non && match_platform) end true end |
.original_suite ⇒ Object
42 |
# File 'lib/test/unit/subsets.rb', line 42 alias :original_suite :suite |
.require_platform(*platforms) ⇒ Object
Causes a whole test suite to require one of the listed platforms in order to run. See match_platform? for details.
Simply declare like:
class Test::Unit::TestCase
require_platform 'mswin'
end
20 21 22 |
# File 'lib/test/unit/subsets.rb', line 20 def require_platform(*platforms) @platforms = platforms end |
.suite ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/test/unit/subsets.rb', line 45 def self.suite if match_platform?(*@platforms) original_suite else # if platforms are specfied for the tests and the platform does NOT # match, remake the suite to skip all tests. method_names = public_instance_methods(true) suite = Test::Unit::TestSuite.new(name) method_names.each do |method_name| catch(:invalid_test) do suite << new('on_test_skipped') if method_name =~ /^test/ end end suite end end |
Instance Method Details
#acase_test(*array) ⇒ Object
Acase tests take an array of testcases. Each testcase in the array will be passed to the block if type CASE_TEST is specified. Individual cases tests can be specified by providing a regexp in CASE; the testcase will run if the pretty-print of the testcase matches the provided regexp. Example:
case_test(
[1,2,3] ,
'[1, 2, 3]',
'another testcase'
).do |testcase|
...your test code...
end
ENV['CASE_TEST']=true => all tests run
ENV['CASE']='1, 2, 3' => first two tests run
ENV['CASE']='another' => only last test runs
130 131 132 133 134 135 136 |
# File 'lib/test/unit/subsets.rb', line 130 def acase_test(*array) if match_regexp?("CASE_TEST", calling_method) array.each do |testcase| yield(testcase) if match_regexp?("CASE", testcase) end end end |
#benchmark_test(length = 10, &block) ⇒ Object
Subset test declaration for benchmark tests – type: BENCHMARK Prints ‘b’ unless run.
80 81 82 83 84 85 86 87 88 |
# File 'lib/test/unit/subsets.rb', line 80 def benchmark_test(length=10, &block) subset_test("BENCHMARK") do puts puts calling_method bm(length) do |x| yield(x) end end end |
#case_test(hash, &block) ⇒ Object
Case tests take a hash of testcases and expected values. Each pair in the hash will be passed to the block if type CASE_TEST is specified. Individual cases tests can be specified by providing a regexp in CASE; the testcase will run if the pretty-print of the testcase matches the provided regexp. Example:
case_test(
[1,2,3] => 'an array testcase',
'[1, 2, 3]' => 'the pretty print of the array',
'another testcase' => 'some third testcase'
).do |testcase, expected|
...your test code...
end
ENV['CASE_TEST']=true => all tests run
ENV['CASE']='1, 2, 3' => first two tests run
ENV['CASE']='another' => only last test runs
106 107 108 109 110 111 112 |
# File 'lib/test/unit/subsets.rb', line 106 def case_test(hash, &block) if match_regexp?("CASE_TEST", calling_method) hash.each_pair do |testcase, expected| yield(testcase, expected) if match_regexp?("CASE", testcase) end end end |
#extended_test(&block) ⇒ Object
Subset test declaration for extended tests – type: EXTENDED Prints ‘x’ unless run.
74 75 76 |
# File 'lib/test/unit/subsets.rb', line 74 def extended_test(&block) subset_test("EXTENDED", "x", &block) end |
#platform_test(*platforms, &block) ⇒ Object
Platform-specific test. Useful for specifying test that should only be run on, for instance, windows. See match_platform? for details.
64 65 66 67 68 69 70 |
# File 'lib/test/unit/subsets.rb', line 64 def platform_test(*platforms, &block) if self.class.match_platform?(*platforms) yield else print ' ' end end |
#prompt_test(*array, &block) ⇒ Object
Subset test declaration for prompt tests – type: PROMPT Prints ‘p’ unless run.
Useful for tests that require user input. If run, then this test will prompt the user for an input for each item in the array. The results are collected in a hash and passed to the block.
Example:
def test_something_important
prompt_test(:a, :b, :c).do |config|
...your test code...
end
end
(if run, prompts print to $stdout the following:)
test_something_important: Enter values or 'skip'
a: # => enter 'avalue'
b: # => enter 'bvalue'
c: # => enter 'cvalue'
# config => {:a => 'avalue', :b => 'bvalue', :c => 'cvalue'}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/test/unit/subsets.rb', line 161 def prompt_test(*array, &block) subset_test("PROMPT", "p") do puts "\n#{calling_method} -- Enter values or 'skip'." config = {} array.each do |key| print "#{key}: " value = gets.strip flunk "skipped test" if value =~ /skip/i config[key] = value end yield(config) end end |