__init__.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
  3. Smalltalk testing framework (used with permission).
  4. This module contains the core framework classes that form the basis of
  5. specific test cases and suites (TestCase, TestSuite etc.), and also a
  6. text-based utility class for running the tests and reporting the results
  7. (TextTestRunner).
  8. Simple usage:
  9. import unittest
  10. class IntegerArithmeticTestCase(unittest.TestCase):
  11. def testAdd(self): # test method names begin with 'test'
  12. self.assertEqual((1 + 2), 3)
  13. self.assertEqual(0 + 1, 1)
  14. def testMultiply(self):
  15. self.assertEqual((0 * 10), 0)
  16. self.assertEqual((5 * 8), 40)
  17. if __name__ == '__main__':
  18. unittest.main()
  19. Further information is available in the bundled documentation, and from
  20. http://docs.python.org/library/unittest.html
  21. Copyright (c) 1999-2003 Steve Purcell
  22. Copyright (c) 2003-2010 Python Software Foundation
  23. This module is free software, and you may redistribute it and/or modify
  24. it under the same terms as Python itself, so long as this copyright message
  25. and disclaimer are retained in their original form.
  26. IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  27. SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
  28. THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  29. DAMAGE.
  30. THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  32. PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
  33. AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
  34. SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  35. """
  36. __all__ = ['TestResult', 'TestCase', 'IsolatedAsyncioTestCase', 'TestSuite',
  37. 'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
  38. 'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
  39. 'expectedFailure', 'TextTestResult', 'installHandler',
  40. 'registerResult', 'removeResult', 'removeHandler',
  41. 'addModuleCleanup', 'doModuleCleanups', 'enterModuleContext']
  42. # Expose obsolete functions for backwards compatibility
  43. # bpo-5846: Deprecated in Python 3.11, scheduled for removal in Python 3.13.
  44. __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
  45. __unittest = True
  46. from .result import TestResult
  47. from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
  48. skipIf, skipUnless, expectedFailure, doModuleCleanups,
  49. enterModuleContext)
  50. from .suite import BaseTestSuite, TestSuite
  51. from .loader import TestLoader, defaultTestLoader
  52. from .main import TestProgram, main
  53. from .runner import TextTestRunner, TextTestResult
  54. from .signals import installHandler, registerResult, removeResult, removeHandler
  55. # IsolatedAsyncioTestCase will be imported lazily.
  56. from .loader import makeSuite, getTestCaseNames, findTestCases
  57. # Lazy import of IsolatedAsyncioTestCase from .async_case
  58. # It imports asyncio, which is relatively heavy, but most tests
  59. # do not need it.
  60. def __dir__():
  61. return globals().keys() | {'IsolatedAsyncioTestCase'}
  62. def __getattr__(name):
  63. if name == 'IsolatedAsyncioTestCase':
  64. global IsolatedAsyncioTestCase
  65. from .async_case import IsolatedAsyncioTestCase
  66. return IsolatedAsyncioTestCase
  67. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")