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