METADATA 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. Metadata-Version: 2.0
  2. Name: testresources
  3. Version: 2.0.1
  4. Summary: Testresources, a pyunit extension for managing expensive test resources
  5. Home-page: https://launchpad.net/testresources
  6. Author: Testresources developers
  7. Author-email: https://launchpad.net/~testresources-developers
  8. License: UNKNOWN
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 6 - Mature
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: BSD License
  13. Classifier: License :: OSI Approved :: Apache Software License
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python
  16. Classifier: Programming Language :: Python :: 2
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Topic :: Software Development :: Quality Assurance
  19. Classifier: Topic :: Software Development :: Testing
  20. Requires-Dist: pbr (>=1.8)
  21. Provides-Extra: test
  22. Requires-Dist: docutils; extra == 'test'
  23. Requires-Dist: fixtures; extra == 'test'
  24. Requires-Dist: testtools; extra == 'test'
  25. testresources: extensions to python unittest to allow declarative use
  26. of resources by test cases.
  27. Copyright (C) 2005-2013 Robert Collins <robertc@robertcollins.net>
  28. Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
  29. license at the users choice. A copy of both licenses are available in the
  30. project source as Apache-2.0 and BSD. You may not use this file except in
  31. compliance with one of these two licences.
  32. Unless required by applicable law or agreed to in writing, software
  33. distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
  34. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  35. license you chose for the specific language governing permissions and
  36. limitations under that license.
  37. See the COPYING file for full details on the licensing of Testresources.
  38. Testresources
  39. +++++++++++++
  40. testresources extends unittest with a clean and simple api to provide test
  41. optimisation where expensive common resources are needed for test cases - for
  42. example sample working trees for VCS systems, reference databases for
  43. enterprise applications, or web servers ... let imagination run wild.
  44. Dependencies to build/selftest
  45. ==============================
  46. * Python 2.6+ (or 3.3+)
  47. * docutils
  48. * testtools (http://pypi.python.org/pypi/testtools/)
  49. * fixtures (http://pypi.python.org/pypi/fixtures)
  50. Dependencies to use testresources
  51. =================================
  52. * Python 2.6+ (or 3.3+)
  53. For older versions of Python, testresources <= 1.0.0 supported 2.4, 2.5 and
  54. 3.2.
  55. How testresources Works
  56. =======================
  57. The basic idea of testresources is:
  58. * Tests declare the resources they need in a ``resources`` attribute.
  59. * When the test is run, the required resource objects are allocated (either
  60. newly constructed, or reused), and assigned to attributes of the TestCase.
  61. testresources distinguishes a 'resource manager' (a subclass of
  62. ``TestResourceManager``) which acts as a kind of factory, and a 'resource'
  63. which can be any kind of object returned from the manager class's
  64. ``getResource`` method.
  65. Resources are either clean or dirty. Being clean means they have same state in
  66. all important ways as a newly constructed instance and they can therefore be
  67. safely reused.
  68. At this time, testresources is incompatible with setUpClass and setUpModule -
  69. when an OptimisingTestSuite is wrapped around a test suite using those
  70. features, the result will be flattened for optimisation and those setup's will
  71. not run at all.
  72. Main Classes
  73. ============
  74. testresources.ResourcedTestCase
  75. -------------------------------
  76. By extending or mixing-in this class, tests can have necessary resources
  77. automatically allocated and disposed or recycled.
  78. ResourceTestCase can be used as a base class for tests, and when that is done
  79. tests will have their ``resources`` attribute automatically checked for
  80. resources by both OptimisingTestSuite and their own setUp() and tearDown()
  81. methods. (This allows tests to remain functional without needing this specific
  82. TestSuite as a container). Alternatively, you can call setUpResources(self,
  83. resources, test_result) and tearDownResources(self, resources, test_result)
  84. from your own classes setUp and tearDown and the same behaviour will be
  85. activated.
  86. To declare the use of a resource, set the ``resources`` attribute to a list of
  87. tuples of ``(attribute_name, resource_manager)``.
  88. During setUp, for each declared requirement, the test gains an attribute
  89. pointing to an allocated resource, which is the result of calling
  90. ``resource_manager.getResource()``. ``finishedWith`` will be called on each
  91. resource during tearDown().
  92. For example::
  93. class TestLog(testresources.ResourcedTestCase):
  94. resources = [('branch', BzrPopulatedBranch())]
  95. def test_log(self):
  96. show_log(self.branch, ...)
  97. testresources.TestResourceManager
  98. ---------------------------------
  99. A TestResourceManager is an object that tests can use to create resources. It
  100. can be overridden to manage different types of resources. Normally test code
  101. doesn't need to call any methods on it, as this will be arranged by the
  102. testresources machinery.
  103. When implementing a new ``TestResourceManager`` subclass you should consider
  104. overriding these methods:
  105. ``make``
  106. Must be overridden in every concrete subclass.
  107. Returns a new instance of the resource object
  108. (the actual resource, not the TestResourceManager). Doesn't need to worry about
  109. reuse, which is taken care of separately. This method is only called when a
  110. new resource is definitely needed.
  111. ``make`` is called by ``getResource``; you should not normally need to override
  112. the latter.
  113. ``clean``
  114. Cleans up an existing resource instance, eg by deleting a directory or
  115. closing a network connection. By default this does nothing, which may be
  116. appropriate for resources that are automatically garbage collected.
  117. ``_reset``
  118. Reset a no-longer-used dirty resource to a clean state. By default this
  119. just discards it and creates a new one, but for some resources there may be a
  120. faster way to reset them.
  121. ``isDirty``
  122. Check whether an existing resource is dirty. By default this just reports
  123. whether ``TestResourceManager.dirtied`` has been called or any of the
  124. dependency resources are dirty.
  125. For instance::
  126. class TemporaryDirectoryResource(TestResourceManager):
  127. def clean(self, resource):
  128. shutil.rmtree(resource)
  129. def make(self):
  130. return tempfile.mkdtemp()
  131. def isDirty(self, resource):
  132. # Can't detect when the directory is written to, so assume it
  133. # can never be reused. We could list the directory, but that might
  134. # not catch it being open as a cwd etc.
  135. return True
  136. The ``resources`` list on the TestResourceManager object is used to declare
  137. dependencies. For instance, a DataBaseResource that needs a TemporaryDirectory
  138. might be declared with a resources list::
  139. class DataBaseResource(TestResourceManager):
  140. resources = [("scratchdir", TemporaryDirectoryResource())]
  141. Most importantly, two getResources to the same TestResourceManager with no
  142. finishedWith call in the middle, will return the same object as long as it is
  143. not dirty.
  144. When a Test has a dependency and that dependency successfully completes but
  145. returns None, the framework does *not* consider this an error: be sure to always
  146. return a valid resource, or raise an error. Error handling hasn't been heavily
  147. exercised, but any bugs in this area will be promptly dealt with.
  148. A sample TestResourceManager can be found in the doc/ folder.
  149. See pydoc testresources.TestResourceManager for details.
  150. testresources.GenericResource
  151. -----------------------------
  152. Glue to adapt testresources to an existing resource-like class.
  153. testresources.FixtureResource
  154. -----------------------------
  155. Glue to adapt testresources to the simpler fixtures.Fixture API. Long
  156. term testresources is likely to consolidate on that simpler API as the
  157. recommended method of writing resources.
  158. testresources.OptimisingTestSuite
  159. ---------------------------------
  160. This TestSuite will introspect all the test cases it holds directly and if
  161. they declare needed resources, will run the tests in an order that attempts to
  162. minimise the number of setup and tear downs required. It attempts to achieve
  163. this by callling getResource() and finishedWith() around the sequence of tests
  164. that use a specific resource.
  165. Tests are added to an OptimisingTestSuite as normal. Any standard library
  166. TestSuite objects will be flattened, while any custom TestSuite subclasses
  167. will be distributed across their member tests. This means that any custom
  168. logic in test suites should be preserved, at the price of some level of
  169. optimisation.
  170. Because the test suite does the optimisation, you can control the amount of
  171. optimising that takes place by adding more or fewer tests to a single
  172. OptimisingTestSuite. You could add everything to a single OptimisingTestSuite,
  173. getting global optimisation or you could use several smaller
  174. OptimisingTestSuites.
  175. testresources.TestLoader
  176. ------------------------
  177. This is a trivial TestLoader that creates OptimisingTestSuites by default.
  178. unittest.TestResult
  179. -------------------
  180. testresources will log activity about resource creation and destruction to the
  181. result object tests are run with. 6 extension methods are looked for:
  182. ``startCleanResource``, ``stopCleanResource``, ``startMakeResource``,
  183. ``stopMakeResource``, ``startResetResource`` and finally ``stopResetResource``.
  184. ``testresources.tests.ResultWithResourceExtensions`` is
  185. an example of a ``TestResult`` with these methods present.
  186. Controlling Resource Reuse
  187. ==========================
  188. When or how do I mark the resource dirtied?
  189. The simplest approach is to have ``TestResourceManager.make`` call ``self.dirtied``:
  190. the resource is always immediately dirty and will never be reused without first
  191. being reset. This is appropriate when the underlying resource is cheap to
  192. reset or recreate, or when it's hard to detect whether it's been dirtied or to
  193. trap operations that change it.
  194. Alternatively, override ``TestResourceManager.isDirty`` and inspect the resource to
  195. see if it is safe to reuse.
  196. Finally, you can arrange for the returned resource to always call back to
  197. ``TestResourceManager.dirtied`` on the first operation that mutates it.
  198. FAQ
  199. ===
  200. * Can I dynamically request resources inside a test method?
  201. Generally, no, you shouldn't do this. The idea is that the resources are
  202. declared statically, so that testresources can "smooth" resource usage across
  203. several tests.
  204. But, you may be able to find some object that is statically declared and reusable
  205. to act as the resource, which can then provide methods to generate sub-elements
  206. of itself during a test.
  207. * If the resource is held inside the TestResourceManager object, and the
  208. TestResourceManager is typically constructed inline in the test case
  209. ``resources`` attribute, how can they be shared across different test
  210. classes?
  211. Good question.
  212. I guess you should arrange for a single instance to be held in an appropriate
  213. module scope, then referenced by the test classes that want to share it.
  214. Releasing
  215. =========
  216. 1. Add a section to NEWS (after In Development).
  217. 2. git tag -s
  218. 3. python setup.py sdist bdist_wheel upload -s