PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/test-unit/lib/test/unit/testcase.rb

http://ruby-activeldap.googlecode.com/
Ruby | 539 lines | 219 code | 38 blank | 282 comment | 18 complexity | 6a4acd9ced563ce6e9246a262e4f298b MD5 | raw file
Possible License(s): GPL-2.0
  1. #--
  2. #
  3. # Author:: Nathaniel Talbott.
  4. # Copyright::
  5. # * Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
  6. # * Copyright (c) 2008-2009 Kouhei Sutou <tt><kou@clear-code.com></tt>
  7. # License:: Ruby license.
  8. require 'test/unit/attribute'
  9. require 'test/unit/fixture'
  10. require 'test/unit/exceptionhandler'
  11. require 'test/unit/assertions'
  12. require 'test/unit/failure'
  13. require 'test/unit/error'
  14. require 'test/unit/pending'
  15. require 'test/unit/omission'
  16. require 'test/unit/notification'
  17. require 'test/unit/priority'
  18. require 'test/unit/data'
  19. require 'test/unit/testsuite'
  20. require 'test/unit/testsuitecreator'
  21. require 'test/unit/assertionfailederror'
  22. require 'test/unit/util/backtracefilter'
  23. require 'test/unit/util/output'
  24. require 'test/unit/util/method-owner-finder'
  25. module Test
  26. module Unit
  27. # Ties everything together. If you subclass and add your own
  28. # test methods, it takes care of making them into tests and
  29. # wrapping those tests into a suite. It also does the
  30. # nitty-gritty of actually running an individual test and
  31. # collecting its results into a Test::Unit::TestResult object.
  32. #
  33. # You can run two hooks before/after a TestCase run.
  34. #
  35. # Example:
  36. # class TestMyClass < Test::Unit::TestCase
  37. # class << self
  38. # def startup
  39. # ...
  40. # end
  41. #
  42. # def shutdown
  43. # ...
  44. # end
  45. # end
  46. #
  47. # def setup
  48. # ...
  49. # end
  50. #
  51. # def cleanup
  52. # ...
  53. # end
  54. #
  55. # def teardown
  56. # ...
  57. # end
  58. #
  59. # def test_my_method1
  60. # ...
  61. # end
  62. #
  63. # def test_my_method2
  64. # ...
  65. # end
  66. # end
  67. #
  68. # Here is a call order:
  69. # * startup
  70. # * setup
  71. # * test_my_method1
  72. # * cleanup
  73. # * teardown
  74. # * setup
  75. # * test_my_method2
  76. # * cleanup
  77. # * teardown
  78. # * shutdown
  79. class TestCase
  80. include Attribute
  81. include Fixture
  82. include ExceptionHandler
  83. include ErrorHandler
  84. include FailureHandler
  85. include TestCasePendingSupport
  86. include TestCaseOmissionSupport
  87. include TestCaseNotificationSupport
  88. include Priority
  89. include Data
  90. include Assertions
  91. include Util::BacktraceFilter
  92. include Util::Output
  93. STARTED = name + "::STARTED" # :nodoc:
  94. FINISHED = name + "::FINISHED" # :nodoc:
  95. STARTED_OBJECT = name + "::STARTED::OBJECT" # :nodoc:
  96. FINISHED_OBJECT = name + "::FINISHED::OBJECT" # :nodoc:
  97. DESCENDANTS = [] # :nodoc:
  98. AVAILABLE_ORDERS = [:alphabetic, :random, :defined] # :nodoc:
  99. class << self
  100. def inherited(sub_class) # :nodoc:
  101. DESCENDANTS << sub_class
  102. end
  103. @@added_methods = {}
  104. def method_added(name) # :nodoc:
  105. super
  106. _added_methods = added_methods
  107. stringified_name = name.to_s
  108. if _added_methods.include?(stringified_name)
  109. attribute(:redefined, true, {}, stringified_name)
  110. end
  111. _added_methods << stringified_name
  112. end
  113. def added_methods # :nodoc:
  114. @@added_methods[self] ||= []
  115. end
  116. # Rolls up all of the test* methods in the fixture into
  117. # one suite, creating a new instance of the fixture for
  118. # each method.
  119. def suite
  120. suite_creator = TestSuiteCreator.new(self)
  121. suite_creator.create
  122. end
  123. # Called before every test case runs. Can be used
  124. # to set up fixture information used in test case
  125. # scope.
  126. #
  127. # Here is an example test case:
  128. # class TestMyClass < Test::Unit::TestCase
  129. # class << self
  130. # def startup
  131. # ...
  132. # end
  133. # end
  134. #
  135. # def setup
  136. # ...
  137. # end
  138. #
  139. # def test_my_class1
  140. # ...
  141. # end
  142. #
  143. # def test_my_class2
  144. # ...
  145. # end
  146. # end
  147. #
  148. # Here is a call order:
  149. # * startup
  150. # * setup
  151. # * test_my_class1 (or test_my_class2)
  152. # * setup
  153. # * test_my_class2 (or test_my_class1)
  154. #
  155. # Note that you should not assume test order. Tests
  156. # should be worked in any order.
  157. def startup
  158. end
  159. # Called after every test case runs. Can be used to tear
  160. # down fixture information used in test case scope.
  161. #
  162. # Here is an example test case:
  163. # class TestMyClass < Test::Unit::TestCase
  164. # class << self
  165. # def shutdown
  166. # ...
  167. # end
  168. # end
  169. #
  170. # def teardown
  171. # ...
  172. # end
  173. #
  174. # def test_my_class1
  175. # ...
  176. # end
  177. #
  178. # def test_my_class2
  179. # ...
  180. # end
  181. # end
  182. #
  183. # Here is a call order:
  184. # * test_my_class1 (or test_my_class2)
  185. # * teardown
  186. # * test_my_class2 (or test_my_class1)
  187. # * teardown
  188. # * shutdown
  189. #
  190. # Note that you should not assume test order. Tests
  191. # should be worked in any order.
  192. def shutdown
  193. end
  194. @@test_orders = {}
  195. # Returns the current test order. This returns
  196. # +:alphabetic+ by default.
  197. def test_order
  198. @@test_orders[self] || AVAILABLE_ORDERS.first
  199. end
  200. # Sets the current test order.
  201. #
  202. # Here are the available _order_:
  203. # [:alphabetic]
  204. # Default. Tests are sorted in alphabetic order.
  205. # [:random]
  206. # Tests are sorted in random order.
  207. # [:defined]
  208. # Tests are sorted in defined order.
  209. def test_order=(order)
  210. @@test_orders[self] = order
  211. end
  212. # Defines a test in declarative syntax or marks
  213. # following method as a test method.
  214. #
  215. # In declarative syntax usage, the following two
  216. # test definitions are the almost same:
  217. #
  218. # description "register user"
  219. # def test_register_user
  220. # ...
  221. # end
  222. #
  223. # test "register user" do
  224. # ...
  225. # end
  226. #
  227. # In test method mark usage, the "my_test_method" is
  228. # treated as a test method:
  229. #
  230. # test
  231. # def my_test_method
  232. # assert_equal("call me", ...)
  233. # end
  234. def test(*test_description_or_targets, &block)
  235. if block_given?
  236. test_description = test_description_or_targets.first
  237. if test_description.nil?
  238. raise ArgumentError, "test description is missing"
  239. end
  240. n_arguments = test_description_or_targets.size
  241. if n_arguments > 1
  242. message = "wrong number of arguments (#{n_arguments} for 1)"
  243. raise ArgumentError, message
  244. end
  245. method_name = test_description
  246. define_method(method_name, &block)
  247. description(test_description, method_name)
  248. attribute(:test, true, {}, method_name)
  249. else
  250. targets = test_description_or_targets
  251. attribute(:test, true, {}, *targets)
  252. end
  253. end
  254. # Describes a test.
  255. #
  256. # The following example associates "register a
  257. # normal user" description with "test_register"
  258. # test.
  259. #
  260. # description "register a normal user"
  261. # def test_register
  262. # ...
  263. # end
  264. def description(value, target=nil)
  265. attribute(:description, value, {}, target || [])
  266. end
  267. end
  268. attr_reader :method_name, :start_time, :elapsed_time
  269. # Creates a new instance of the fixture for running the
  270. # test represented by test_method_name.
  271. def initialize(test_method_name)
  272. @method_name = test_method_name
  273. @test_passed = true
  274. @interrupted = false
  275. @start_time = nil
  276. @elapsed_time = nil
  277. @test_data = nil
  278. @test_data_label = nil
  279. end
  280. def assign_test_data(label, data)
  281. @test_data_label = label
  282. @test_data = data
  283. end
  284. def valid?
  285. return false unless respond_to?(@method_name)
  286. test_method = method(@method_name)
  287. if @test_data
  288. return false unless test_method.arity == 1
  289. else
  290. return false unless test_method.arity <= 0
  291. end
  292. owner = Util::MethodOwnerFinder.find(self, @method_name)
  293. if owner.class != Module and self.class != owner
  294. return false
  295. end
  296. true
  297. end
  298. # Runs the individual test method represented by this
  299. # instance of the fixture, collecting statistics, failures
  300. # and errors in result.
  301. def run(result)
  302. begin
  303. @_result = result
  304. @start_time = Time.now
  305. yield(STARTED, name)
  306. yield(STARTED_OBJECT, self)
  307. begin
  308. run_setup
  309. run_test
  310. run_cleanup
  311. add_pass
  312. rescue Exception
  313. @interrupted = true
  314. raise unless handle_exception($!)
  315. ensure
  316. begin
  317. run_teardown
  318. rescue Exception
  319. raise unless handle_exception($!)
  320. end
  321. end
  322. @elapsed_time = Time.now - @start_time
  323. result.add_run
  324. yield(FINISHED, name)
  325. yield(FINISHED_OBJECT, self)
  326. ensure
  327. # @_result = nil # For test-spec's after_all :<
  328. end
  329. end
  330. # Called before every test method runs. Can be used
  331. # to set up fixture information.
  332. #
  333. # You can add additional setup tasks by the following
  334. # code:
  335. # class TestMyClass < Test::Unit::TestCase
  336. # def setup
  337. # ...
  338. # end
  339. #
  340. # setup
  341. # def my_setup1
  342. # ...
  343. # end
  344. #
  345. # setup
  346. # def my_setup2
  347. # ...
  348. # end
  349. #
  350. # def test_my_class
  351. # ...
  352. # end
  353. # end
  354. #
  355. # Here is a call order:
  356. # * setup
  357. # * my_setup1
  358. # * my_setup2
  359. # * test_my_class
  360. def setup
  361. end
  362. # Called after every test method runs but the test
  363. # method isn't marked as 'passed'. Can be used to
  364. # clean up and/or verify tested condition.
  365. # e.g. Can be used to verify mock.
  366. #
  367. # You can add additional cleanup tasks by the following
  368. # code:
  369. # class TestMyClass < Test::Unit::TestCase
  370. # def cleanup
  371. # ...
  372. # end
  373. #
  374. # cleanup
  375. # def my_cleanup1
  376. # ...
  377. # end
  378. #
  379. # cleanup
  380. # def my_cleanup2
  381. # ...
  382. # end
  383. #
  384. # def test_my_class
  385. # ...
  386. # end
  387. # end
  388. #
  389. # Here is a call order:
  390. # * test_my_class
  391. # * my_cleanup2
  392. # * my_cleanup1
  393. # * cleanup
  394. def cleanup
  395. end
  396. # Called after every test method runs. Can be used to tear
  397. # down fixture information.
  398. #
  399. # You can add additional teardown tasks by the following
  400. # code:
  401. # class TestMyClass < Test::Unit::TestCase
  402. # def teardown
  403. # ...
  404. # end
  405. #
  406. # teardown
  407. # def my_teardown1
  408. # ...
  409. # end
  410. #
  411. # teardown
  412. # def my_teardown2
  413. # ...
  414. # end
  415. #
  416. # def test_my_class
  417. # ...
  418. # end
  419. # end
  420. #
  421. # Here is a call order:
  422. # * test_my_class
  423. # * my_teardown2
  424. # * my_teardown1
  425. # * teardown
  426. def teardown
  427. end
  428. def default_test
  429. flunk("No tests were specified")
  430. end
  431. def size
  432. 1
  433. end
  434. # Returns a human-readable name for the specific test that
  435. # this instance of TestCase represents.
  436. def name
  437. if @test_data
  438. "#{@method_name}[#{@test_data_label}](#{self.class.name})"
  439. else
  440. "#{@method_name}(#{self.class.name})"
  441. end
  442. end
  443. # Returns a description for the test. A description
  444. # will be associated by Test::Unit::TestCase.test or
  445. # Test::Unit::TestCase.description.
  446. #
  447. # Returns a name for the test for no description test.
  448. def description
  449. self[:description] || name
  450. end
  451. # Overridden to return #name.
  452. def to_s
  453. name
  454. end
  455. # It's handy to be able to compare TestCase instances.
  456. def ==(other)
  457. return false unless(other.kind_of?(self.class))
  458. return false unless(@method_name == other.method_name)
  459. self.class == other.class
  460. end
  461. def interrupted?
  462. @interrupted
  463. end
  464. # Returns whether this individual test passed or
  465. # not. Primarily for use in teardown so that artifacts
  466. # can be left behind if the test fails.
  467. def passed?
  468. @test_passed
  469. end
  470. private
  471. def current_result
  472. @_result
  473. end
  474. def run_test
  475. if self[:redefined]
  476. notify("#{self.class}\##{@method_name} was redefined")
  477. end
  478. if @test_data
  479. __send__(@method_name, @test_data)
  480. else
  481. __send__(@method_name)
  482. end
  483. end
  484. def handle_exception(exception)
  485. self.class.exception_handlers.each do |handler|
  486. return true if send(handler, exception)
  487. end
  488. false
  489. end
  490. def problem_occurred
  491. @test_passed = false
  492. end
  493. def add_assertion
  494. current_result.add_assertion
  495. end
  496. def add_pass
  497. current_result.add_pass
  498. end
  499. end
  500. end
  501. end