__init__.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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']
  42. # Expose obsolete functions for backwards compatibility
  43. __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
  44. __unittest = True
  45. from .result import TestResult
  46. from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
  47. skipIf, skipUnless, expectedFailure)
  48. from .suite import BaseTestSuite, TestSuite
  49. from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
  50. findTestCases)
  51. from .main import TestProgram, main
  52. from .runner import TextTestRunner, TextTestResult
  53. from .signals import installHandler, registerResult, removeResult, removeHandler
  54. # IsolatedAsyncioTestCase will be imported lazily.
  55. # deprecated
  56. _TextTestResult = TextTestResult
  57. # There are no tests here, so don't try to run anything discovered from
  58. # introspecting the symbols (e.g. FunctionTestCase). Instead, all our
  59. # tests come from within unittest.test.
  60. def load_tests(loader, tests, pattern):
  61. import os.path
  62. # top level directory cached on loader instance
  63. this_dir = os.path.dirname(__file__)
  64. return loader.discover(start_dir=this_dir, pattern=pattern)
  65. # Lazy import of IsolatedAsyncioTestCase from .async_case
  66. # It imports asyncio, which is relatively heavy, but most tests
  67. # do not need it.
  68. def __dir__():
  69. return globals().keys() | {'IsolatedAsyncioTestCase'}
  70. def __getattr__(name):
  71. if name == 'IsolatedAsyncioTestCase':
  72. global IsolatedAsyncioTestCase
  73. from .async_case import IsolatedAsyncioTestCase
  74. return IsolatedAsyncioTestCase
  75. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")