__init__.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # deprecated
  58. _TextTestResult = TextTestResult
  59. # There are no tests here, so don't try to run anything discovered from
  60. # introspecting the symbols (e.g. FunctionTestCase). Instead, all our
  61. # tests come from within unittest.test.
  62. def load_tests(loader, tests, pattern):
  63. import os.path
  64. # top level directory cached on loader instance
  65. this_dir = os.path.dirname(__file__)
  66. return loader.discover(start_dir=this_dir, pattern=pattern)
  67. # Lazy import of IsolatedAsyncioTestCase from .async_case
  68. # It imports asyncio, which is relatively heavy, but most tests
  69. # do not need it.
  70. def __dir__():
  71. return globals().keys() | {'IsolatedAsyncioTestCase'}
  72. def __getattr__(name):
  73. if name == 'IsolatedAsyncioTestCase':
  74. global IsolatedAsyncioTestCase
  75. from .async_case import IsolatedAsyncioTestCase
  76. return IsolatedAsyncioTestCase
  77. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")