Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.env(type, reset = false) ⇒ Object

Access to the case-insensitive ENV variables



13
14
15
16
17
18
19
20
21
# File 'lib/test/unit/subsets.rb', line 13

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

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/test/unit/subsets.rb', line 41

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_suiteObject



53
# File 'lib/test/unit/subsets.rb', line 53

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


31
32
33
# File 'lib/test/unit/subsets.rb', line 31

def require_platform(*platforms)
	@platforms = platforms
end

.suiteObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/test/unit/subsets.rb', line 56

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


141
142
143
144
145
146
147
# File 'lib/test/unit/subsets.rb', line 141

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.



91
92
93
94
95
96
97
98
99
# File 'lib/test/unit/subsets.rb', line 91

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


117
118
119
120
121
122
123
# File 'lib/test/unit/subsets.rb', line 117

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.



85
86
87
# File 'lib/test/unit/subsets.rb', line 85

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.



75
76
77
78
79
80
81
# File 'lib/test/unit/subsets.rb', line 75

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'}


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/test/unit/subsets.rb', line 172

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