code_junkie

In Ruby's Test::Unit::TestCase, how do I override the initialize method?

Nov 12th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. class MyTest < Test::Unit::TestCase
  2. def setup
  3. end
  4.  
  5. def test_1
  6. end
  7.  
  8. def test_1
  9. end
  10. end
  11.  
  12. require 'test/unit'
  13. require 'test/unit/ui/console/testrunner'
  14.  
  15. class TestDecorator < Test::Unit::TestSuite
  16.  
  17. def initialize(test_case_class)
  18. super
  19. self << test_case_class.suite
  20. end
  21.  
  22. def run(result, &progress_block)
  23. setup_suite
  24. begin
  25. super(result, &progress_block)
  26. ensure
  27. tear_down_suite
  28. end
  29. end
  30.  
  31. end
  32.  
  33. class MyTestCase < Test::Unit::TestCase
  34.  
  35. def test_1
  36. puts "test_1"
  37. assert_equal(1, 1)
  38. end
  39.  
  40. def test_2
  41. puts "test_2"
  42. assert_equal(2, 2)
  43. end
  44.  
  45. end
  46.  
  47. class MySuite < TestDecorator
  48.  
  49. def setup_suite
  50. puts "setup_suite"
  51. end
  52.  
  53. def tear_down_suite
  54. puts "tear_down_suite"
  55. end
  56.  
  57. end
  58.  
  59. Test::Unit::UI::Console::TestRunner.run(MySuite.new(MyTestCase))
  60.  
  61. require 'test/unit'
  62. require 'test/unit/ui/console/testrunner'
  63.  
  64. class TestDecorator < Test::Unit::TestSuite
  65.  
  66. def initialize(test_case_class)
  67. super
  68. self << test_case_class.suite
  69. end
  70.  
  71. def run(result, &progress_block)
  72. setup_suite
  73. begin
  74. super(result, &progress_block)
  75. ensure
  76. tear_down_suite
  77. end
  78. end
  79.  
  80. end
  81.  
  82. class MyTestCase < Test::Unit::TestCase
  83.  
  84. def test_1
  85. puts "test_1"
  86. assert_equal(1, 1)
  87. end
  88.  
  89. def test_2
  90. puts "test_2"
  91. assert_equal(2, 2)
  92. end
  93.  
  94. end
  95.  
  96. class MySuite < TestDecorator
  97.  
  98. def setup_suite
  99. puts "setup_suite"
  100. end
  101.  
  102. def tear_down_suite
  103. puts "tear_down_suite"
  104. end
  105.  
  106. end
  107.  
  108. class AnotherTestCase < Test::Unit::TestCase
  109.  
  110. def test_a
  111. puts "test_a"
  112. assert_equal("a", "a")
  113. end
  114.  
  115. end
  116.  
  117. class Tests
  118.  
  119. def self.suite
  120. suite = Test::Unit::TestSuite.new
  121. suite << MySuite.new(MyTestCase)
  122. suite << AnotherTestCase.suite
  123. suite
  124. end
  125.  
  126. end
  127.  
  128. Test::Unit::UI::Console::TestRunner.run(Tests.suite)
  129.  
  130. describe "Some Feature" do
  131. before :each do
  132. # this code runs once per-test
  133. end
  134.  
  135. before :all do
  136. # this code runs once before all the tests
  137. end
  138.  
  139. it "should do something useful, rather than just being called test1" do
  140. # el code here
  141. end
  142. end
  143.  
  144. class MyTest < Test::Unit::TestCase
  145. class << self
  146. def startup
  147. puts 'runs only once at start'
  148. end
  149. def shutdown
  150. puts 'runs only once at end'
  151. end
  152. end
  153.  
  154. def setup
  155. puts 'runs before each test'
  156. end
  157. def teardown
  158. puts 'runs after each test'
  159. end
  160. def test_stuff
  161. assert(true)
  162. end
  163. end
  164.  
  165. def self.suite
  166. mysuite = super
  167. def mysuite.run(*args)
  168. MyTest.startup()
  169. super
  170. MyTest.shutdown()
  171. end
  172. mysuite
  173. end
  174.  
  175. class MyTest < Test::Unit::TestCase
  176. class << self
  177. def startup
  178. puts 'runs only once at start'
  179. end
  180. def shutdown
  181. puts 'runs only once at end'
  182. end
  183. def suite
  184. mysuite = super
  185. def mysuite.run(*args)
  186. MyTest.startup()
  187. super
  188. MyTest.shutdown()
  189. end
  190. mysuite
  191. end
  192. end
  193.  
  194. def setup
  195. puts 'runs before each test'
  196. end
  197. def teardown
  198. puts 'runs after each test'
  199. end
  200. def test_stuff
  201. assert(true)
  202. end
  203. end
  204.  
  205. class ImprovedUnitTestCase < Test::Unit::TestCase
  206. cattr_accessor :expected_test_count
  207.  
  208. def self.global_setup; end
  209. def self.global_teardown; end
  210.  
  211. def teardown
  212. if((self.class.expected_test_count-=1) == 0)
  213. self.class.global_teardown
  214. end
  215. end
  216. def setup
  217. cls = self.class
  218.  
  219. if(not cls.expected_test_count)
  220. cls.expected_test_count = (cls.instance_methods.reject{|method| method[0..3] != 'test'}).length
  221. cls.global_setup
  222. end
  223. end
  224. end
  225.  
  226. class TestSomething < ImprovedUnitTestCase
  227. def self.global_setup
  228. puts 'global_setup is only run once at the beginning'
  229. end
  230.  
  231. def self.global_teardown
  232. puts 'global_teardown is only run once at the end'
  233. end
  234.  
  235. def test_1
  236. end
  237.  
  238. def test_2
  239. end
  240. end
  241.  
  242. require 'test/unit'
  243. require 'setuponce'
  244.  
  245.  
  246. class MyTest < Test::Unit::TestCase
  247. include SetupOnce
  248.  
  249. def self.setup_once
  250. puts "doing one-time setup"
  251. end
  252.  
  253. def self.teardown_once
  254. puts "doing one-time teardown"
  255. end
  256.  
  257. end
  258.  
  259. require 'mixin_class_methods' # see footnote 1
  260.  
  261. module SetupOnce
  262. mixin_class_methods
  263.  
  264. define_class_methods do
  265. def setup_once; end
  266.  
  267. def teardown_once; end
  268.  
  269. def suite
  270. mySuite = super
  271.  
  272. def mySuite.run(*args)
  273. @name.to_class.setup_once
  274. super(*args)
  275. @name.to_class.teardown_once
  276. end
  277.  
  278. return mySuite
  279. end
  280. end
  281. end
  282.  
  283. # See footnote 2
  284. class String
  285. def to_class
  286. split('::').inject(Kernel) {
  287. |scope, const_name|
  288. scope.const_get(const_name)
  289. }
  290. end
  291. end
  292.  
  293. class MyTests < Test::Unit::TestCase
  294. def test_AASetup # I have a few tests that start with "A", but I doubt any will start with "Aardvark" or "Aargh!"
  295. #Run setup code
  296. end
  297.  
  298. def MoreTests
  299. end
  300.  
  301. def test_ZTeardown
  302. #Run teardown code
  303. end
  304.  
  305. class TC_001 << Test::Unit::TestCase
  306. def setup
  307. # do stuff once
  308. end
  309.  
  310. def testSuite
  311. falseArguments()
  312. arguments()
  313. end
  314.  
  315. def falseArguments
  316. # do stuff
  317. end
  318.  
  319. def arguments
  320. # do stuff
  321. end
  322. end
Add Comment
Please, Sign In to add comment