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
-
.env(type, reset = false) ⇒ Object
Access to the case-insensitive ENV variables.
-
.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
.env(type, reset = false) ⇒ Object
Access to the case-insensitive ENV variables
14 15 16 17 18 19 20 21 22 |
# File 'lib/test/unit/subsets.rb', line 14 def env(type, reset=false) if @env_vars.nil? || reset @env_vars = {} ENV.each_pair do |key, value| @env_vars[key.downcase] = value end end @env_vars[type.downcase] end |
.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
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/test/unit/subsets.rb', line 42 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
54 |
# File 'lib/test/unit/subsets.rb', line 54 alias :original_suite :suite |
.require_platform(*platforms) ⇒ Object
32 33 34 |
# File 'lib/test/unit/subsets.rb', line 32 def require_platform(*platforms) @platforms = platforms end |
.suite ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/test/unit/subsets.rb', line 57 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
142 143 144 145 146 147 148 |
# File 'lib/test/unit/subsets.rb', line 142 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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/test/unit/subsets.rb', line 92 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
118 119 120 121 122 123 124 |
# File 'lib/test/unit/subsets.rb', line 118 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.
86 87 88 |
# File 'lib/test/unit/subsets.rb', line 86 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.
76 77 78 79 80 81 82 |
# File 'lib/test/unit/subsets.rb', line 76 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'}
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/test/unit/subsets.rb', line 173 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 |