PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/activeldap-1.0.9/test-unit/lib/test/unit.rb

https://bitbucket.org/tommyblue/memoldan
Ruby | 342 lines | 17 code | 4 blank | 321 comment | 1 complexity | f06e798fd67e1f11b6e6c35ee1b0a9e3 MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'test/unit/testcase'
  2. require 'test/unit/autorunner'
  3. module Test # :nodoc:
  4. #
  5. # = Test::Unit - Ruby Unit Testing Framework
  6. #
  7. # == Introduction
  8. #
  9. # Unit testing is making waves all over the place, largely due to the
  10. # fact that it is a core practice of XP. While XP is great, unit testing
  11. # has been around for a long time and has always been a good idea. One
  12. # of the keys to good unit testing, though, is not just writing tests,
  13. # but having tests. What's the difference? Well, if you just _write_ a
  14. # test and throw it away, you have no guarantee that something won't
  15. # change later which breaks your code. If, on the other hand, you _have_
  16. # tests (obviously you have to write them first), and run them as often
  17. # as possible, you slowly build up a wall of things that cannot break
  18. # without you immediately knowing about it. This is when unit testing
  19. # hits its peak usefulness.
  20. #
  21. # Enter Test::Unit, a framework for unit testing in Ruby, helping you to
  22. # design, debug and evaluate your code by making it easy to write and
  23. # have tests for it.
  24. #
  25. #
  26. # == Notes
  27. #
  28. # Test::Unit has grown out of and superceded Lapidary.
  29. #
  30. #
  31. # == Feedback
  32. #
  33. # I like (and do my best to practice) XP, so I value early releases,
  34. # user feedback, and clean, simple, expressive code. There is always
  35. # room for improvement in everything I do, and Test::Unit is no
  36. # exception. Please, let me know what you think of Test::Unit as it
  37. # stands, and what you'd like to see expanded/changed/improved/etc. If
  38. # you find a bug, let me know ASAP; one good way to let me know what the
  39. # bug is is to submit a new test that catches it :-) Also, I'd love to
  40. # hear about any successes you have with Test::Unit, and any
  41. # documentation you might add will be greatly appreciated. My contact
  42. # info is below.
  43. #
  44. #
  45. # == Contact Information
  46. #
  47. # A lot of discussion happens about Ruby in general on the ruby-talk
  48. # mailing list (http://www.ruby-lang.org/en/ml.html), and you can ask
  49. # any questions you might have there. I monitor the list, as do many
  50. # other helpful Rubyists, and you're sure to get a quick answer. Of
  51. # course, you're also welcome to email me (Nathaniel Talbott) directly
  52. # at mailto:testunit@talbott.ws, and I'll do my best to help you out.
  53. #
  54. #
  55. # == Credits
  56. #
  57. # I'd like to thank...
  58. #
  59. # Matz, for a great language!
  60. #
  61. # Masaki Suketa, for his work on RubyUnit, which filled a vital need in
  62. # the Ruby world for a very long time. I'm also grateful for his help in
  63. # polishing Test::Unit and getting the RubyUnit compatibility layer
  64. # right. His graciousness in allowing Test::Unit to supercede RubyUnit
  65. # continues to be a challenge to me to be more willing to defer my own
  66. # rights.
  67. #
  68. # Ken McKinlay, for his interest and work on unit testing, and for his
  69. # willingness to dialog about it. He was also a great help in pointing
  70. # out some of the holes in the RubyUnit compatibility layer.
  71. #
  72. # Dave Thomas, for the original idea that led to the extremely simple
  73. # "require 'test/unit'", plus his code to improve it even more by
  74. # allowing the selection of tests from the command-line. Also, without
  75. # RDoc, the documentation for Test::Unit would stink a lot more than it
  76. # does now.
  77. #
  78. # Everyone who's helped out with bug reports, feature ideas,
  79. # encouragement to continue, etc. It's a real privilege to be a part of
  80. # the Ruby community.
  81. #
  82. # The guys at RoleModel Software, for putting up with me repeating, "But
  83. # this would be so much easier in Ruby!" whenever we're coding in Java.
  84. #
  85. # My Creator, for giving me life, and giving it more abundantly.
  86. #
  87. #
  88. # == License
  89. #
  90. # Test::Unit is copyright (c) 2000-2003 Nathaniel Talbott. It is free
  91. # software, and is distributed under the Ruby license. See the COPYING
  92. # file in the standard Ruby distribution for details.
  93. #
  94. #
  95. # == Warranty
  96. #
  97. # This software is provided "as is" and without any express or
  98. # implied warranties, including, without limitation, the implied
  99. # warranties of merchantibility and fitness for a particular
  100. # purpose.
  101. #
  102. #
  103. # == Author
  104. #
  105. # Nathaniel Talbott.
  106. # Copyright (c) 2000-2003, Nathaniel Talbott
  107. #
  108. # ----
  109. #
  110. # = Usage
  111. #
  112. # The general idea behind unit testing is that you write a _test_
  113. # _method_ that makes certain _assertions_ about your code, working
  114. # against a _test_ _fixture_. A bunch of these _test_ _methods_ are
  115. # bundled up into a _test_ _suite_ and can be run any time the
  116. # developer wants. The results of a run are gathered in a _test_
  117. # _result_ and displayed to the user through some UI. So, lets break
  118. # this down and see how Test::Unit provides each of these necessary
  119. # pieces.
  120. #
  121. #
  122. # == Assertions
  123. #
  124. # These are the heart of the framework. Think of an assertion as a
  125. # statement of expected outcome, i.e. "I assert that x should be equal
  126. # to y". If, when the assertion is executed, it turns out to be
  127. # correct, nothing happens, and life is good. If, on the other hand,
  128. # your assertion turns out to be false, an error is propagated with
  129. # pertinent information so that you can go back and make your
  130. # assertion succeed, and, once again, life is good. For an explanation
  131. # of the current assertions, see Test::Unit::Assertions.
  132. #
  133. #
  134. # == Test Method & Test Fixture
  135. #
  136. # Obviously, these assertions have to be called within a context that
  137. # knows about them and can do something meaningful with their
  138. # pass/fail value. Also, it's handy to collect a bunch of related
  139. # tests, each test represented by a method, into a common test class
  140. # that knows how to run them. The tests will be in a separate class
  141. # from the code they're testing for a couple of reasons. First of all,
  142. # it allows your code to stay uncluttered with test code, making it
  143. # easier to maintain. Second, it allows the tests to be stripped out
  144. # for deployment, since they're really there for you, the developer,
  145. # and your users don't need them. Third, and most importantly, it
  146. # allows you to set up a common test fixture for your tests to run
  147. # against.
  148. #
  149. # What's a test fixture? Well, tests do not live in a vacuum; rather,
  150. # they're run against the code they are testing. Often, a collection
  151. # of tests will run against a common set of data, also called a
  152. # fixture. If they're all bundled into the same test class, they can
  153. # all share the setting up and tearing down of that data, eliminating
  154. # unnecessary duplication and making it much easier to add related
  155. # tests.
  156. #
  157. # Test::Unit::TestCase wraps up a collection of test methods together
  158. # and allows you to easily set up and tear down the same test fixture
  159. # for each test. This is done by overriding #setup and/or #teardown,
  160. # which will be called before and after each test method that is
  161. # run. The TestCase also knows how to collect the results of your
  162. # assertions into a Test::Unit::TestResult, which can then be reported
  163. # back to you... but I'm getting ahead of myself. To write a test,
  164. # follow these steps:
  165. #
  166. # * Make sure Test::Unit is in your library path.
  167. # * require 'test/unit' in your test script.
  168. # * Create a class that subclasses Test::Unit::TestCase.
  169. # * Add a method that begins with "test" to your class.
  170. # * Make assertions in your test method.
  171. # * Optionally define #setup and/or #teardown to set up and/or tear
  172. # down your common test fixture.
  173. # * You can now run your test as you would any other Ruby
  174. # script... try it and see!
  175. #
  176. # A really simple test might look like this (#setup and #teardown are
  177. # commented out to indicate that they are completely optional):
  178. #
  179. # require 'test/unit'
  180. #
  181. # class TC_MyTest < Test::Unit::TestCase
  182. # # def setup
  183. # # end
  184. #
  185. # # def teardown
  186. # # end
  187. #
  188. # def test_fail
  189. # assert(false, 'Assertion was false.')
  190. # end
  191. # end
  192. #
  193. #
  194. # == Test Runners
  195. #
  196. # So, now you have this great test class, but you still need a way to
  197. # run it and view any failures that occur during the run. This is
  198. # where Test::Unit::UI::Console::TestRunner (and others, such as
  199. # Test::Unit::UI::GTK::TestRunner) comes into play. The console test
  200. # runner is automatically invoked for you if you require 'test/unit'
  201. # and simply run the file. To use another runner, or to manually
  202. # invoke a runner, simply call its run class method and pass in an
  203. # object that responds to the suite message with a
  204. # Test::Unit::TestSuite. This can be as simple as passing in your
  205. # TestCase class (which has a class suite method). It might look
  206. # something like this:
  207. #
  208. # require 'test/unit/ui/console/testrunner'
  209. # Test::Unit::UI::Console::TestRunner.run(TC_MyTest)
  210. #
  211. #
  212. # == Test Suite
  213. #
  214. # As more and more unit tests accumulate for a given project, it
  215. # becomes a real drag running them one at a time, and it also
  216. # introduces the potential to overlook a failing test because you
  217. # forget to run it. Suddenly it becomes very handy that the
  218. # TestRunners can take any object that returns a Test::Unit::TestSuite
  219. # in response to a suite method. The TestSuite can, in turn, contain
  220. # other TestSuites or individual tests (typically created by a
  221. # TestCase). In other words, you can easily wrap up a group of
  222. # TestCases and TestSuites like this:
  223. #
  224. # require 'test/unit/testsuite'
  225. # require 'tc_myfirsttests'
  226. # require 'tc_moretestsbyme'
  227. # require 'ts_anothersetoftests'
  228. #
  229. # class TS_MyTests
  230. # def self.suite
  231. # suite = Test::Unit::TestSuite.new
  232. # suite << TC_MyFirstTests.suite
  233. # suite << TC_MoreTestsByMe.suite
  234. # suite << TS_AnotherSetOfTests.suite
  235. # return suite
  236. # end
  237. # end
  238. # Test::Unit::UI::Console::TestRunner.run(TS_MyTests)
  239. #
  240. # Now, this is a bit cumbersome, so Test::Unit does a little bit more
  241. # for you, by wrapping these up automatically when you require
  242. # 'test/unit'. What does this mean? It means you could write the above
  243. # test case like this instead:
  244. #
  245. # require 'test/unit'
  246. # require 'tc_myfirsttests'
  247. # require 'tc_moretestsbyme'
  248. # require 'ts_anothersetoftests'
  249. #
  250. # Test::Unit is smart enough to find all the test cases existing in
  251. # the ObjectSpace and wrap them up into a suite for you. It then runs
  252. # the dynamic suite using the console TestRunner.
  253. #
  254. #
  255. # == Configuration file
  256. #
  257. # Test::Unit reads 'test-unit.yml' in the current working
  258. # directory as Test::Unit's configuration file. It can
  259. # contain the following configurations:
  260. #
  261. # * color scheme definitions
  262. # * test runner to be used
  263. # * test runner options
  264. # * test collector to be used
  265. #
  266. # Except color scheme definitions, all of them are
  267. # specified by command line option.
  268. #
  269. # Here are sample color scheme definitions:
  270. #
  271. # color_schemes:
  272. # inverted:
  273. # success:
  274. # name: red
  275. # bold: true
  276. # failure:
  277. # name: green
  278. # bold: true
  279. # other_scheme:
  280. # ...
  281. #
  282. # Here are the syntax of color scheme definitions:
  283. #
  284. # color_schemes:
  285. # SCHEME_NAME:
  286. # EVENT_NAME:
  287. # name: COLOR_NAME
  288. # intensity: BOOLEAN
  289. # bold: BOOLEAN
  290. # italic: BOOLEAN
  291. # underline: BOOLEAN
  292. # ...
  293. # ...
  294. #
  295. # SCHEME_NAME:: the name of the color scheme
  296. # EVENT_NAME:: one of [success, failure, pending,
  297. # omission, notification, error]
  298. # COLOR_NAME:: one of [black, red, green, yellow, blue,
  299. # magenta, cyan, white]
  300. # BOOLEAN:: true or false
  301. #
  302. # You can use the above 'inverted' color scheme with the
  303. # following configuration:
  304. #
  305. # runner: console
  306. # console_options:
  307. # color_scheme: inverted
  308. # color_schemes:
  309. # inverted:
  310. # success:
  311. # name: red
  312. # bold: true
  313. # failure:
  314. # name: green
  315. # bold: true
  316. #
  317. # == Questions?
  318. #
  319. # I'd really like to get feedback from all levels of Ruby
  320. # practitioners about typos, grammatical errors, unclear statements,
  321. # missing points, etc., in this document (or any other).
  322. #
  323. module Unit
  324. # If set to false Test::Unit will not automatically run at exit.
  325. def self.run=(flag)
  326. @run = flag
  327. end
  328. # Automatically run tests at exit?
  329. def self.run?
  330. @run ||= false
  331. end
  332. end
  333. end
  334. at_exit do
  335. unless $! || Test::Unit.run?
  336. exit Test::Unit::AutoRunner.run
  337. end
  338. end