add padding function to imgScalePadding()
This commit is contained in:
98
.CondaPkg/env/Lib/unittest/__init__.py
vendored
98
.CondaPkg/env/Lib/unittest/__init__.py
vendored
@@ -1,98 +0,0 @@
|
||||
"""
|
||||
Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
|
||||
Smalltalk testing framework (used with permission).
|
||||
|
||||
This module contains the core framework classes that form the basis of
|
||||
specific test cases and suites (TestCase, TestSuite etc.), and also a
|
||||
text-based utility class for running the tests and reporting the results
|
||||
(TextTestRunner).
|
||||
|
||||
Simple usage:
|
||||
|
||||
import unittest
|
||||
|
||||
class IntegerArithmeticTestCase(unittest.TestCase):
|
||||
def testAdd(self): # test method names begin with 'test'
|
||||
self.assertEqual((1 + 2), 3)
|
||||
self.assertEqual(0 + 1, 1)
|
||||
def testMultiply(self):
|
||||
self.assertEqual((0 * 10), 0)
|
||||
self.assertEqual((5 * 8), 40)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Further information is available in the bundled documentation, and from
|
||||
|
||||
http://docs.python.org/library/unittest.html
|
||||
|
||||
Copyright (c) 1999-2003 Steve Purcell
|
||||
Copyright (c) 2003-2010 Python Software Foundation
|
||||
This module is free software, and you may redistribute it and/or modify
|
||||
it under the same terms as Python itself, so long as this copyright message
|
||||
and disclaimer are retained in their original form.
|
||||
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
|
||||
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
|
||||
THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGE.
|
||||
|
||||
THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
||||
PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
|
||||
AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
|
||||
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
"""
|
||||
|
||||
__all__ = ['TestResult', 'TestCase', 'IsolatedAsyncioTestCase', 'TestSuite',
|
||||
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
|
||||
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
|
||||
'expectedFailure', 'TextTestResult', 'installHandler',
|
||||
'registerResult', 'removeResult', 'removeHandler',
|
||||
'addModuleCleanup', 'doModuleCleanups', 'enterModuleContext']
|
||||
|
||||
# Expose obsolete functions for backwards compatibility
|
||||
# bpo-5846: Deprecated in Python 3.11, scheduled for removal in Python 3.13.
|
||||
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
|
||||
|
||||
__unittest = True
|
||||
|
||||
from .result import TestResult
|
||||
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
|
||||
skipIf, skipUnless, expectedFailure, doModuleCleanups,
|
||||
enterModuleContext)
|
||||
from .suite import BaseTestSuite, TestSuite
|
||||
from .loader import TestLoader, defaultTestLoader
|
||||
from .main import TestProgram, main
|
||||
from .runner import TextTestRunner, TextTestResult
|
||||
from .signals import installHandler, registerResult, removeResult, removeHandler
|
||||
# IsolatedAsyncioTestCase will be imported lazily.
|
||||
from .loader import makeSuite, getTestCaseNames, findTestCases
|
||||
|
||||
# deprecated
|
||||
_TextTestResult = TextTestResult
|
||||
|
||||
|
||||
# There are no tests here, so don't try to run anything discovered from
|
||||
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
|
||||
# tests come from within unittest.test.
|
||||
def load_tests(loader, tests, pattern):
|
||||
import os.path
|
||||
# top level directory cached on loader instance
|
||||
this_dir = os.path.dirname(__file__)
|
||||
return loader.discover(start_dir=this_dir, pattern=pattern)
|
||||
|
||||
|
||||
# Lazy import of IsolatedAsyncioTestCase from .async_case
|
||||
# It imports asyncio, which is relatively heavy, but most tests
|
||||
# do not need it.
|
||||
|
||||
def __dir__():
|
||||
return globals().keys() | {'IsolatedAsyncioTestCase'}
|
||||
|
||||
def __getattr__(name):
|
||||
if name == 'IsolatedAsyncioTestCase':
|
||||
global IsolatedAsyncioTestCase
|
||||
from .async_case import IsolatedAsyncioTestCase
|
||||
return IsolatedAsyncioTestCase
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
18
.CondaPkg/env/Lib/unittest/__main__.py
vendored
18
.CondaPkg/env/Lib/unittest/__main__.py
vendored
@@ -1,18 +0,0 @@
|
||||
"""Main entry point"""
|
||||
|
||||
import sys
|
||||
if sys.argv[0].endswith("__main__.py"):
|
||||
import os.path
|
||||
# We change sys.argv[0] to make help message more useful
|
||||
# use executable without path, unquoted
|
||||
# (it's just a hint anyway)
|
||||
# (if you have spaces in your executable you get what you deserve!)
|
||||
executable = os.path.basename(sys.executable)
|
||||
sys.argv[0] = executable + " -m unittest"
|
||||
del os
|
||||
|
||||
__unittest = True
|
||||
|
||||
from .main import main
|
||||
|
||||
main(module=None)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
86
.CondaPkg/env/Lib/unittest/_log.py
vendored
86
.CondaPkg/env/Lib/unittest/_log.py
vendored
@@ -1,86 +0,0 @@
|
||||
import logging
|
||||
import collections
|
||||
|
||||
from .case import _BaseTestCaseContext
|
||||
|
||||
|
||||
_LoggingWatcher = collections.namedtuple("_LoggingWatcher",
|
||||
["records", "output"])
|
||||
|
||||
class _CapturingHandler(logging.Handler):
|
||||
"""
|
||||
A logging handler capturing all (raw and formatted) logging output.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
logging.Handler.__init__(self)
|
||||
self.watcher = _LoggingWatcher([], [])
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
|
||||
def emit(self, record):
|
||||
self.watcher.records.append(record)
|
||||
msg = self.format(record)
|
||||
self.watcher.output.append(msg)
|
||||
|
||||
|
||||
class _AssertLogsContext(_BaseTestCaseContext):
|
||||
"""A context manager for assertLogs() and assertNoLogs() """
|
||||
|
||||
LOGGING_FORMAT = "%(levelname)s:%(name)s:%(message)s"
|
||||
|
||||
def __init__(self, test_case, logger_name, level, no_logs):
|
||||
_BaseTestCaseContext.__init__(self, test_case)
|
||||
self.logger_name = logger_name
|
||||
if level:
|
||||
self.level = logging._nameToLevel.get(level, level)
|
||||
else:
|
||||
self.level = logging.INFO
|
||||
self.msg = None
|
||||
self.no_logs = no_logs
|
||||
|
||||
def __enter__(self):
|
||||
if isinstance(self.logger_name, logging.Logger):
|
||||
logger = self.logger = self.logger_name
|
||||
else:
|
||||
logger = self.logger = logging.getLogger(self.logger_name)
|
||||
formatter = logging.Formatter(self.LOGGING_FORMAT)
|
||||
handler = _CapturingHandler()
|
||||
handler.setLevel(self.level)
|
||||
handler.setFormatter(formatter)
|
||||
self.watcher = handler.watcher
|
||||
self.old_handlers = logger.handlers[:]
|
||||
self.old_level = logger.level
|
||||
self.old_propagate = logger.propagate
|
||||
logger.handlers = [handler]
|
||||
logger.setLevel(self.level)
|
||||
logger.propagate = False
|
||||
if self.no_logs:
|
||||
return
|
||||
return handler.watcher
|
||||
|
||||
def __exit__(self, exc_type, exc_value, tb):
|
||||
self.logger.handlers = self.old_handlers
|
||||
self.logger.propagate = self.old_propagate
|
||||
self.logger.setLevel(self.old_level)
|
||||
|
||||
if exc_type is not None:
|
||||
# let unexpected exceptions pass through
|
||||
return False
|
||||
|
||||
if self.no_logs:
|
||||
# assertNoLogs
|
||||
if len(self.watcher.records) > 0:
|
||||
self._raiseFailure(
|
||||
"Unexpected logs found: {!r}".format(
|
||||
self.watcher.output
|
||||
)
|
||||
)
|
||||
|
||||
else:
|
||||
# assertLogs
|
||||
if len(self.watcher.records) == 0:
|
||||
self._raiseFailure(
|
||||
"no logs of level {} or higher triggered on {}"
|
||||
.format(logging.getLevelName(self.level), self.logger.name))
|
||||
142
.CondaPkg/env/Lib/unittest/async_case.py
vendored
142
.CondaPkg/env/Lib/unittest/async_case.py
vendored
@@ -1,142 +0,0 @@
|
||||
import asyncio
|
||||
import contextvars
|
||||
import inspect
|
||||
import warnings
|
||||
|
||||
from .case import TestCase
|
||||
|
||||
|
||||
class IsolatedAsyncioTestCase(TestCase):
|
||||
# Names intentionally have a long prefix
|
||||
# to reduce a chance of clashing with user-defined attributes
|
||||
# from inherited test case
|
||||
#
|
||||
# The class doesn't call loop.run_until_complete(self.setUp()) and family
|
||||
# but uses a different approach:
|
||||
# 1. create a long-running task that reads self.setUp()
|
||||
# awaitable from queue along with a future
|
||||
# 2. await the awaitable object passing in and set the result
|
||||
# into the future object
|
||||
# 3. Outer code puts the awaitable and the future object into a queue
|
||||
# with waiting for the future
|
||||
# The trick is necessary because every run_until_complete() call
|
||||
# creates a new task with embedded ContextVar context.
|
||||
# To share contextvars between setUp(), test and tearDown() we need to execute
|
||||
# them inside the same task.
|
||||
|
||||
# Note: the test case modifies event loop policy if the policy was not instantiated
|
||||
# yet.
|
||||
# asyncio.get_event_loop_policy() creates a default policy on demand but never
|
||||
# returns None
|
||||
# I believe this is not an issue in user level tests but python itself for testing
|
||||
# should reset a policy in every test module
|
||||
# by calling asyncio.set_event_loop_policy(None) in tearDownModule()
|
||||
|
||||
def __init__(self, methodName='runTest'):
|
||||
super().__init__(methodName)
|
||||
self._asyncioRunner = None
|
||||
self._asyncioTestContext = contextvars.copy_context()
|
||||
|
||||
async def asyncSetUp(self):
|
||||
pass
|
||||
|
||||
async def asyncTearDown(self):
|
||||
pass
|
||||
|
||||
def addAsyncCleanup(self, func, /, *args, **kwargs):
|
||||
# A trivial trampoline to addCleanup()
|
||||
# the function exists because it has a different semantics
|
||||
# and signature:
|
||||
# addCleanup() accepts regular functions
|
||||
# but addAsyncCleanup() accepts coroutines
|
||||
#
|
||||
# We intentionally don't add inspect.iscoroutinefunction() check
|
||||
# for func argument because there is no way
|
||||
# to check for async function reliably:
|
||||
# 1. It can be "async def func()" itself
|
||||
# 2. Class can implement "async def __call__()" method
|
||||
# 3. Regular "def func()" that returns awaitable object
|
||||
self.addCleanup(*(func, *args), **kwargs)
|
||||
|
||||
async def enterAsyncContext(self, cm):
|
||||
"""Enters the supplied asynchronous context manager.
|
||||
|
||||
If successful, also adds its __aexit__ method as a cleanup
|
||||
function and returns the result of the __aenter__ method.
|
||||
"""
|
||||
# We look up the special methods on the type to match the with
|
||||
# statement.
|
||||
cls = type(cm)
|
||||
try:
|
||||
enter = cls.__aenter__
|
||||
exit = cls.__aexit__
|
||||
except AttributeError:
|
||||
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
|
||||
f"not support the asynchronous context manager protocol"
|
||||
) from None
|
||||
result = await enter(cm)
|
||||
self.addAsyncCleanup(exit, cm, None, None, None)
|
||||
return result
|
||||
|
||||
def _callSetUp(self):
|
||||
# Force loop to be initialized and set as the current loop
|
||||
# so that setUp functions can use get_event_loop() and get the
|
||||
# correct loop instance.
|
||||
self._asyncioRunner.get_loop()
|
||||
self._asyncioTestContext.run(self.setUp)
|
||||
self._callAsync(self.asyncSetUp)
|
||||
|
||||
def _callTestMethod(self, method):
|
||||
if self._callMaybeAsync(method) is not None:
|
||||
warnings.warn(f'It is deprecated to return a value that is not None from a '
|
||||
f'test case ({method})', DeprecationWarning, stacklevel=4)
|
||||
|
||||
def _callTearDown(self):
|
||||
self._callAsync(self.asyncTearDown)
|
||||
self._asyncioTestContext.run(self.tearDown)
|
||||
|
||||
def _callCleanup(self, function, *args, **kwargs):
|
||||
self._callMaybeAsync(function, *args, **kwargs)
|
||||
|
||||
def _callAsync(self, func, /, *args, **kwargs):
|
||||
assert self._asyncioRunner is not None, 'asyncio runner is not initialized'
|
||||
assert inspect.iscoroutinefunction(func), f'{func!r} is not an async function'
|
||||
return self._asyncioRunner.run(
|
||||
func(*args, **kwargs),
|
||||
context=self._asyncioTestContext
|
||||
)
|
||||
|
||||
def _callMaybeAsync(self, func, /, *args, **kwargs):
|
||||
assert self._asyncioRunner is not None, 'asyncio runner is not initialized'
|
||||
if inspect.iscoroutinefunction(func):
|
||||
return self._asyncioRunner.run(
|
||||
func(*args, **kwargs),
|
||||
context=self._asyncioTestContext,
|
||||
)
|
||||
else:
|
||||
return self._asyncioTestContext.run(func, *args, **kwargs)
|
||||
|
||||
def _setupAsyncioRunner(self):
|
||||
assert self._asyncioRunner is None, 'asyncio runner is already initialized'
|
||||
runner = asyncio.Runner(debug=True)
|
||||
self._asyncioRunner = runner
|
||||
|
||||
def _tearDownAsyncioRunner(self):
|
||||
runner = self._asyncioRunner
|
||||
runner.close()
|
||||
|
||||
def run(self, result=None):
|
||||
self._setupAsyncioRunner()
|
||||
try:
|
||||
return super().run(result)
|
||||
finally:
|
||||
self._tearDownAsyncioRunner()
|
||||
|
||||
def debug(self):
|
||||
self._setupAsyncioRunner()
|
||||
super().debug()
|
||||
self._tearDownAsyncioRunner()
|
||||
|
||||
def __del__(self):
|
||||
if self._asyncioRunner is not None:
|
||||
self._tearDownAsyncioRunner()
|
||||
1479
.CondaPkg/env/Lib/unittest/case.py
vendored
1479
.CondaPkg/env/Lib/unittest/case.py
vendored
File diff suppressed because it is too large
Load Diff
508
.CondaPkg/env/Lib/unittest/loader.py
vendored
508
.CondaPkg/env/Lib/unittest/loader.py
vendored
@@ -1,508 +0,0 @@
|
||||
"""Loading unittests."""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import traceback
|
||||
import types
|
||||
import functools
|
||||
import warnings
|
||||
|
||||
from fnmatch import fnmatch, fnmatchcase
|
||||
|
||||
from . import case, suite, util
|
||||
|
||||
__unittest = True
|
||||
|
||||
# what about .pyc (etc)
|
||||
# we would need to avoid loading the same tests multiple times
|
||||
# from '.py', *and* '.pyc'
|
||||
VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
|
||||
|
||||
|
||||
class _FailedTest(case.TestCase):
|
||||
_testMethodName = None
|
||||
|
||||
def __init__(self, method_name, exception):
|
||||
self._exception = exception
|
||||
super(_FailedTest, self).__init__(method_name)
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name != self._testMethodName:
|
||||
return super(_FailedTest, self).__getattr__(name)
|
||||
def testFailure():
|
||||
raise self._exception
|
||||
return testFailure
|
||||
|
||||
|
||||
def _make_failed_import_test(name, suiteClass):
|
||||
message = 'Failed to import test module: %s\n%s' % (
|
||||
name, traceback.format_exc())
|
||||
return _make_failed_test(name, ImportError(message), suiteClass, message)
|
||||
|
||||
def _make_failed_load_tests(name, exception, suiteClass):
|
||||
message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
|
||||
return _make_failed_test(
|
||||
name, exception, suiteClass, message)
|
||||
|
||||
def _make_failed_test(methodname, exception, suiteClass, message):
|
||||
test = _FailedTest(methodname, exception)
|
||||
return suiteClass((test,)), message
|
||||
|
||||
def _make_skipped_test(methodname, exception, suiteClass):
|
||||
@case.skip(str(exception))
|
||||
def testSkipped(self):
|
||||
pass
|
||||
attrs = {methodname: testSkipped}
|
||||
TestClass = type("ModuleSkipped", (case.TestCase,), attrs)
|
||||
return suiteClass((TestClass(methodname),))
|
||||
|
||||
def _jython_aware_splitext(path):
|
||||
if path.lower().endswith('$py.class'):
|
||||
return path[:-9]
|
||||
return os.path.splitext(path)[0]
|
||||
|
||||
|
||||
class TestLoader(object):
|
||||
"""
|
||||
This class is responsible for loading tests according to various criteria
|
||||
and returning them wrapped in a TestSuite
|
||||
"""
|
||||
testMethodPrefix = 'test'
|
||||
sortTestMethodsUsing = staticmethod(util.three_way_cmp)
|
||||
testNamePatterns = None
|
||||
suiteClass = suite.TestSuite
|
||||
_top_level_dir = None
|
||||
|
||||
def __init__(self):
|
||||
super(TestLoader, self).__init__()
|
||||
self.errors = []
|
||||
# Tracks packages which we have called into via load_tests, to
|
||||
# avoid infinite re-entrancy.
|
||||
self._loading_packages = set()
|
||||
|
||||
def loadTestsFromTestCase(self, testCaseClass):
|
||||
"""Return a suite of all test cases contained in testCaseClass"""
|
||||
if issubclass(testCaseClass, suite.TestSuite):
|
||||
raise TypeError("Test cases should not be derived from "
|
||||
"TestSuite. Maybe you meant to derive from "
|
||||
"TestCase?")
|
||||
testCaseNames = self.getTestCaseNames(testCaseClass)
|
||||
if not testCaseNames and hasattr(testCaseClass, 'runTest'):
|
||||
testCaseNames = ['runTest']
|
||||
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
|
||||
return loaded_suite
|
||||
|
||||
# XXX After Python 3.5, remove backward compatibility hacks for
|
||||
# use_load_tests deprecation via *args and **kws. See issue 16662.
|
||||
def loadTestsFromModule(self, module, *args, pattern=None, **kws):
|
||||
"""Return a suite of all test cases contained in the given module"""
|
||||
# This method used to take an undocumented and unofficial
|
||||
# use_load_tests argument. For backward compatibility, we still
|
||||
# accept the argument (which can also be the first position) but we
|
||||
# ignore it and issue a deprecation warning if it's present.
|
||||
if len(args) > 0 or 'use_load_tests' in kws:
|
||||
warnings.warn('use_load_tests is deprecated and ignored',
|
||||
DeprecationWarning)
|
||||
kws.pop('use_load_tests', None)
|
||||
if len(args) > 1:
|
||||
# Complain about the number of arguments, but don't forget the
|
||||
# required `module` argument.
|
||||
complaint = len(args) + 1
|
||||
raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
|
||||
if len(kws) != 0:
|
||||
# Since the keyword arguments are unsorted (see PEP 468), just
|
||||
# pick the alphabetically sorted first argument to complain about,
|
||||
# if multiple were given. At least the error message will be
|
||||
# predictable.
|
||||
complaint = sorted(kws)[0]
|
||||
raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
|
||||
tests = []
|
||||
for name in dir(module):
|
||||
obj = getattr(module, name)
|
||||
if isinstance(obj, type) and issubclass(obj, case.TestCase):
|
||||
tests.append(self.loadTestsFromTestCase(obj))
|
||||
|
||||
load_tests = getattr(module, 'load_tests', None)
|
||||
tests = self.suiteClass(tests)
|
||||
if load_tests is not None:
|
||||
try:
|
||||
return load_tests(self, tests, pattern)
|
||||
except Exception as e:
|
||||
error_case, error_message = _make_failed_load_tests(
|
||||
module.__name__, e, self.suiteClass)
|
||||
self.errors.append(error_message)
|
||||
return error_case
|
||||
return tests
|
||||
|
||||
def loadTestsFromName(self, name, module=None):
|
||||
"""Return a suite of all test cases given a string specifier.
|
||||
|
||||
The name may resolve either to a module, a test case class, a
|
||||
test method within a test case class, or a callable object which
|
||||
returns a TestCase or TestSuite instance.
|
||||
|
||||
The method optionally resolves the names relative to a given module.
|
||||
"""
|
||||
parts = name.split('.')
|
||||
error_case, error_message = None, None
|
||||
if module is None:
|
||||
parts_copy = parts[:]
|
||||
while parts_copy:
|
||||
try:
|
||||
module_name = '.'.join(parts_copy)
|
||||
module = __import__(module_name)
|
||||
break
|
||||
except ImportError:
|
||||
next_attribute = parts_copy.pop()
|
||||
# Last error so we can give it to the user if needed.
|
||||
error_case, error_message = _make_failed_import_test(
|
||||
next_attribute, self.suiteClass)
|
||||
if not parts_copy:
|
||||
# Even the top level import failed: report that error.
|
||||
self.errors.append(error_message)
|
||||
return error_case
|
||||
parts = parts[1:]
|
||||
obj = module
|
||||
for part in parts:
|
||||
try:
|
||||
parent, obj = obj, getattr(obj, part)
|
||||
except AttributeError as e:
|
||||
# We can't traverse some part of the name.
|
||||
if (getattr(obj, '__path__', None) is not None
|
||||
and error_case is not None):
|
||||
# This is a package (no __path__ per importlib docs), and we
|
||||
# encountered an error importing something. We cannot tell
|
||||
# the difference between package.WrongNameTestClass and
|
||||
# package.wrong_module_name so we just report the
|
||||
# ImportError - it is more informative.
|
||||
self.errors.append(error_message)
|
||||
return error_case
|
||||
else:
|
||||
# Otherwise, we signal that an AttributeError has occurred.
|
||||
error_case, error_message = _make_failed_test(
|
||||
part, e, self.suiteClass,
|
||||
'Failed to access attribute:\n%s' % (
|
||||
traceback.format_exc(),))
|
||||
self.errors.append(error_message)
|
||||
return error_case
|
||||
|
||||
if isinstance(obj, types.ModuleType):
|
||||
return self.loadTestsFromModule(obj)
|
||||
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
|
||||
return self.loadTestsFromTestCase(obj)
|
||||
elif (isinstance(obj, types.FunctionType) and
|
||||
isinstance(parent, type) and
|
||||
issubclass(parent, case.TestCase)):
|
||||
name = parts[-1]
|
||||
inst = parent(name)
|
||||
# static methods follow a different path
|
||||
if not isinstance(getattr(inst, name), types.FunctionType):
|
||||
return self.suiteClass([inst])
|
||||
elif isinstance(obj, suite.TestSuite):
|
||||
return obj
|
||||
if callable(obj):
|
||||
test = obj()
|
||||
if isinstance(test, suite.TestSuite):
|
||||
return test
|
||||
elif isinstance(test, case.TestCase):
|
||||
return self.suiteClass([test])
|
||||
else:
|
||||
raise TypeError("calling %s returned %s, not a test" %
|
||||
(obj, test))
|
||||
else:
|
||||
raise TypeError("don't know how to make test from: %s" % obj)
|
||||
|
||||
def loadTestsFromNames(self, names, module=None):
|
||||
"""Return a suite of all test cases found using the given sequence
|
||||
of string specifiers. See 'loadTestsFromName()'.
|
||||
"""
|
||||
suites = [self.loadTestsFromName(name, module) for name in names]
|
||||
return self.suiteClass(suites)
|
||||
|
||||
def getTestCaseNames(self, testCaseClass):
|
||||
"""Return a sorted sequence of method names found within testCaseClass
|
||||
"""
|
||||
def shouldIncludeMethod(attrname):
|
||||
if not attrname.startswith(self.testMethodPrefix):
|
||||
return False
|
||||
testFunc = getattr(testCaseClass, attrname)
|
||||
if not callable(testFunc):
|
||||
return False
|
||||
fullName = f'%s.%s.%s' % (
|
||||
testCaseClass.__module__, testCaseClass.__qualname__, attrname
|
||||
)
|
||||
return self.testNamePatterns is None or \
|
||||
any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
|
||||
testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
|
||||
if self.sortTestMethodsUsing:
|
||||
testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
|
||||
return testFnNames
|
||||
|
||||
def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
|
||||
"""Find and return all test modules from the specified start
|
||||
directory, recursing into subdirectories to find them and return all
|
||||
tests found within them. Only test files that match the pattern will
|
||||
be loaded. (Using shell style pattern matching.)
|
||||
|
||||
All test modules must be importable from the top level of the project.
|
||||
If the start directory is not the top level directory then the top
|
||||
level directory must be specified separately.
|
||||
|
||||
If a test package name (directory with '__init__.py') matches the
|
||||
pattern then the package will be checked for a 'load_tests' function. If
|
||||
this exists then it will be called with (loader, tests, pattern) unless
|
||||
the package has already had load_tests called from the same discovery
|
||||
invocation, in which case the package module object is not scanned for
|
||||
tests - this ensures that when a package uses discover to further
|
||||
discover child tests that infinite recursion does not happen.
|
||||
|
||||
If load_tests exists then discovery does *not* recurse into the package,
|
||||
load_tests is responsible for loading all tests in the package.
|
||||
|
||||
The pattern is deliberately not stored as a loader attribute so that
|
||||
packages can continue discovery themselves. top_level_dir is stored so
|
||||
load_tests does not need to pass this argument in to loader.discover().
|
||||
|
||||
Paths are sorted before being imported to ensure reproducible execution
|
||||
order even on filesystems with non-alphabetical ordering like ext3/4.
|
||||
"""
|
||||
set_implicit_top = False
|
||||
if top_level_dir is None and self._top_level_dir is not None:
|
||||
# make top_level_dir optional if called from load_tests in a package
|
||||
top_level_dir = self._top_level_dir
|
||||
elif top_level_dir is None:
|
||||
set_implicit_top = True
|
||||
top_level_dir = start_dir
|
||||
|
||||
top_level_dir = os.path.abspath(top_level_dir)
|
||||
|
||||
if not top_level_dir in sys.path:
|
||||
# all test modules must be importable from the top level directory
|
||||
# should we *unconditionally* put the start directory in first
|
||||
# in sys.path to minimise likelihood of conflicts between installed
|
||||
# modules and development versions?
|
||||
sys.path.insert(0, top_level_dir)
|
||||
self._top_level_dir = top_level_dir
|
||||
|
||||
is_not_importable = False
|
||||
if os.path.isdir(os.path.abspath(start_dir)):
|
||||
start_dir = os.path.abspath(start_dir)
|
||||
if start_dir != top_level_dir:
|
||||
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
|
||||
else:
|
||||
# support for discovery from dotted module names
|
||||
try:
|
||||
__import__(start_dir)
|
||||
except ImportError:
|
||||
is_not_importable = True
|
||||
else:
|
||||
the_module = sys.modules[start_dir]
|
||||
top_part = start_dir.split('.')[0]
|
||||
try:
|
||||
start_dir = os.path.abspath(
|
||||
os.path.dirname((the_module.__file__)))
|
||||
except AttributeError:
|
||||
if the_module.__name__ in sys.builtin_module_names:
|
||||
# builtin module
|
||||
raise TypeError('Can not use builtin modules '
|
||||
'as dotted module names') from None
|
||||
else:
|
||||
raise TypeError(
|
||||
f"don't know how to discover from {the_module!r}"
|
||||
) from None
|
||||
|
||||
if set_implicit_top:
|
||||
self._top_level_dir = self._get_directory_containing_module(top_part)
|
||||
sys.path.remove(top_level_dir)
|
||||
|
||||
if is_not_importable:
|
||||
raise ImportError('Start directory is not importable: %r' % start_dir)
|
||||
|
||||
tests = list(self._find_tests(start_dir, pattern))
|
||||
return self.suiteClass(tests)
|
||||
|
||||
def _get_directory_containing_module(self, module_name):
|
||||
module = sys.modules[module_name]
|
||||
full_path = os.path.abspath(module.__file__)
|
||||
|
||||
if os.path.basename(full_path).lower().startswith('__init__.py'):
|
||||
return os.path.dirname(os.path.dirname(full_path))
|
||||
else:
|
||||
# here we have been given a module rather than a package - so
|
||||
# all we can do is search the *same* directory the module is in
|
||||
# should an exception be raised instead
|
||||
return os.path.dirname(full_path)
|
||||
|
||||
def _get_name_from_path(self, path):
|
||||
if path == self._top_level_dir:
|
||||
return '.'
|
||||
path = _jython_aware_splitext(os.path.normpath(path))
|
||||
|
||||
_relpath = os.path.relpath(path, self._top_level_dir)
|
||||
assert not os.path.isabs(_relpath), "Path must be within the project"
|
||||
assert not _relpath.startswith('..'), "Path must be within the project"
|
||||
|
||||
name = _relpath.replace(os.path.sep, '.')
|
||||
return name
|
||||
|
||||
def _get_module_from_name(self, name):
|
||||
__import__(name)
|
||||
return sys.modules[name]
|
||||
|
||||
def _match_path(self, path, full_path, pattern):
|
||||
# override this method to use alternative matching strategy
|
||||
return fnmatch(path, pattern)
|
||||
|
||||
def _find_tests(self, start_dir, pattern):
|
||||
"""Used by discovery. Yields test suites it loads."""
|
||||
# Handle the __init__ in this package
|
||||
name = self._get_name_from_path(start_dir)
|
||||
# name is '.' when start_dir == top_level_dir (and top_level_dir is by
|
||||
# definition not a package).
|
||||
if name != '.' and name not in self._loading_packages:
|
||||
# name is in self._loading_packages while we have called into
|
||||
# loadTestsFromModule with name.
|
||||
tests, should_recurse = self._find_test_path(start_dir, pattern)
|
||||
if tests is not None:
|
||||
yield tests
|
||||
if not should_recurse:
|
||||
# Either an error occurred, or load_tests was used by the
|
||||
# package.
|
||||
return
|
||||
# Handle the contents.
|
||||
paths = sorted(os.listdir(start_dir))
|
||||
for path in paths:
|
||||
full_path = os.path.join(start_dir, path)
|
||||
tests, should_recurse = self._find_test_path(full_path, pattern)
|
||||
if tests is not None:
|
||||
yield tests
|
||||
if should_recurse:
|
||||
# we found a package that didn't use load_tests.
|
||||
name = self._get_name_from_path(full_path)
|
||||
self._loading_packages.add(name)
|
||||
try:
|
||||
yield from self._find_tests(full_path, pattern)
|
||||
finally:
|
||||
self._loading_packages.discard(name)
|
||||
|
||||
def _find_test_path(self, full_path, pattern):
|
||||
"""Used by discovery.
|
||||
|
||||
Loads tests from a single file, or a directories' __init__.py when
|
||||
passed the directory.
|
||||
|
||||
Returns a tuple (None_or_tests_from_file, should_recurse).
|
||||
"""
|
||||
basename = os.path.basename(full_path)
|
||||
if os.path.isfile(full_path):
|
||||
if not VALID_MODULE_NAME.match(basename):
|
||||
# valid Python identifiers only
|
||||
return None, False
|
||||
if not self._match_path(basename, full_path, pattern):
|
||||
return None, False
|
||||
# if the test file matches, load it
|
||||
name = self._get_name_from_path(full_path)
|
||||
try:
|
||||
module = self._get_module_from_name(name)
|
||||
except case.SkipTest as e:
|
||||
return _make_skipped_test(name, e, self.suiteClass), False
|
||||
except:
|
||||
error_case, error_message = \
|
||||
_make_failed_import_test(name, self.suiteClass)
|
||||
self.errors.append(error_message)
|
||||
return error_case, False
|
||||
else:
|
||||
mod_file = os.path.abspath(
|
||||
getattr(module, '__file__', full_path))
|
||||
realpath = _jython_aware_splitext(
|
||||
os.path.realpath(mod_file))
|
||||
fullpath_noext = _jython_aware_splitext(
|
||||
os.path.realpath(full_path))
|
||||
if realpath.lower() != fullpath_noext.lower():
|
||||
module_dir = os.path.dirname(realpath)
|
||||
mod_name = _jython_aware_splitext(
|
||||
os.path.basename(full_path))
|
||||
expected_dir = os.path.dirname(full_path)
|
||||
msg = ("%r module incorrectly imported from %r. Expected "
|
||||
"%r. Is this module globally installed?")
|
||||
raise ImportError(
|
||||
msg % (mod_name, module_dir, expected_dir))
|
||||
return self.loadTestsFromModule(module, pattern=pattern), False
|
||||
elif os.path.isdir(full_path):
|
||||
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
|
||||
return None, False
|
||||
|
||||
load_tests = None
|
||||
tests = None
|
||||
name = self._get_name_from_path(full_path)
|
||||
try:
|
||||
package = self._get_module_from_name(name)
|
||||
except case.SkipTest as e:
|
||||
return _make_skipped_test(name, e, self.suiteClass), False
|
||||
except:
|
||||
error_case, error_message = \
|
||||
_make_failed_import_test(name, self.suiteClass)
|
||||
self.errors.append(error_message)
|
||||
return error_case, False
|
||||
else:
|
||||
load_tests = getattr(package, 'load_tests', None)
|
||||
# Mark this package as being in load_tests (possibly ;))
|
||||
self._loading_packages.add(name)
|
||||
try:
|
||||
tests = self.loadTestsFromModule(package, pattern=pattern)
|
||||
if load_tests is not None:
|
||||
# loadTestsFromModule(package) has loaded tests for us.
|
||||
return tests, False
|
||||
return tests, True
|
||||
finally:
|
||||
self._loading_packages.discard(name)
|
||||
else:
|
||||
return None, False
|
||||
|
||||
|
||||
defaultTestLoader = TestLoader()
|
||||
|
||||
|
||||
# These functions are considered obsolete for long time.
|
||||
# They will be removed in Python 3.13.
|
||||
|
||||
def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
|
||||
loader = TestLoader()
|
||||
loader.sortTestMethodsUsing = sortUsing
|
||||
loader.testMethodPrefix = prefix
|
||||
loader.testNamePatterns = testNamePatterns
|
||||
if suiteClass:
|
||||
loader.suiteClass = suiteClass
|
||||
return loader
|
||||
|
||||
def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"unittest.getTestCaseNames() is deprecated and will be removed in Python 3.13. "
|
||||
"Please use unittest.TestLoader.getTestCaseNames() instead.",
|
||||
DeprecationWarning, stacklevel=2
|
||||
)
|
||||
return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
|
||||
|
||||
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
|
||||
suiteClass=suite.TestSuite):
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"unittest.makeSuite() is deprecated and will be removed in Python 3.13. "
|
||||
"Please use unittest.TestLoader.loadTestsFromTestCase() instead.",
|
||||
DeprecationWarning, stacklevel=2
|
||||
)
|
||||
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
|
||||
testCaseClass)
|
||||
|
||||
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
|
||||
suiteClass=suite.TestSuite):
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"unittest.findTestCases() is deprecated and will be removed in Python 3.13. "
|
||||
"Please use unittest.TestLoader.loadTestsFromModule() instead.",
|
||||
DeprecationWarning, stacklevel=2
|
||||
)
|
||||
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
|
||||
module)
|
||||
278
.CondaPkg/env/Lib/unittest/main.py
vendored
278
.CondaPkg/env/Lib/unittest/main.py
vendored
@@ -1,278 +0,0 @@
|
||||
"""Unittest main program"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import os
|
||||
import warnings
|
||||
|
||||
from . import loader, runner
|
||||
from .signals import installHandler
|
||||
|
||||
__unittest = True
|
||||
|
||||
MAIN_EXAMPLES = """\
|
||||
Examples:
|
||||
%(prog)s test_module - run tests from test_module
|
||||
%(prog)s module.TestClass - run tests from module.TestClass
|
||||
%(prog)s module.Class.test_method - run specified test method
|
||||
%(prog)s path/to/test_file.py - run tests from test_file.py
|
||||
"""
|
||||
|
||||
MODULE_EXAMPLES = """\
|
||||
Examples:
|
||||
%(prog)s - run default set of tests
|
||||
%(prog)s MyTestSuite - run suite 'MyTestSuite'
|
||||
%(prog)s MyTestCase.testSomething - run MyTestCase.testSomething
|
||||
%(prog)s MyTestCase - run all 'test*' test methods
|
||||
in MyTestCase
|
||||
"""
|
||||
|
||||
def _convert_name(name):
|
||||
# on Linux / Mac OS X 'foo.PY' is not importable, but on
|
||||
# Windows it is. Simpler to do a case insensitive match
|
||||
# a better check would be to check that the name is a
|
||||
# valid Python module name.
|
||||
if os.path.isfile(name) and name.lower().endswith('.py'):
|
||||
if os.path.isabs(name):
|
||||
rel_path = os.path.relpath(name, os.getcwd())
|
||||
if os.path.isabs(rel_path) or rel_path.startswith(os.pardir):
|
||||
return name
|
||||
name = rel_path
|
||||
# on Windows both '\' and '/' are used as path
|
||||
# separators. Better to replace both than rely on os.path.sep
|
||||
return os.path.normpath(name)[:-3].replace('\\', '.').replace('/', '.')
|
||||
return name
|
||||
|
||||
def _convert_names(names):
|
||||
return [_convert_name(name) for name in names]
|
||||
|
||||
|
||||
def _convert_select_pattern(pattern):
|
||||
if not '*' in pattern:
|
||||
pattern = '*%s*' % pattern
|
||||
return pattern
|
||||
|
||||
|
||||
class TestProgram(object):
|
||||
"""A command-line program that runs a set of tests; this is primarily
|
||||
for making test modules conveniently executable.
|
||||
"""
|
||||
# defaults for testing
|
||||
module=None
|
||||
verbosity = 1
|
||||
failfast = catchbreak = buffer = progName = warnings = testNamePatterns = None
|
||||
_discovery_parser = None
|
||||
|
||||
def __init__(self, module='__main__', defaultTest=None, argv=None,
|
||||
testRunner=None, testLoader=loader.defaultTestLoader,
|
||||
exit=True, verbosity=1, failfast=None, catchbreak=None,
|
||||
buffer=None, warnings=None, *, tb_locals=False):
|
||||
if isinstance(module, str):
|
||||
self.module = __import__(module)
|
||||
for part in module.split('.')[1:]:
|
||||
self.module = getattr(self.module, part)
|
||||
else:
|
||||
self.module = module
|
||||
if argv is None:
|
||||
argv = sys.argv
|
||||
|
||||
self.exit = exit
|
||||
self.failfast = failfast
|
||||
self.catchbreak = catchbreak
|
||||
self.verbosity = verbosity
|
||||
self.buffer = buffer
|
||||
self.tb_locals = tb_locals
|
||||
if warnings is None and not sys.warnoptions:
|
||||
# even if DeprecationWarnings are ignored by default
|
||||
# print them anyway unless other warnings settings are
|
||||
# specified by the warnings arg or the -W python flag
|
||||
self.warnings = 'default'
|
||||
else:
|
||||
# here self.warnings is set either to the value passed
|
||||
# to the warnings args or to None.
|
||||
# If the user didn't pass a value self.warnings will
|
||||
# be None. This means that the behavior is unchanged
|
||||
# and depends on the values passed to -W.
|
||||
self.warnings = warnings
|
||||
self.defaultTest = defaultTest
|
||||
self.testRunner = testRunner
|
||||
self.testLoader = testLoader
|
||||
self.progName = os.path.basename(argv[0])
|
||||
self.parseArgs(argv)
|
||||
self.runTests()
|
||||
|
||||
def usageExit(self, msg=None):
|
||||
warnings.warn("TestProgram.usageExit() is deprecated and will be"
|
||||
" removed in Python 3.13", DeprecationWarning)
|
||||
if msg:
|
||||
print(msg)
|
||||
if self._discovery_parser is None:
|
||||
self._initArgParsers()
|
||||
self._print_help()
|
||||
sys.exit(2)
|
||||
|
||||
def _print_help(self, *args, **kwargs):
|
||||
if self.module is None:
|
||||
print(self._main_parser.format_help())
|
||||
print(MAIN_EXAMPLES % {'prog': self.progName})
|
||||
self._discovery_parser.print_help()
|
||||
else:
|
||||
print(self._main_parser.format_help())
|
||||
print(MODULE_EXAMPLES % {'prog': self.progName})
|
||||
|
||||
def parseArgs(self, argv):
|
||||
self._initArgParsers()
|
||||
if self.module is None:
|
||||
if len(argv) > 1 and argv[1].lower() == 'discover':
|
||||
self._do_discovery(argv[2:])
|
||||
return
|
||||
self._main_parser.parse_args(argv[1:], self)
|
||||
if not self.tests:
|
||||
# this allows "python -m unittest -v" to still work for
|
||||
# test discovery.
|
||||
self._do_discovery([])
|
||||
return
|
||||
else:
|
||||
self._main_parser.parse_args(argv[1:], self)
|
||||
|
||||
if self.tests:
|
||||
self.testNames = _convert_names(self.tests)
|
||||
if __name__ == '__main__':
|
||||
# to support python -m unittest ...
|
||||
self.module = None
|
||||
elif self.defaultTest is None:
|
||||
# createTests will load tests from self.module
|
||||
self.testNames = None
|
||||
elif isinstance(self.defaultTest, str):
|
||||
self.testNames = (self.defaultTest,)
|
||||
else:
|
||||
self.testNames = list(self.defaultTest)
|
||||
self.createTests()
|
||||
|
||||
def createTests(self, from_discovery=False, Loader=None):
|
||||
if self.testNamePatterns:
|
||||
self.testLoader.testNamePatterns = self.testNamePatterns
|
||||
if from_discovery:
|
||||
loader = self.testLoader if Loader is None else Loader()
|
||||
self.test = loader.discover(self.start, self.pattern, self.top)
|
||||
elif self.testNames is None:
|
||||
self.test = self.testLoader.loadTestsFromModule(self.module)
|
||||
else:
|
||||
self.test = self.testLoader.loadTestsFromNames(self.testNames,
|
||||
self.module)
|
||||
|
||||
def _initArgParsers(self):
|
||||
parent_parser = self._getParentArgParser()
|
||||
self._main_parser = self._getMainArgParser(parent_parser)
|
||||
self._discovery_parser = self._getDiscoveryArgParser(parent_parser)
|
||||
|
||||
def _getParentArgParser(self):
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
|
||||
parser.add_argument('-v', '--verbose', dest='verbosity',
|
||||
action='store_const', const=2,
|
||||
help='Verbose output')
|
||||
parser.add_argument('-q', '--quiet', dest='verbosity',
|
||||
action='store_const', const=0,
|
||||
help='Quiet output')
|
||||
parser.add_argument('--locals', dest='tb_locals',
|
||||
action='store_true',
|
||||
help='Show local variables in tracebacks')
|
||||
if self.failfast is None:
|
||||
parser.add_argument('-f', '--failfast', dest='failfast',
|
||||
action='store_true',
|
||||
help='Stop on first fail or error')
|
||||
self.failfast = False
|
||||
if self.catchbreak is None:
|
||||
parser.add_argument('-c', '--catch', dest='catchbreak',
|
||||
action='store_true',
|
||||
help='Catch Ctrl-C and display results so far')
|
||||
self.catchbreak = False
|
||||
if self.buffer is None:
|
||||
parser.add_argument('-b', '--buffer', dest='buffer',
|
||||
action='store_true',
|
||||
help='Buffer stdout and stderr during tests')
|
||||
self.buffer = False
|
||||
if self.testNamePatterns is None:
|
||||
parser.add_argument('-k', dest='testNamePatterns',
|
||||
action='append', type=_convert_select_pattern,
|
||||
help='Only run tests which match the given substring')
|
||||
self.testNamePatterns = []
|
||||
|
||||
return parser
|
||||
|
||||
def _getMainArgParser(self, parent):
|
||||
parser = argparse.ArgumentParser(parents=[parent])
|
||||
parser.prog = self.progName
|
||||
parser.print_help = self._print_help
|
||||
|
||||
parser.add_argument('tests', nargs='*',
|
||||
help='a list of any number of test modules, '
|
||||
'classes and test methods.')
|
||||
|
||||
return parser
|
||||
|
||||
def _getDiscoveryArgParser(self, parent):
|
||||
parser = argparse.ArgumentParser(parents=[parent])
|
||||
parser.prog = '%s discover' % self.progName
|
||||
parser.epilog = ('For test discovery all test modules must be '
|
||||
'importable from the top level directory of the '
|
||||
'project.')
|
||||
|
||||
parser.add_argument('-s', '--start-directory', dest='start',
|
||||
help="Directory to start discovery ('.' default)")
|
||||
parser.add_argument('-p', '--pattern', dest='pattern',
|
||||
help="Pattern to match tests ('test*.py' default)")
|
||||
parser.add_argument('-t', '--top-level-directory', dest='top',
|
||||
help='Top level directory of project (defaults to '
|
||||
'start directory)')
|
||||
for arg in ('start', 'pattern', 'top'):
|
||||
parser.add_argument(arg, nargs='?',
|
||||
default=argparse.SUPPRESS,
|
||||
help=argparse.SUPPRESS)
|
||||
|
||||
return parser
|
||||
|
||||
def _do_discovery(self, argv, Loader=None):
|
||||
self.start = '.'
|
||||
self.pattern = 'test*.py'
|
||||
self.top = None
|
||||
if argv is not None:
|
||||
# handle command line args for test discovery
|
||||
if self._discovery_parser is None:
|
||||
# for testing
|
||||
self._initArgParsers()
|
||||
self._discovery_parser.parse_args(argv, self)
|
||||
|
||||
self.createTests(from_discovery=True, Loader=Loader)
|
||||
|
||||
def runTests(self):
|
||||
if self.catchbreak:
|
||||
installHandler()
|
||||
if self.testRunner is None:
|
||||
self.testRunner = runner.TextTestRunner
|
||||
if isinstance(self.testRunner, type):
|
||||
try:
|
||||
try:
|
||||
testRunner = self.testRunner(verbosity=self.verbosity,
|
||||
failfast=self.failfast,
|
||||
buffer=self.buffer,
|
||||
warnings=self.warnings,
|
||||
tb_locals=self.tb_locals)
|
||||
except TypeError:
|
||||
# didn't accept the tb_locals argument
|
||||
testRunner = self.testRunner(verbosity=self.verbosity,
|
||||
failfast=self.failfast,
|
||||
buffer=self.buffer,
|
||||
warnings=self.warnings)
|
||||
except TypeError:
|
||||
# didn't accept the verbosity, buffer or failfast arguments
|
||||
testRunner = self.testRunner()
|
||||
else:
|
||||
# it is assumed to be a TestRunner instance
|
||||
testRunner = self.testRunner
|
||||
self.result = testRunner.run(self.test)
|
||||
if self.exit:
|
||||
sys.exit(not self.result.wasSuccessful())
|
||||
|
||||
main = TestProgram
|
||||
2976
.CondaPkg/env/Lib/unittest/mock.py
vendored
2976
.CondaPkg/env/Lib/unittest/mock.py
vendored
File diff suppressed because it is too large
Load Diff
244
.CondaPkg/env/Lib/unittest/result.py
vendored
244
.CondaPkg/env/Lib/unittest/result.py
vendored
@@ -1,244 +0,0 @@
|
||||
"""Test result object"""
|
||||
|
||||
import io
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from . import util
|
||||
from functools import wraps
|
||||
|
||||
__unittest = True
|
||||
|
||||
def failfast(method):
|
||||
@wraps(method)
|
||||
def inner(self, *args, **kw):
|
||||
if getattr(self, 'failfast', False):
|
||||
self.stop()
|
||||
return method(self, *args, **kw)
|
||||
return inner
|
||||
|
||||
STDOUT_LINE = '\nStdout:\n%s'
|
||||
STDERR_LINE = '\nStderr:\n%s'
|
||||
|
||||
|
||||
class TestResult(object):
|
||||
"""Holder for test result information.
|
||||
|
||||
Test results are automatically managed by the TestCase and TestSuite
|
||||
classes, and do not need to be explicitly manipulated by writers of tests.
|
||||
|
||||
Each instance holds the total number of tests run, and collections of
|
||||
failures and errors that occurred among those test runs. The collections
|
||||
contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
|
||||
formatted traceback of the error that occurred.
|
||||
"""
|
||||
_previousTestClass = None
|
||||
_testRunEntered = False
|
||||
_moduleSetUpFailed = False
|
||||
def __init__(self, stream=None, descriptions=None, verbosity=None):
|
||||
self.failfast = False
|
||||
self.failures = []
|
||||
self.errors = []
|
||||
self.testsRun = 0
|
||||
self.skipped = []
|
||||
self.expectedFailures = []
|
||||
self.unexpectedSuccesses = []
|
||||
self.shouldStop = False
|
||||
self.buffer = False
|
||||
self.tb_locals = False
|
||||
self._stdout_buffer = None
|
||||
self._stderr_buffer = None
|
||||
self._original_stdout = sys.stdout
|
||||
self._original_stderr = sys.stderr
|
||||
self._mirrorOutput = False
|
||||
|
||||
def printErrors(self):
|
||||
"Called by TestRunner after test run"
|
||||
|
||||
def startTest(self, test):
|
||||
"Called when the given test is about to be run"
|
||||
self.testsRun += 1
|
||||
self._mirrorOutput = False
|
||||
self._setupStdout()
|
||||
|
||||
def _setupStdout(self):
|
||||
if self.buffer:
|
||||
if self._stderr_buffer is None:
|
||||
self._stderr_buffer = io.StringIO()
|
||||
self._stdout_buffer = io.StringIO()
|
||||
sys.stdout = self._stdout_buffer
|
||||
sys.stderr = self._stderr_buffer
|
||||
|
||||
def startTestRun(self):
|
||||
"""Called once before any tests are executed.
|
||||
|
||||
See startTest for a method called before each test.
|
||||
"""
|
||||
|
||||
def stopTest(self, test):
|
||||
"""Called when the given test has been run"""
|
||||
self._restoreStdout()
|
||||
self._mirrorOutput = False
|
||||
|
||||
def _restoreStdout(self):
|
||||
if self.buffer:
|
||||
if self._mirrorOutput:
|
||||
output = sys.stdout.getvalue()
|
||||
error = sys.stderr.getvalue()
|
||||
if output:
|
||||
if not output.endswith('\n'):
|
||||
output += '\n'
|
||||
self._original_stdout.write(STDOUT_LINE % output)
|
||||
if error:
|
||||
if not error.endswith('\n'):
|
||||
error += '\n'
|
||||
self._original_stderr.write(STDERR_LINE % error)
|
||||
|
||||
sys.stdout = self._original_stdout
|
||||
sys.stderr = self._original_stderr
|
||||
self._stdout_buffer.seek(0)
|
||||
self._stdout_buffer.truncate()
|
||||
self._stderr_buffer.seek(0)
|
||||
self._stderr_buffer.truncate()
|
||||
|
||||
def stopTestRun(self):
|
||||
"""Called once after all tests are executed.
|
||||
|
||||
See stopTest for a method called after each test.
|
||||
"""
|
||||
|
||||
@failfast
|
||||
def addError(self, test, err):
|
||||
"""Called when an error has occurred. 'err' is a tuple of values as
|
||||
returned by sys.exc_info().
|
||||
"""
|
||||
self.errors.append((test, self._exc_info_to_string(err, test)))
|
||||
self._mirrorOutput = True
|
||||
|
||||
@failfast
|
||||
def addFailure(self, test, err):
|
||||
"""Called when an error has occurred. 'err' is a tuple of values as
|
||||
returned by sys.exc_info()."""
|
||||
self.failures.append((test, self._exc_info_to_string(err, test)))
|
||||
self._mirrorOutput = True
|
||||
|
||||
def addSubTest(self, test, subtest, err):
|
||||
"""Called at the end of a subtest.
|
||||
'err' is None if the subtest ended successfully, otherwise it's a
|
||||
tuple of values as returned by sys.exc_info().
|
||||
"""
|
||||
# By default, we don't do anything with successful subtests, but
|
||||
# more sophisticated test results might want to record them.
|
||||
if err is not None:
|
||||
if getattr(self, 'failfast', False):
|
||||
self.stop()
|
||||
if issubclass(err[0], test.failureException):
|
||||
errors = self.failures
|
||||
else:
|
||||
errors = self.errors
|
||||
errors.append((subtest, self._exc_info_to_string(err, test)))
|
||||
self._mirrorOutput = True
|
||||
|
||||
def addSuccess(self, test):
|
||||
"Called when a test has completed successfully"
|
||||
pass
|
||||
|
||||
def addSkip(self, test, reason):
|
||||
"""Called when a test is skipped."""
|
||||
self.skipped.append((test, reason))
|
||||
|
||||
def addExpectedFailure(self, test, err):
|
||||
"""Called when an expected failure/error occurred."""
|
||||
self.expectedFailures.append(
|
||||
(test, self._exc_info_to_string(err, test)))
|
||||
|
||||
@failfast
|
||||
def addUnexpectedSuccess(self, test):
|
||||
"""Called when a test was expected to fail, but succeed."""
|
||||
self.unexpectedSuccesses.append(test)
|
||||
|
||||
def wasSuccessful(self):
|
||||
"""Tells whether or not this result was a success."""
|
||||
# The hasattr check is for test_result's OldResult test. That
|
||||
# way this method works on objects that lack the attribute.
|
||||
# (where would such result instances come from? old stored pickles?)
|
||||
return ((len(self.failures) == len(self.errors) == 0) and
|
||||
(not hasattr(self, 'unexpectedSuccesses') or
|
||||
len(self.unexpectedSuccesses) == 0))
|
||||
|
||||
def stop(self):
|
||||
"""Indicates that the tests should be aborted."""
|
||||
self.shouldStop = True
|
||||
|
||||
def _exc_info_to_string(self, err, test):
|
||||
"""Converts a sys.exc_info()-style tuple of values into a string."""
|
||||
exctype, value, tb = err
|
||||
tb = self._clean_tracebacks(exctype, value, tb, test)
|
||||
tb_e = traceback.TracebackException(
|
||||
exctype, value, tb,
|
||||
capture_locals=self.tb_locals, compact=True)
|
||||
msgLines = list(tb_e.format())
|
||||
|
||||
if self.buffer:
|
||||
output = sys.stdout.getvalue()
|
||||
error = sys.stderr.getvalue()
|
||||
if output:
|
||||
if not output.endswith('\n'):
|
||||
output += '\n'
|
||||
msgLines.append(STDOUT_LINE % output)
|
||||
if error:
|
||||
if not error.endswith('\n'):
|
||||
error += '\n'
|
||||
msgLines.append(STDERR_LINE % error)
|
||||
return ''.join(msgLines)
|
||||
|
||||
def _clean_tracebacks(self, exctype, value, tb, test):
|
||||
ret = None
|
||||
first = True
|
||||
excs = [(exctype, value, tb)]
|
||||
seen = {id(value)} # Detect loops in chained exceptions.
|
||||
while excs:
|
||||
(exctype, value, tb) = excs.pop()
|
||||
# Skip test runner traceback levels
|
||||
while tb and self._is_relevant_tb_level(tb):
|
||||
tb = tb.tb_next
|
||||
|
||||
# Skip assert*() traceback levels
|
||||
if exctype is test.failureException:
|
||||
self._remove_unittest_tb_frames(tb)
|
||||
|
||||
if first:
|
||||
ret = tb
|
||||
first = False
|
||||
else:
|
||||
value.__traceback__ = tb
|
||||
|
||||
if value is not None:
|
||||
for c in (value.__cause__, value.__context__):
|
||||
if c is not None and id(c) not in seen:
|
||||
excs.append((type(c), c, c.__traceback__))
|
||||
seen.add(id(c))
|
||||
return ret
|
||||
|
||||
def _is_relevant_tb_level(self, tb):
|
||||
return '__unittest' in tb.tb_frame.f_globals
|
||||
|
||||
def _remove_unittest_tb_frames(self, tb):
|
||||
'''Truncates usercode tb at the first unittest frame.
|
||||
|
||||
If the first frame of the traceback is in user code,
|
||||
the prefix up to the first unittest frame is returned.
|
||||
If the first frame is already in the unittest module,
|
||||
the traceback is not modified.
|
||||
'''
|
||||
prev = None
|
||||
while tb and not self._is_relevant_tb_level(tb):
|
||||
prev = tb
|
||||
tb = tb.tb_next
|
||||
if prev is not None:
|
||||
prev.tb_next = None
|
||||
|
||||
def __repr__(self):
|
||||
return ("<%s run=%i errors=%i failures=%i>" %
|
||||
(util.strclass(self.__class__), self.testsRun, len(self.errors),
|
||||
len(self.failures)))
|
||||
263
.CondaPkg/env/Lib/unittest/runner.py
vendored
263
.CondaPkg/env/Lib/unittest/runner.py
vendored
@@ -1,263 +0,0 @@
|
||||
"""Running tests"""
|
||||
|
||||
import sys
|
||||
import time
|
||||
import warnings
|
||||
|
||||
from . import result
|
||||
from .case import _SubTest
|
||||
from .signals import registerResult
|
||||
|
||||
__unittest = True
|
||||
|
||||
|
||||
class _WritelnDecorator(object):
|
||||
"""Used to decorate file-like objects with a handy 'writeln' method"""
|
||||
def __init__(self,stream):
|
||||
self.stream = stream
|
||||
|
||||
def __getattr__(self, attr):
|
||||
if attr in ('stream', '__getstate__'):
|
||||
raise AttributeError(attr)
|
||||
return getattr(self.stream,attr)
|
||||
|
||||
def writeln(self, arg=None):
|
||||
if arg:
|
||||
self.write(arg)
|
||||
self.write('\n') # text-mode streams translate to \r\n if needed
|
||||
|
||||
|
||||
class TextTestResult(result.TestResult):
|
||||
"""A test result class that can print formatted text results to a stream.
|
||||
|
||||
Used by TextTestRunner.
|
||||
"""
|
||||
separator1 = '=' * 70
|
||||
separator2 = '-' * 70
|
||||
|
||||
def __init__(self, stream, descriptions, verbosity):
|
||||
super(TextTestResult, self).__init__(stream, descriptions, verbosity)
|
||||
self.stream = stream
|
||||
self.showAll = verbosity > 1
|
||||
self.dots = verbosity == 1
|
||||
self.descriptions = descriptions
|
||||
self._newline = True
|
||||
|
||||
def getDescription(self, test):
|
||||
doc_first_line = test.shortDescription()
|
||||
if self.descriptions and doc_first_line:
|
||||
return '\n'.join((str(test), doc_first_line))
|
||||
else:
|
||||
return str(test)
|
||||
|
||||
def startTest(self, test):
|
||||
super(TextTestResult, self).startTest(test)
|
||||
if self.showAll:
|
||||
self.stream.write(self.getDescription(test))
|
||||
self.stream.write(" ... ")
|
||||
self.stream.flush()
|
||||
self._newline = False
|
||||
|
||||
def _write_status(self, test, status):
|
||||
is_subtest = isinstance(test, _SubTest)
|
||||
if is_subtest or self._newline:
|
||||
if not self._newline:
|
||||
self.stream.writeln()
|
||||
if is_subtest:
|
||||
self.stream.write(" ")
|
||||
self.stream.write(self.getDescription(test))
|
||||
self.stream.write(" ... ")
|
||||
self.stream.writeln(status)
|
||||
self.stream.flush()
|
||||
self._newline = True
|
||||
|
||||
def addSubTest(self, test, subtest, err):
|
||||
if err is not None:
|
||||
if self.showAll:
|
||||
if issubclass(err[0], subtest.failureException):
|
||||
self._write_status(subtest, "FAIL")
|
||||
else:
|
||||
self._write_status(subtest, "ERROR")
|
||||
elif self.dots:
|
||||
if issubclass(err[0], subtest.failureException):
|
||||
self.stream.write('F')
|
||||
else:
|
||||
self.stream.write('E')
|
||||
self.stream.flush()
|
||||
super(TextTestResult, self).addSubTest(test, subtest, err)
|
||||
|
||||
def addSuccess(self, test):
|
||||
super(TextTestResult, self).addSuccess(test)
|
||||
if self.showAll:
|
||||
self._write_status(test, "ok")
|
||||
elif self.dots:
|
||||
self.stream.write('.')
|
||||
self.stream.flush()
|
||||
|
||||
def addError(self, test, err):
|
||||
super(TextTestResult, self).addError(test, err)
|
||||
if self.showAll:
|
||||
self._write_status(test, "ERROR")
|
||||
elif self.dots:
|
||||
self.stream.write('E')
|
||||
self.stream.flush()
|
||||
|
||||
def addFailure(self, test, err):
|
||||
super(TextTestResult, self).addFailure(test, err)
|
||||
if self.showAll:
|
||||
self._write_status(test, "FAIL")
|
||||
elif self.dots:
|
||||
self.stream.write('F')
|
||||
self.stream.flush()
|
||||
|
||||
def addSkip(self, test, reason):
|
||||
super(TextTestResult, self).addSkip(test, reason)
|
||||
if self.showAll:
|
||||
self._write_status(test, "skipped {0!r}".format(reason))
|
||||
elif self.dots:
|
||||
self.stream.write("s")
|
||||
self.stream.flush()
|
||||
|
||||
def addExpectedFailure(self, test, err):
|
||||
super(TextTestResult, self).addExpectedFailure(test, err)
|
||||
if self.showAll:
|
||||
self.stream.writeln("expected failure")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write("x")
|
||||
self.stream.flush()
|
||||
|
||||
def addUnexpectedSuccess(self, test):
|
||||
super(TextTestResult, self).addUnexpectedSuccess(test)
|
||||
if self.showAll:
|
||||
self.stream.writeln("unexpected success")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write("u")
|
||||
self.stream.flush()
|
||||
|
||||
def printErrors(self):
|
||||
if self.dots or self.showAll:
|
||||
self.stream.writeln()
|
||||
self.stream.flush()
|
||||
self.printErrorList('ERROR', self.errors)
|
||||
self.printErrorList('FAIL', self.failures)
|
||||
unexpectedSuccesses = getattr(self, 'unexpectedSuccesses', ())
|
||||
if unexpectedSuccesses:
|
||||
self.stream.writeln(self.separator1)
|
||||
for test in unexpectedSuccesses:
|
||||
self.stream.writeln(f"UNEXPECTED SUCCESS: {self.getDescription(test)}")
|
||||
self.stream.flush()
|
||||
|
||||
def printErrorList(self, flavour, errors):
|
||||
for test, err in errors:
|
||||
self.stream.writeln(self.separator1)
|
||||
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
|
||||
self.stream.writeln(self.separator2)
|
||||
self.stream.writeln("%s" % err)
|
||||
self.stream.flush()
|
||||
|
||||
|
||||
class TextTestRunner(object):
|
||||
"""A test runner class that displays results in textual form.
|
||||
|
||||
It prints out the names of tests as they are run, errors as they
|
||||
occur, and a summary of the results at the end of the test run.
|
||||
"""
|
||||
resultclass = TextTestResult
|
||||
|
||||
def __init__(self, stream=None, descriptions=True, verbosity=1,
|
||||
failfast=False, buffer=False, resultclass=None, warnings=None,
|
||||
*, tb_locals=False):
|
||||
"""Construct a TextTestRunner.
|
||||
|
||||
Subclasses should accept **kwargs to ensure compatibility as the
|
||||
interface changes.
|
||||
"""
|
||||
if stream is None:
|
||||
stream = sys.stderr
|
||||
self.stream = _WritelnDecorator(stream)
|
||||
self.descriptions = descriptions
|
||||
self.verbosity = verbosity
|
||||
self.failfast = failfast
|
||||
self.buffer = buffer
|
||||
self.tb_locals = tb_locals
|
||||
self.warnings = warnings
|
||||
if resultclass is not None:
|
||||
self.resultclass = resultclass
|
||||
|
||||
def _makeResult(self):
|
||||
return self.resultclass(self.stream, self.descriptions, self.verbosity)
|
||||
|
||||
def run(self, test):
|
||||
"Run the given test case or test suite."
|
||||
result = self._makeResult()
|
||||
registerResult(result)
|
||||
result.failfast = self.failfast
|
||||
result.buffer = self.buffer
|
||||
result.tb_locals = self.tb_locals
|
||||
with warnings.catch_warnings():
|
||||
if self.warnings:
|
||||
# if self.warnings is set, use it to filter all the warnings
|
||||
warnings.simplefilter(self.warnings)
|
||||
# if the filter is 'default' or 'always', special-case the
|
||||
# warnings from the deprecated unittest methods to show them
|
||||
# no more than once per module, because they can be fairly
|
||||
# noisy. The -Wd and -Wa flags can be used to bypass this
|
||||
# only when self.warnings is None.
|
||||
if self.warnings in ['default', 'always']:
|
||||
warnings.filterwarnings('module',
|
||||
category=DeprecationWarning,
|
||||
message=r'Please use assert\w+ instead.')
|
||||
startTime = time.perf_counter()
|
||||
startTestRun = getattr(result, 'startTestRun', None)
|
||||
if startTestRun is not None:
|
||||
startTestRun()
|
||||
try:
|
||||
test(result)
|
||||
finally:
|
||||
stopTestRun = getattr(result, 'stopTestRun', None)
|
||||
if stopTestRun is not None:
|
||||
stopTestRun()
|
||||
stopTime = time.perf_counter()
|
||||
timeTaken = stopTime - startTime
|
||||
result.printErrors()
|
||||
if hasattr(result, 'separator2'):
|
||||
self.stream.writeln(result.separator2)
|
||||
run = result.testsRun
|
||||
self.stream.writeln("Ran %d test%s in %.3fs" %
|
||||
(run, run != 1 and "s" or "", timeTaken))
|
||||
self.stream.writeln()
|
||||
|
||||
expectedFails = unexpectedSuccesses = skipped = 0
|
||||
try:
|
||||
results = map(len, (result.expectedFailures,
|
||||
result.unexpectedSuccesses,
|
||||
result.skipped))
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
expectedFails, unexpectedSuccesses, skipped = results
|
||||
|
||||
infos = []
|
||||
if not result.wasSuccessful():
|
||||
self.stream.write("FAILED")
|
||||
failed, errored = len(result.failures), len(result.errors)
|
||||
if failed:
|
||||
infos.append("failures=%d" % failed)
|
||||
if errored:
|
||||
infos.append("errors=%d" % errored)
|
||||
else:
|
||||
self.stream.write("OK")
|
||||
if skipped:
|
||||
infos.append("skipped=%d" % skipped)
|
||||
if expectedFails:
|
||||
infos.append("expected failures=%d" % expectedFails)
|
||||
if unexpectedSuccesses:
|
||||
infos.append("unexpected successes=%d" % unexpectedSuccesses)
|
||||
if infos:
|
||||
self.stream.writeln(" (%s)" % (", ".join(infos),))
|
||||
else:
|
||||
self.stream.write("\n")
|
||||
self.stream.flush()
|
||||
return result
|
||||
71
.CondaPkg/env/Lib/unittest/signals.py
vendored
71
.CondaPkg/env/Lib/unittest/signals.py
vendored
@@ -1,71 +0,0 @@
|
||||
import signal
|
||||
import weakref
|
||||
|
||||
from functools import wraps
|
||||
|
||||
__unittest = True
|
||||
|
||||
|
||||
class _InterruptHandler(object):
|
||||
def __init__(self, default_handler):
|
||||
self.called = False
|
||||
self.original_handler = default_handler
|
||||
if isinstance(default_handler, int):
|
||||
if default_handler == signal.SIG_DFL:
|
||||
# Pretend it's signal.default_int_handler instead.
|
||||
default_handler = signal.default_int_handler
|
||||
elif default_handler == signal.SIG_IGN:
|
||||
# Not quite the same thing as SIG_IGN, but the closest we
|
||||
# can make it: do nothing.
|
||||
def default_handler(unused_signum, unused_frame):
|
||||
pass
|
||||
else:
|
||||
raise TypeError("expected SIGINT signal handler to be "
|
||||
"signal.SIG_IGN, signal.SIG_DFL, or a "
|
||||
"callable object")
|
||||
self.default_handler = default_handler
|
||||
|
||||
def __call__(self, signum, frame):
|
||||
installed_handler = signal.getsignal(signal.SIGINT)
|
||||
if installed_handler is not self:
|
||||
# if we aren't the installed handler, then delegate immediately
|
||||
# to the default handler
|
||||
self.default_handler(signum, frame)
|
||||
|
||||
if self.called:
|
||||
self.default_handler(signum, frame)
|
||||
self.called = True
|
||||
for result in _results.keys():
|
||||
result.stop()
|
||||
|
||||
_results = weakref.WeakKeyDictionary()
|
||||
def registerResult(result):
|
||||
_results[result] = 1
|
||||
|
||||
def removeResult(result):
|
||||
return bool(_results.pop(result, None))
|
||||
|
||||
_interrupt_handler = None
|
||||
def installHandler():
|
||||
global _interrupt_handler
|
||||
if _interrupt_handler is None:
|
||||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
_interrupt_handler = _InterruptHandler(default_handler)
|
||||
signal.signal(signal.SIGINT, _interrupt_handler)
|
||||
|
||||
|
||||
def removeHandler(method=None):
|
||||
if method is not None:
|
||||
@wraps(method)
|
||||
def inner(*args, **kwargs):
|
||||
initial = signal.getsignal(signal.SIGINT)
|
||||
removeHandler()
|
||||
try:
|
||||
return method(*args, **kwargs)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, initial)
|
||||
return inner
|
||||
|
||||
global _interrupt_handler
|
||||
if _interrupt_handler is not None:
|
||||
signal.signal(signal.SIGINT, _interrupt_handler.original_handler)
|
||||
379
.CondaPkg/env/Lib/unittest/suite.py
vendored
379
.CondaPkg/env/Lib/unittest/suite.py
vendored
@@ -1,379 +0,0 @@
|
||||
"""TestSuite"""
|
||||
|
||||
import sys
|
||||
|
||||
from . import case
|
||||
from . import util
|
||||
|
||||
__unittest = True
|
||||
|
||||
|
||||
def _call_if_exists(parent, attr):
|
||||
func = getattr(parent, attr, lambda: None)
|
||||
func()
|
||||
|
||||
|
||||
class BaseTestSuite(object):
|
||||
"""A simple test suite that doesn't provide class or module shared fixtures.
|
||||
"""
|
||||
_cleanup = True
|
||||
|
||||
def __init__(self, tests=()):
|
||||
self._tests = []
|
||||
self._removed_tests = 0
|
||||
self.addTests(tests)
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s tests=%s>" % (util.strclass(self.__class__), list(self))
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, self.__class__):
|
||||
return NotImplemented
|
||||
return list(self) == list(other)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._tests)
|
||||
|
||||
def countTestCases(self):
|
||||
cases = self._removed_tests
|
||||
for test in self:
|
||||
if test:
|
||||
cases += test.countTestCases()
|
||||
return cases
|
||||
|
||||
def addTest(self, test):
|
||||
# sanity checks
|
||||
if not callable(test):
|
||||
raise TypeError("{} is not callable".format(repr(test)))
|
||||
if isinstance(test, type) and issubclass(test,
|
||||
(case.TestCase, TestSuite)):
|
||||
raise TypeError("TestCases and TestSuites must be instantiated "
|
||||
"before passing them to addTest()")
|
||||
self._tests.append(test)
|
||||
|
||||
def addTests(self, tests):
|
||||
if isinstance(tests, str):
|
||||
raise TypeError("tests must be an iterable of tests, not a string")
|
||||
for test in tests:
|
||||
self.addTest(test)
|
||||
|
||||
def run(self, result):
|
||||
for index, test in enumerate(self):
|
||||
if result.shouldStop:
|
||||
break
|
||||
test(result)
|
||||
if self._cleanup:
|
||||
self._removeTestAtIndex(index)
|
||||
return result
|
||||
|
||||
def _removeTestAtIndex(self, index):
|
||||
"""Stop holding a reference to the TestCase at index."""
|
||||
try:
|
||||
test = self._tests[index]
|
||||
except TypeError:
|
||||
# support for suite implementations that have overridden self._tests
|
||||
pass
|
||||
else:
|
||||
# Some unittest tests add non TestCase/TestSuite objects to
|
||||
# the suite.
|
||||
if hasattr(test, 'countTestCases'):
|
||||
self._removed_tests += test.countTestCases()
|
||||
self._tests[index] = None
|
||||
|
||||
def __call__(self, *args, **kwds):
|
||||
return self.run(*args, **kwds)
|
||||
|
||||
def debug(self):
|
||||
"""Run the tests without collecting errors in a TestResult"""
|
||||
for test in self:
|
||||
test.debug()
|
||||
|
||||
|
||||
class TestSuite(BaseTestSuite):
|
||||
"""A test suite is a composite test consisting of a number of TestCases.
|
||||
|
||||
For use, create an instance of TestSuite, then add test case instances.
|
||||
When all tests have been added, the suite can be passed to a test
|
||||
runner, such as TextTestRunner. It will run the individual test cases
|
||||
in the order in which they were added, aggregating the results. When
|
||||
subclassing, do not forget to call the base class constructor.
|
||||
"""
|
||||
|
||||
def run(self, result, debug=False):
|
||||
topLevel = False
|
||||
if getattr(result, '_testRunEntered', False) is False:
|
||||
result._testRunEntered = topLevel = True
|
||||
|
||||
for index, test in enumerate(self):
|
||||
if result.shouldStop:
|
||||
break
|
||||
|
||||
if _isnotsuite(test):
|
||||
self._tearDownPreviousClass(test, result)
|
||||
self._handleModuleFixture(test, result)
|
||||
self._handleClassSetUp(test, result)
|
||||
result._previousTestClass = test.__class__
|
||||
|
||||
if (getattr(test.__class__, '_classSetupFailed', False) or
|
||||
getattr(result, '_moduleSetUpFailed', False)):
|
||||
continue
|
||||
|
||||
if not debug:
|
||||
test(result)
|
||||
else:
|
||||
test.debug()
|
||||
|
||||
if self._cleanup:
|
||||
self._removeTestAtIndex(index)
|
||||
|
||||
if topLevel:
|
||||
self._tearDownPreviousClass(None, result)
|
||||
self._handleModuleTearDown(result)
|
||||
result._testRunEntered = False
|
||||
return result
|
||||
|
||||
def debug(self):
|
||||
"""Run the tests without collecting errors in a TestResult"""
|
||||
debug = _DebugResult()
|
||||
self.run(debug, True)
|
||||
|
||||
################################
|
||||
|
||||
def _handleClassSetUp(self, test, result):
|
||||
previousClass = getattr(result, '_previousTestClass', None)
|
||||
currentClass = test.__class__
|
||||
if currentClass == previousClass:
|
||||
return
|
||||
if result._moduleSetUpFailed:
|
||||
return
|
||||
if getattr(currentClass, "__unittest_skip__", False):
|
||||
return
|
||||
|
||||
failed = False
|
||||
try:
|
||||
currentClass._classSetupFailed = False
|
||||
except TypeError:
|
||||
# test may actually be a function
|
||||
# so its class will be a builtin-type
|
||||
pass
|
||||
|
||||
setUpClass = getattr(currentClass, 'setUpClass', None)
|
||||
doClassCleanups = getattr(currentClass, 'doClassCleanups', None)
|
||||
if setUpClass is not None:
|
||||
_call_if_exists(result, '_setupStdout')
|
||||
try:
|
||||
try:
|
||||
setUpClass()
|
||||
except Exception as e:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise
|
||||
failed = True
|
||||
try:
|
||||
currentClass._classSetupFailed = True
|
||||
except TypeError:
|
||||
pass
|
||||
className = util.strclass(currentClass)
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'setUpClass',
|
||||
className)
|
||||
if failed and doClassCleanups is not None:
|
||||
doClassCleanups()
|
||||
for exc_info in currentClass.tearDown_exceptions:
|
||||
self._createClassOrModuleLevelException(
|
||||
result, exc_info[1], 'setUpClass', className,
|
||||
info=exc_info)
|
||||
finally:
|
||||
_call_if_exists(result, '_restoreStdout')
|
||||
|
||||
def _get_previous_module(self, result):
|
||||
previousModule = None
|
||||
previousClass = getattr(result, '_previousTestClass', None)
|
||||
if previousClass is not None:
|
||||
previousModule = previousClass.__module__
|
||||
return previousModule
|
||||
|
||||
|
||||
def _handleModuleFixture(self, test, result):
|
||||
previousModule = self._get_previous_module(result)
|
||||
currentModule = test.__class__.__module__
|
||||
if currentModule == previousModule:
|
||||
return
|
||||
|
||||
self._handleModuleTearDown(result)
|
||||
|
||||
|
||||
result._moduleSetUpFailed = False
|
||||
try:
|
||||
module = sys.modules[currentModule]
|
||||
except KeyError:
|
||||
return
|
||||
setUpModule = getattr(module, 'setUpModule', None)
|
||||
if setUpModule is not None:
|
||||
_call_if_exists(result, '_setupStdout')
|
||||
try:
|
||||
try:
|
||||
setUpModule()
|
||||
except Exception as e:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise
|
||||
result._moduleSetUpFailed = True
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'setUpModule',
|
||||
currentModule)
|
||||
if result._moduleSetUpFailed:
|
||||
try:
|
||||
case.doModuleCleanups()
|
||||
except Exception as e:
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'setUpModule',
|
||||
currentModule)
|
||||
finally:
|
||||
_call_if_exists(result, '_restoreStdout')
|
||||
|
||||
def _createClassOrModuleLevelException(self, result, exc, method_name,
|
||||
parent, info=None):
|
||||
errorName = f'{method_name} ({parent})'
|
||||
self._addClassOrModuleLevelException(result, exc, errorName, info)
|
||||
|
||||
def _addClassOrModuleLevelException(self, result, exception, errorName,
|
||||
info=None):
|
||||
error = _ErrorHolder(errorName)
|
||||
addSkip = getattr(result, 'addSkip', None)
|
||||
if addSkip is not None and isinstance(exception, case.SkipTest):
|
||||
addSkip(error, str(exception))
|
||||
else:
|
||||
if not info:
|
||||
result.addError(error, sys.exc_info())
|
||||
else:
|
||||
result.addError(error, info)
|
||||
|
||||
def _handleModuleTearDown(self, result):
|
||||
previousModule = self._get_previous_module(result)
|
||||
if previousModule is None:
|
||||
return
|
||||
if result._moduleSetUpFailed:
|
||||
return
|
||||
|
||||
try:
|
||||
module = sys.modules[previousModule]
|
||||
except KeyError:
|
||||
return
|
||||
|
||||
_call_if_exists(result, '_setupStdout')
|
||||
try:
|
||||
tearDownModule = getattr(module, 'tearDownModule', None)
|
||||
if tearDownModule is not None:
|
||||
try:
|
||||
tearDownModule()
|
||||
except Exception as e:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'tearDownModule',
|
||||
previousModule)
|
||||
try:
|
||||
case.doModuleCleanups()
|
||||
except Exception as e:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'tearDownModule',
|
||||
previousModule)
|
||||
finally:
|
||||
_call_if_exists(result, '_restoreStdout')
|
||||
|
||||
def _tearDownPreviousClass(self, test, result):
|
||||
previousClass = getattr(result, '_previousTestClass', None)
|
||||
currentClass = test.__class__
|
||||
if currentClass == previousClass or previousClass is None:
|
||||
return
|
||||
if getattr(previousClass, '_classSetupFailed', False):
|
||||
return
|
||||
if getattr(result, '_moduleSetUpFailed', False):
|
||||
return
|
||||
if getattr(previousClass, "__unittest_skip__", False):
|
||||
return
|
||||
|
||||
tearDownClass = getattr(previousClass, 'tearDownClass', None)
|
||||
doClassCleanups = getattr(previousClass, 'doClassCleanups', None)
|
||||
if tearDownClass is None and doClassCleanups is None:
|
||||
return
|
||||
|
||||
_call_if_exists(result, '_setupStdout')
|
||||
try:
|
||||
if tearDownClass is not None:
|
||||
try:
|
||||
tearDownClass()
|
||||
except Exception as e:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise
|
||||
className = util.strclass(previousClass)
|
||||
self._createClassOrModuleLevelException(result, e,
|
||||
'tearDownClass',
|
||||
className)
|
||||
if doClassCleanups is not None:
|
||||
doClassCleanups()
|
||||
for exc_info in previousClass.tearDown_exceptions:
|
||||
if isinstance(result, _DebugResult):
|
||||
raise exc_info[1]
|
||||
className = util.strclass(previousClass)
|
||||
self._createClassOrModuleLevelException(result, exc_info[1],
|
||||
'tearDownClass',
|
||||
className,
|
||||
info=exc_info)
|
||||
finally:
|
||||
_call_if_exists(result, '_restoreStdout')
|
||||
|
||||
|
||||
class _ErrorHolder(object):
|
||||
"""
|
||||
Placeholder for a TestCase inside a result. As far as a TestResult
|
||||
is concerned, this looks exactly like a unit test. Used to insert
|
||||
arbitrary errors into a test suite run.
|
||||
"""
|
||||
# Inspired by the ErrorHolder from Twisted:
|
||||
# http://twistedmatrix.com/trac/browser/trunk/twisted/trial/runner.py
|
||||
|
||||
# attribute used by TestResult._exc_info_to_string
|
||||
failureException = None
|
||||
|
||||
def __init__(self, description):
|
||||
self.description = description
|
||||
|
||||
def id(self):
|
||||
return self.description
|
||||
|
||||
def shortDescription(self):
|
||||
return None
|
||||
|
||||
def __repr__(self):
|
||||
return "<ErrorHolder description=%r>" % (self.description,)
|
||||
|
||||
def __str__(self):
|
||||
return self.id()
|
||||
|
||||
def run(self, result):
|
||||
# could call result.addError(...) - but this test-like object
|
||||
# shouldn't be run anyway
|
||||
pass
|
||||
|
||||
def __call__(self, result):
|
||||
return self.run(result)
|
||||
|
||||
def countTestCases(self):
|
||||
return 0
|
||||
|
||||
def _isnotsuite(test):
|
||||
"A crude way to tell apart testcases and suites with duck-typing"
|
||||
try:
|
||||
iter(test)
|
||||
except TypeError:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class _DebugResult(object):
|
||||
"Used by the TestSuite to hold previous class when running in debug."
|
||||
_previousTestClass = None
|
||||
_moduleSetUpFailed = False
|
||||
shouldStop = False
|
||||
25
.CondaPkg/env/Lib/unittest/test/__init__.py
vendored
25
.CondaPkg/env/Lib/unittest/test/__init__.py
vendored
@@ -1,25 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
|
||||
here = os.path.dirname(__file__)
|
||||
loader = unittest.defaultTestLoader
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
for fn in os.listdir(here):
|
||||
if fn.startswith("test") and fn.endswith(".py"):
|
||||
modname = "unittest.test." + fn[:-3]
|
||||
try:
|
||||
__import__(modname)
|
||||
except unittest.SkipTest:
|
||||
continue
|
||||
module = sys.modules[modname]
|
||||
suite.addTest(loader.loadTestsFromModule(module))
|
||||
suite.addTest(loader.loadTestsFromName('unittest.test.testmock'))
|
||||
return suite
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(defaultTest="suite")
|
||||
18
.CondaPkg/env/Lib/unittest/test/__main__.py
vendored
18
.CondaPkg/env/Lib/unittest/test/__main__.py
vendored
@@ -1,18 +0,0 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
|
||||
def load_tests(loader, standard_tests, pattern):
|
||||
# top level directory cached on loader instance
|
||||
this_dir = os.path.dirname(__file__)
|
||||
pattern = pattern or "test_*.py"
|
||||
# We are inside unittest.test, so the top-level is two notches up
|
||||
top_level_dir = os.path.dirname(os.path.dirname(this_dir))
|
||||
package_tests = loader.discover(start_dir=this_dir, pattern=pattern,
|
||||
top_level_dir=top_level_dir)
|
||||
standard_tests.addTests(package_tests)
|
||||
return standard_tests
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,73 +0,0 @@
|
||||
# helper module for test_runner.Test_TextTestRunner.test_warnings
|
||||
|
||||
"""
|
||||
This module has a number of tests that raise different kinds of warnings.
|
||||
When the tests are run, the warnings are caught and their messages are printed
|
||||
to stdout. This module also accepts an arg that is then passed to
|
||||
unittest.main to affect the behavior of warnings.
|
||||
Test_TextTestRunner.test_warnings executes this script with different
|
||||
combinations of warnings args and -W flags and check that the output is correct.
|
||||
See #10535.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
def warnfun():
|
||||
warnings.warn('rw', RuntimeWarning)
|
||||
|
||||
class TestWarnings(unittest.TestCase):
|
||||
# unittest warnings will be printed at most once per type (max one message
|
||||
# for the fail* methods, and one for the assert* methods)
|
||||
def test_assert(self):
|
||||
self.assertEquals(2+2, 4)
|
||||
self.assertEquals(2*2, 4)
|
||||
self.assertEquals(2**2, 4)
|
||||
|
||||
def test_fail(self):
|
||||
self.failUnless(1)
|
||||
self.failUnless(True)
|
||||
|
||||
def test_other_unittest(self):
|
||||
self.assertAlmostEqual(2+2, 4)
|
||||
self.assertNotAlmostEqual(4+4, 2)
|
||||
|
||||
# these warnings are normally silenced, but they are printed in unittest
|
||||
def test_deprecation(self):
|
||||
warnings.warn('dw', DeprecationWarning)
|
||||
warnings.warn('dw', DeprecationWarning)
|
||||
warnings.warn('dw', DeprecationWarning)
|
||||
|
||||
def test_import(self):
|
||||
warnings.warn('iw', ImportWarning)
|
||||
warnings.warn('iw', ImportWarning)
|
||||
warnings.warn('iw', ImportWarning)
|
||||
|
||||
# user warnings should always be printed
|
||||
def test_warning(self):
|
||||
warnings.warn('uw')
|
||||
warnings.warn('uw')
|
||||
warnings.warn('uw')
|
||||
|
||||
# these warnings come from the same place; they will be printed
|
||||
# only once by default or three times if the 'always' filter is used
|
||||
def test_function(self):
|
||||
|
||||
warnfun()
|
||||
warnfun()
|
||||
warnfun()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with warnings.catch_warnings(record=True) as ws:
|
||||
# if an arg is provided pass it to unittest.main as 'warnings'
|
||||
if len(sys.argv) == 2:
|
||||
unittest.main(exit=False, warnings=sys.argv.pop())
|
||||
else:
|
||||
unittest.main(exit=False)
|
||||
|
||||
# print all the warning messages collected
|
||||
for w in ws:
|
||||
print(w.message)
|
||||
1
.CondaPkg/env/Lib/unittest/test/dummy.py
vendored
1
.CondaPkg/env/Lib/unittest/test/dummy.py
vendored
@@ -1 +0,0 @@
|
||||
# Empty module for testing the loading of modules
|
||||
138
.CondaPkg/env/Lib/unittest/test/support.py
vendored
138
.CondaPkg/env/Lib/unittest/test/support.py
vendored
@@ -1,138 +0,0 @@
|
||||
import unittest
|
||||
|
||||
|
||||
class TestEquality(object):
|
||||
"""Used as a mixin for TestCase"""
|
||||
|
||||
# Check for a valid __eq__ implementation
|
||||
def test_eq(self):
|
||||
for obj_1, obj_2 in self.eq_pairs:
|
||||
self.assertEqual(obj_1, obj_2)
|
||||
self.assertEqual(obj_2, obj_1)
|
||||
|
||||
# Check for a valid __ne__ implementation
|
||||
def test_ne(self):
|
||||
for obj_1, obj_2 in self.ne_pairs:
|
||||
self.assertNotEqual(obj_1, obj_2)
|
||||
self.assertNotEqual(obj_2, obj_1)
|
||||
|
||||
class TestHashing(object):
|
||||
"""Used as a mixin for TestCase"""
|
||||
|
||||
# Check for a valid __hash__ implementation
|
||||
def test_hash(self):
|
||||
for obj_1, obj_2 in self.eq_pairs:
|
||||
try:
|
||||
if not hash(obj_1) == hash(obj_2):
|
||||
self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
|
||||
except Exception as e:
|
||||
self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
|
||||
|
||||
for obj_1, obj_2 in self.ne_pairs:
|
||||
try:
|
||||
if hash(obj_1) == hash(obj_2):
|
||||
self.fail("%s and %s hash equal, but shouldn't" %
|
||||
(obj_1, obj_2))
|
||||
except Exception as e:
|
||||
self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
|
||||
|
||||
|
||||
class _BaseLoggingResult(unittest.TestResult):
|
||||
def __init__(self, log):
|
||||
self._events = log
|
||||
super().__init__()
|
||||
|
||||
def startTest(self, test):
|
||||
self._events.append('startTest')
|
||||
super().startTest(test)
|
||||
|
||||
def startTestRun(self):
|
||||
self._events.append('startTestRun')
|
||||
super().startTestRun()
|
||||
|
||||
def stopTest(self, test):
|
||||
self._events.append('stopTest')
|
||||
super().stopTest(test)
|
||||
|
||||
def stopTestRun(self):
|
||||
self._events.append('stopTestRun')
|
||||
super().stopTestRun()
|
||||
|
||||
def addFailure(self, *args):
|
||||
self._events.append('addFailure')
|
||||
super().addFailure(*args)
|
||||
|
||||
def addSuccess(self, *args):
|
||||
self._events.append('addSuccess')
|
||||
super().addSuccess(*args)
|
||||
|
||||
def addError(self, *args):
|
||||
self._events.append('addError')
|
||||
super().addError(*args)
|
||||
|
||||
def addSkip(self, *args):
|
||||
self._events.append('addSkip')
|
||||
super().addSkip(*args)
|
||||
|
||||
def addExpectedFailure(self, *args):
|
||||
self._events.append('addExpectedFailure')
|
||||
super().addExpectedFailure(*args)
|
||||
|
||||
def addUnexpectedSuccess(self, *args):
|
||||
self._events.append('addUnexpectedSuccess')
|
||||
super().addUnexpectedSuccess(*args)
|
||||
|
||||
|
||||
class LegacyLoggingResult(_BaseLoggingResult):
|
||||
"""
|
||||
A legacy TestResult implementation, without an addSubTest method,
|
||||
which records its method calls.
|
||||
"""
|
||||
|
||||
@property
|
||||
def addSubTest(self):
|
||||
raise AttributeError
|
||||
|
||||
|
||||
class LoggingResult(_BaseLoggingResult):
|
||||
"""
|
||||
A TestResult implementation which records its method calls.
|
||||
"""
|
||||
|
||||
def addSubTest(self, test, subtest, err):
|
||||
if err is None:
|
||||
self._events.append('addSubTestSuccess')
|
||||
else:
|
||||
self._events.append('addSubTestFailure')
|
||||
super().addSubTest(test, subtest, err)
|
||||
|
||||
|
||||
class ResultWithNoStartTestRunStopTestRun(object):
|
||||
"""An object honouring TestResult before startTestRun/stopTestRun."""
|
||||
|
||||
def __init__(self):
|
||||
self.failures = []
|
||||
self.errors = []
|
||||
self.testsRun = 0
|
||||
self.skipped = []
|
||||
self.expectedFailures = []
|
||||
self.unexpectedSuccesses = []
|
||||
self.shouldStop = False
|
||||
|
||||
def startTest(self, test):
|
||||
pass
|
||||
|
||||
def stopTest(self, test):
|
||||
pass
|
||||
|
||||
def addError(self, test):
|
||||
pass
|
||||
|
||||
def addFailure(self, test):
|
||||
pass
|
||||
|
||||
def addSuccess(self, test):
|
||||
pass
|
||||
|
||||
def wasSuccessful(self):
|
||||
return True
|
||||
416
.CondaPkg/env/Lib/unittest/test/test_assertions.py
vendored
416
.CondaPkg/env/Lib/unittest/test/test_assertions.py
vendored
@@ -1,416 +0,0 @@
|
||||
import datetime
|
||||
import warnings
|
||||
import weakref
|
||||
import unittest
|
||||
from test.support import gc_collect
|
||||
from itertools import product
|
||||
|
||||
|
||||
class Test_Assertions(unittest.TestCase):
|
||||
def test_AlmostEqual(self):
|
||||
self.assertAlmostEqual(1.00000001, 1.0)
|
||||
self.assertNotAlmostEqual(1.0000001, 1.0)
|
||||
self.assertRaises(self.failureException,
|
||||
self.assertAlmostEqual, 1.0000001, 1.0)
|
||||
self.assertRaises(self.failureException,
|
||||
self.assertNotAlmostEqual, 1.00000001, 1.0)
|
||||
|
||||
self.assertAlmostEqual(1.1, 1.0, places=0)
|
||||
self.assertRaises(self.failureException,
|
||||
self.assertAlmostEqual, 1.1, 1.0, places=1)
|
||||
|
||||
self.assertAlmostEqual(0, .1+.1j, places=0)
|
||||
self.assertNotAlmostEqual(0, .1+.1j, places=1)
|
||||
self.assertRaises(self.failureException,
|
||||
self.assertAlmostEqual, 0, .1+.1j, places=1)
|
||||
self.assertRaises(self.failureException,
|
||||
self.assertNotAlmostEqual, 0, .1+.1j, places=0)
|
||||
|
||||
self.assertAlmostEqual(float('inf'), float('inf'))
|
||||
self.assertRaises(self.failureException, self.assertNotAlmostEqual,
|
||||
float('inf'), float('inf'))
|
||||
|
||||
def test_AmostEqualWithDelta(self):
|
||||
self.assertAlmostEqual(1.1, 1.0, delta=0.5)
|
||||
self.assertAlmostEqual(1.0, 1.1, delta=0.5)
|
||||
self.assertNotAlmostEqual(1.1, 1.0, delta=0.05)
|
||||
self.assertNotAlmostEqual(1.0, 1.1, delta=0.05)
|
||||
|
||||
self.assertAlmostEqual(1.0, 1.0, delta=0.5)
|
||||
self.assertRaises(self.failureException, self.assertNotAlmostEqual,
|
||||
1.0, 1.0, delta=0.5)
|
||||
|
||||
self.assertRaises(self.failureException, self.assertAlmostEqual,
|
||||
1.1, 1.0, delta=0.05)
|
||||
self.assertRaises(self.failureException, self.assertNotAlmostEqual,
|
||||
1.1, 1.0, delta=0.5)
|
||||
|
||||
self.assertRaises(TypeError, self.assertAlmostEqual,
|
||||
1.1, 1.0, places=2, delta=2)
|
||||
self.assertRaises(TypeError, self.assertNotAlmostEqual,
|
||||
1.1, 1.0, places=2, delta=2)
|
||||
|
||||
first = datetime.datetime.now()
|
||||
second = first + datetime.timedelta(seconds=10)
|
||||
self.assertAlmostEqual(first, second,
|
||||
delta=datetime.timedelta(seconds=20))
|
||||
self.assertNotAlmostEqual(first, second,
|
||||
delta=datetime.timedelta(seconds=5))
|
||||
|
||||
def test_assertRaises(self):
|
||||
def _raise(e):
|
||||
raise e
|
||||
self.assertRaises(KeyError, _raise, KeyError)
|
||||
self.assertRaises(KeyError, _raise, KeyError("key"))
|
||||
try:
|
||||
self.assertRaises(KeyError, lambda: None)
|
||||
except self.failureException as e:
|
||||
self.assertIn("KeyError not raised", str(e))
|
||||
else:
|
||||
self.fail("assertRaises() didn't fail")
|
||||
try:
|
||||
self.assertRaises(KeyError, _raise, ValueError)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("assertRaises() didn't let exception pass through")
|
||||
with self.assertRaises(KeyError) as cm:
|
||||
try:
|
||||
raise KeyError
|
||||
except Exception as e:
|
||||
exc = e
|
||||
raise
|
||||
self.assertIs(cm.exception, exc)
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
raise KeyError("key")
|
||||
try:
|
||||
with self.assertRaises(KeyError):
|
||||
pass
|
||||
except self.failureException as e:
|
||||
self.assertIn("KeyError not raised", str(e))
|
||||
else:
|
||||
self.fail("assertRaises() didn't fail")
|
||||
try:
|
||||
with self.assertRaises(KeyError):
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("assertRaises() didn't let exception pass through")
|
||||
|
||||
def test_assertRaises_frames_survival(self):
|
||||
# Issue #9815: assertRaises should avoid keeping local variables
|
||||
# in a traceback alive.
|
||||
class A:
|
||||
pass
|
||||
wr = None
|
||||
|
||||
class Foo(unittest.TestCase):
|
||||
|
||||
def foo(self):
|
||||
nonlocal wr
|
||||
a = A()
|
||||
wr = weakref.ref(a)
|
||||
try:
|
||||
raise OSError
|
||||
except OSError:
|
||||
raise ValueError
|
||||
|
||||
def test_functional(self):
|
||||
self.assertRaises(ValueError, self.foo)
|
||||
|
||||
def test_with(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.foo()
|
||||
|
||||
Foo("test_functional").run()
|
||||
gc_collect() # For PyPy or other GCs.
|
||||
self.assertIsNone(wr())
|
||||
Foo("test_with").run()
|
||||
gc_collect() # For PyPy or other GCs.
|
||||
self.assertIsNone(wr())
|
||||
|
||||
def testAssertNotRegex(self):
|
||||
self.assertNotRegex('Ala ma kota', r'r+')
|
||||
try:
|
||||
self.assertNotRegex('Ala ma kota', r'k.t', 'Message')
|
||||
except self.failureException as e:
|
||||
self.assertIn('Message', e.args[0])
|
||||
else:
|
||||
self.fail('assertNotRegex should have failed.')
|
||||
|
||||
|
||||
class TestLongMessage(unittest.TestCase):
|
||||
"""Test that the individual asserts honour longMessage.
|
||||
This actually tests all the message behaviour for
|
||||
asserts that use longMessage."""
|
||||
|
||||
def setUp(self):
|
||||
class TestableTestFalse(unittest.TestCase):
|
||||
longMessage = False
|
||||
failureException = self.failureException
|
||||
|
||||
def testTest(self):
|
||||
pass
|
||||
|
||||
class TestableTestTrue(unittest.TestCase):
|
||||
longMessage = True
|
||||
failureException = self.failureException
|
||||
|
||||
def testTest(self):
|
||||
pass
|
||||
|
||||
self.testableTrue = TestableTestTrue('testTest')
|
||||
self.testableFalse = TestableTestFalse('testTest')
|
||||
|
||||
def testDefault(self):
|
||||
self.assertTrue(unittest.TestCase.longMessage)
|
||||
|
||||
def test_formatMsg(self):
|
||||
self.assertEqual(self.testableFalse._formatMessage(None, "foo"), "foo")
|
||||
self.assertEqual(self.testableFalse._formatMessage("foo", "bar"), "foo")
|
||||
|
||||
self.assertEqual(self.testableTrue._formatMessage(None, "foo"), "foo")
|
||||
self.assertEqual(self.testableTrue._formatMessage("foo", "bar"), "bar : foo")
|
||||
|
||||
# This blows up if _formatMessage uses string concatenation
|
||||
self.testableTrue._formatMessage(object(), 'foo')
|
||||
|
||||
def test_formatMessage_unicode_error(self):
|
||||
one = ''.join(chr(i) for i in range(255))
|
||||
# this used to cause a UnicodeDecodeError constructing msg
|
||||
self.testableTrue._formatMessage(one, '\uFFFD')
|
||||
|
||||
def assertMessages(self, methodName, args, errors):
|
||||
"""
|
||||
Check that methodName(*args) raises the correct error messages.
|
||||
errors should be a list of 4 regex that match the error when:
|
||||
1) longMessage = False and no msg passed;
|
||||
2) longMessage = False and msg passed;
|
||||
3) longMessage = True and no msg passed;
|
||||
4) longMessage = True and msg passed;
|
||||
"""
|
||||
def getMethod(i):
|
||||
useTestableFalse = i < 2
|
||||
if useTestableFalse:
|
||||
test = self.testableFalse
|
||||
else:
|
||||
test = self.testableTrue
|
||||
return getattr(test, methodName)
|
||||
|
||||
for i, expected_regex in enumerate(errors):
|
||||
testMethod = getMethod(i)
|
||||
kwargs = {}
|
||||
withMsg = i % 2
|
||||
if withMsg:
|
||||
kwargs = {"msg": "oops"}
|
||||
|
||||
with self.assertRaisesRegex(self.failureException,
|
||||
expected_regex=expected_regex):
|
||||
testMethod(*args, **kwargs)
|
||||
|
||||
def testAssertTrue(self):
|
||||
self.assertMessages('assertTrue', (False,),
|
||||
["^False is not true$", "^oops$", "^False is not true$",
|
||||
"^False is not true : oops$"])
|
||||
|
||||
def testAssertFalse(self):
|
||||
self.assertMessages('assertFalse', (True,),
|
||||
["^True is not false$", "^oops$", "^True is not false$",
|
||||
"^True is not false : oops$"])
|
||||
|
||||
def testNotEqual(self):
|
||||
self.assertMessages('assertNotEqual', (1, 1),
|
||||
["^1 == 1$", "^oops$", "^1 == 1$",
|
||||
"^1 == 1 : oops$"])
|
||||
|
||||
def testAlmostEqual(self):
|
||||
self.assertMessages(
|
||||
'assertAlmostEqual', (1, 2),
|
||||
[r"^1 != 2 within 7 places \(1 difference\)$", "^oops$",
|
||||
r"^1 != 2 within 7 places \(1 difference\)$",
|
||||
r"^1 != 2 within 7 places \(1 difference\) : oops$"])
|
||||
|
||||
def testNotAlmostEqual(self):
|
||||
self.assertMessages('assertNotAlmostEqual', (1, 1),
|
||||
["^1 == 1 within 7 places$", "^oops$",
|
||||
"^1 == 1 within 7 places$", "^1 == 1 within 7 places : oops$"])
|
||||
|
||||
def test_baseAssertEqual(self):
|
||||
self.assertMessages('_baseAssertEqual', (1, 2),
|
||||
["^1 != 2$", "^oops$", "^1 != 2$", "^1 != 2 : oops$"])
|
||||
|
||||
def testAssertSequenceEqual(self):
|
||||
# Error messages are multiline so not testing on full message
|
||||
# assertTupleEqual and assertListEqual delegate to this method
|
||||
self.assertMessages('assertSequenceEqual', ([], [None]),
|
||||
[r"\+ \[None\]$", "^oops$", r"\+ \[None\]$",
|
||||
r"\+ \[None\] : oops$"])
|
||||
|
||||
def testAssertSetEqual(self):
|
||||
self.assertMessages('assertSetEqual', (set(), set([None])),
|
||||
["None$", "^oops$", "None$",
|
||||
"None : oops$"])
|
||||
|
||||
def testAssertIn(self):
|
||||
self.assertMessages('assertIn', (None, []),
|
||||
[r'^None not found in \[\]$', "^oops$",
|
||||
r'^None not found in \[\]$',
|
||||
r'^None not found in \[\] : oops$'])
|
||||
|
||||
def testAssertNotIn(self):
|
||||
self.assertMessages('assertNotIn', (None, [None]),
|
||||
[r'^None unexpectedly found in \[None\]$', "^oops$",
|
||||
r'^None unexpectedly found in \[None\]$',
|
||||
r'^None unexpectedly found in \[None\] : oops$'])
|
||||
|
||||
def testAssertDictEqual(self):
|
||||
self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),
|
||||
[r"\+ \{'key': 'value'\}$", "^oops$",
|
||||
r"\+ \{'key': 'value'\}$",
|
||||
r"\+ \{'key': 'value'\} : oops$"])
|
||||
|
||||
def testAssertDictContainsSubset(self):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
|
||||
self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
|
||||
["^Missing: 'key'$", "^oops$",
|
||||
"^Missing: 'key'$",
|
||||
"^Missing: 'key' : oops$"])
|
||||
|
||||
def testAssertMultiLineEqual(self):
|
||||
self.assertMessages('assertMultiLineEqual', ("", "foo"),
|
||||
[r"\+ foo$", "^oops$",
|
||||
r"\+ foo$",
|
||||
r"\+ foo : oops$"])
|
||||
|
||||
def testAssertLess(self):
|
||||
self.assertMessages('assertLess', (2, 1),
|
||||
["^2 not less than 1$", "^oops$",
|
||||
"^2 not less than 1$", "^2 not less than 1 : oops$"])
|
||||
|
||||
def testAssertLessEqual(self):
|
||||
self.assertMessages('assertLessEqual', (2, 1),
|
||||
["^2 not less than or equal to 1$", "^oops$",
|
||||
"^2 not less than or equal to 1$",
|
||||
"^2 not less than or equal to 1 : oops$"])
|
||||
|
||||
def testAssertGreater(self):
|
||||
self.assertMessages('assertGreater', (1, 2),
|
||||
["^1 not greater than 2$", "^oops$",
|
||||
"^1 not greater than 2$",
|
||||
"^1 not greater than 2 : oops$"])
|
||||
|
||||
def testAssertGreaterEqual(self):
|
||||
self.assertMessages('assertGreaterEqual', (1, 2),
|
||||
["^1 not greater than or equal to 2$", "^oops$",
|
||||
"^1 not greater than or equal to 2$",
|
||||
"^1 not greater than or equal to 2 : oops$"])
|
||||
|
||||
def testAssertIsNone(self):
|
||||
self.assertMessages('assertIsNone', ('not None',),
|
||||
["^'not None' is not None$", "^oops$",
|
||||
"^'not None' is not None$",
|
||||
"^'not None' is not None : oops$"])
|
||||
|
||||
def testAssertIsNotNone(self):
|
||||
self.assertMessages('assertIsNotNone', (None,),
|
||||
["^unexpectedly None$", "^oops$",
|
||||
"^unexpectedly None$",
|
||||
"^unexpectedly None : oops$"])
|
||||
|
||||
def testAssertIs(self):
|
||||
self.assertMessages('assertIs', (None, 'foo'),
|
||||
["^None is not 'foo'$", "^oops$",
|
||||
"^None is not 'foo'$",
|
||||
"^None is not 'foo' : oops$"])
|
||||
|
||||
def testAssertIsNot(self):
|
||||
self.assertMessages('assertIsNot', (None, None),
|
||||
["^unexpectedly identical: None$", "^oops$",
|
||||
"^unexpectedly identical: None$",
|
||||
"^unexpectedly identical: None : oops$"])
|
||||
|
||||
def testAssertRegex(self):
|
||||
self.assertMessages('assertRegex', ('foo', 'bar'),
|
||||
["^Regex didn't match:",
|
||||
"^oops$",
|
||||
"^Regex didn't match:",
|
||||
"^Regex didn't match: (.*) : oops$"])
|
||||
|
||||
def testAssertNotRegex(self):
|
||||
self.assertMessages('assertNotRegex', ('foo', 'foo'),
|
||||
["^Regex matched:",
|
||||
"^oops$",
|
||||
"^Regex matched:",
|
||||
"^Regex matched: (.*) : oops$"])
|
||||
|
||||
|
||||
def assertMessagesCM(self, methodName, args, func, errors):
|
||||
"""
|
||||
Check that the correct error messages are raised while executing:
|
||||
with method(*args):
|
||||
func()
|
||||
*errors* should be a list of 4 regex that match the error when:
|
||||
1) longMessage = False and no msg passed;
|
||||
2) longMessage = False and msg passed;
|
||||
3) longMessage = True and no msg passed;
|
||||
4) longMessage = True and msg passed;
|
||||
"""
|
||||
p = product((self.testableFalse, self.testableTrue),
|
||||
({}, {"msg": "oops"}))
|
||||
for (cls, kwargs), err in zip(p, errors):
|
||||
method = getattr(cls, methodName)
|
||||
with self.assertRaisesRegex(cls.failureException, err):
|
||||
with method(*args, **kwargs) as cm:
|
||||
func()
|
||||
|
||||
def testAssertRaises(self):
|
||||
self.assertMessagesCM('assertRaises', (TypeError,), lambda: None,
|
||||
['^TypeError not raised$', '^oops$',
|
||||
'^TypeError not raised$',
|
||||
'^TypeError not raised : oops$'])
|
||||
|
||||
def testAssertRaisesRegex(self):
|
||||
# test error not raised
|
||||
self.assertMessagesCM('assertRaisesRegex', (TypeError, 'unused regex'),
|
||||
lambda: None,
|
||||
['^TypeError not raised$', '^oops$',
|
||||
'^TypeError not raised$',
|
||||
'^TypeError not raised : oops$'])
|
||||
# test error raised but with wrong message
|
||||
def raise_wrong_message():
|
||||
raise TypeError('foo')
|
||||
self.assertMessagesCM('assertRaisesRegex', (TypeError, 'regex'),
|
||||
raise_wrong_message,
|
||||
['^"regex" does not match "foo"$', '^oops$',
|
||||
'^"regex" does not match "foo"$',
|
||||
'^"regex" does not match "foo" : oops$'])
|
||||
|
||||
def testAssertWarns(self):
|
||||
self.assertMessagesCM('assertWarns', (UserWarning,), lambda: None,
|
||||
['^UserWarning not triggered$', '^oops$',
|
||||
'^UserWarning not triggered$',
|
||||
'^UserWarning not triggered : oops$'])
|
||||
|
||||
def testAssertWarnsRegex(self):
|
||||
# test error not raised
|
||||
self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'unused regex'),
|
||||
lambda: None,
|
||||
['^UserWarning not triggered$', '^oops$',
|
||||
'^UserWarning not triggered$',
|
||||
'^UserWarning not triggered : oops$'])
|
||||
# test warning raised but with wrong message
|
||||
def raise_wrong_message():
|
||||
warnings.warn('foo')
|
||||
self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'regex'),
|
||||
raise_wrong_message,
|
||||
['^"regex" does not match "foo"$', '^oops$',
|
||||
'^"regex" does not match "foo"$',
|
||||
'^"regex" does not match "foo" : oops$'])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
488
.CondaPkg/env/Lib/unittest/test/test_async_case.py
vendored
488
.CondaPkg/env/Lib/unittest/test/test_async_case.py
vendored
@@ -1,488 +0,0 @@
|
||||
import asyncio
|
||||
import contextvars
|
||||
import unittest
|
||||
from test import support
|
||||
|
||||
support.requires_working_socket(module=True)
|
||||
|
||||
|
||||
class MyException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def tearDownModule():
|
||||
asyncio.set_event_loop_policy(None)
|
||||
|
||||
|
||||
class TestCM:
|
||||
def __init__(self, ordering, enter_result=None):
|
||||
self.ordering = ordering
|
||||
self.enter_result = enter_result
|
||||
|
||||
async def __aenter__(self):
|
||||
self.ordering.append('enter')
|
||||
return self.enter_result
|
||||
|
||||
async def __aexit__(self, *exc_info):
|
||||
self.ordering.append('exit')
|
||||
|
||||
|
||||
class LacksEnterAndExit:
|
||||
pass
|
||||
class LacksEnter:
|
||||
async def __aexit__(self, *exc_info):
|
||||
pass
|
||||
class LacksExit:
|
||||
async def __aenter__(self):
|
||||
pass
|
||||
|
||||
|
||||
VAR = contextvars.ContextVar('VAR', default=())
|
||||
|
||||
|
||||
class TestAsyncCase(unittest.TestCase):
|
||||
maxDiff = None
|
||||
|
||||
def setUp(self):
|
||||
# Ensure that IsolatedAsyncioTestCase instances are destroyed before
|
||||
# starting a new event loop
|
||||
self.addCleanup(support.gc_collect)
|
||||
|
||||
def test_full_cycle(self):
|
||||
expected = ['setUp',
|
||||
'asyncSetUp',
|
||||
'test',
|
||||
'asyncTearDown',
|
||||
'tearDown',
|
||||
'cleanup6',
|
||||
'cleanup5',
|
||||
'cleanup4',
|
||||
'cleanup3',
|
||||
'cleanup2',
|
||||
'cleanup1']
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
self.assertEqual(events, [])
|
||||
events.append('setUp')
|
||||
VAR.set(VAR.get() + ('setUp',))
|
||||
self.addCleanup(self.on_cleanup1)
|
||||
self.addAsyncCleanup(self.on_cleanup2)
|
||||
|
||||
async def asyncSetUp(self):
|
||||
self.assertEqual(events, expected[:1])
|
||||
events.append('asyncSetUp')
|
||||
VAR.set(VAR.get() + ('asyncSetUp',))
|
||||
self.addCleanup(self.on_cleanup3)
|
||||
self.addAsyncCleanup(self.on_cleanup4)
|
||||
|
||||
async def test_func(self):
|
||||
self.assertEqual(events, expected[:2])
|
||||
events.append('test')
|
||||
VAR.set(VAR.get() + ('test',))
|
||||
self.addCleanup(self.on_cleanup5)
|
||||
self.addAsyncCleanup(self.on_cleanup6)
|
||||
|
||||
async def asyncTearDown(self):
|
||||
self.assertEqual(events, expected[:3])
|
||||
VAR.set(VAR.get() + ('asyncTearDown',))
|
||||
events.append('asyncTearDown')
|
||||
|
||||
def tearDown(self):
|
||||
self.assertEqual(events, expected[:4])
|
||||
events.append('tearDown')
|
||||
VAR.set(VAR.get() + ('tearDown',))
|
||||
|
||||
def on_cleanup1(self):
|
||||
self.assertEqual(events, expected[:10])
|
||||
events.append('cleanup1')
|
||||
VAR.set(VAR.get() + ('cleanup1',))
|
||||
nonlocal cvar
|
||||
cvar = VAR.get()
|
||||
|
||||
async def on_cleanup2(self):
|
||||
self.assertEqual(events, expected[:9])
|
||||
events.append('cleanup2')
|
||||
VAR.set(VAR.get() + ('cleanup2',))
|
||||
|
||||
def on_cleanup3(self):
|
||||
self.assertEqual(events, expected[:8])
|
||||
events.append('cleanup3')
|
||||
VAR.set(VAR.get() + ('cleanup3',))
|
||||
|
||||
async def on_cleanup4(self):
|
||||
self.assertEqual(events, expected[:7])
|
||||
events.append('cleanup4')
|
||||
VAR.set(VAR.get() + ('cleanup4',))
|
||||
|
||||
def on_cleanup5(self):
|
||||
self.assertEqual(events, expected[:6])
|
||||
events.append('cleanup5')
|
||||
VAR.set(VAR.get() + ('cleanup5',))
|
||||
|
||||
async def on_cleanup6(self):
|
||||
self.assertEqual(events, expected[:5])
|
||||
events.append('cleanup6')
|
||||
VAR.set(VAR.get() + ('cleanup6',))
|
||||
|
||||
events = []
|
||||
cvar = ()
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(result.errors, [])
|
||||
self.assertEqual(result.failures, [])
|
||||
self.assertEqual(events, expected)
|
||||
self.assertEqual(cvar, tuple(expected))
|
||||
|
||||
events = []
|
||||
cvar = ()
|
||||
test = Test("test_func")
|
||||
test.debug()
|
||||
self.assertEqual(events, expected)
|
||||
self.assertEqual(cvar, tuple(expected))
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, expected)
|
||||
self.assertEqual(cvar, tuple(expected))
|
||||
|
||||
def test_exception_in_setup(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self):
|
||||
events.append('asyncSetUp')
|
||||
self.addAsyncCleanup(self.on_cleanup)
|
||||
raise MyException()
|
||||
|
||||
async def test_func(self):
|
||||
events.append('test')
|
||||
|
||||
async def asyncTearDown(self):
|
||||
events.append('asyncTearDown')
|
||||
|
||||
async def on_cleanup(self):
|
||||
events.append('cleanup')
|
||||
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['asyncSetUp', 'cleanup'])
|
||||
self.assertIs(result.errors[0][0], test)
|
||||
self.assertIn('MyException', result.errors[0][1])
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
self.addCleanup(test._tearDownAsyncioRunner)
|
||||
try:
|
||||
test.debug()
|
||||
except MyException:
|
||||
pass
|
||||
else:
|
||||
self.fail('Expected a MyException exception')
|
||||
self.assertEqual(events, ['asyncSetUp'])
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, ['asyncSetUp', 'cleanup'])
|
||||
|
||||
def test_exception_in_test(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self):
|
||||
events.append('asyncSetUp')
|
||||
|
||||
async def test_func(self):
|
||||
events.append('test')
|
||||
self.addAsyncCleanup(self.on_cleanup)
|
||||
raise MyException()
|
||||
|
||||
async def asyncTearDown(self):
|
||||
events.append('asyncTearDown')
|
||||
|
||||
async def on_cleanup(self):
|
||||
events.append('cleanup')
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
|
||||
self.assertIs(result.errors[0][0], test)
|
||||
self.assertIn('MyException', result.errors[0][1])
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
self.addCleanup(test._tearDownAsyncioRunner)
|
||||
try:
|
||||
test.debug()
|
||||
except MyException:
|
||||
pass
|
||||
else:
|
||||
self.fail('Expected a MyException exception')
|
||||
self.assertEqual(events, ['asyncSetUp', 'test'])
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup'])
|
||||
|
||||
def test_exception_in_tear_down(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self):
|
||||
events.append('asyncSetUp')
|
||||
|
||||
async def test_func(self):
|
||||
events.append('test')
|
||||
self.addAsyncCleanup(self.on_cleanup)
|
||||
|
||||
async def asyncTearDown(self):
|
||||
events.append('asyncTearDown')
|
||||
raise MyException()
|
||||
|
||||
async def on_cleanup(self):
|
||||
events.append('cleanup')
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
|
||||
self.assertIs(result.errors[0][0], test)
|
||||
self.assertIn('MyException', result.errors[0][1])
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
self.addCleanup(test._tearDownAsyncioRunner)
|
||||
try:
|
||||
test.debug()
|
||||
except MyException:
|
||||
pass
|
||||
else:
|
||||
self.fail('Expected a MyException exception')
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown'])
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
|
||||
|
||||
def test_exception_in_tear_clean_up(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self):
|
||||
events.append('asyncSetUp')
|
||||
|
||||
async def test_func(self):
|
||||
events.append('test')
|
||||
self.addAsyncCleanup(self.on_cleanup1)
|
||||
self.addAsyncCleanup(self.on_cleanup2)
|
||||
|
||||
async def asyncTearDown(self):
|
||||
events.append('asyncTearDown')
|
||||
|
||||
async def on_cleanup1(self):
|
||||
events.append('cleanup1')
|
||||
raise MyException('some error')
|
||||
|
||||
async def on_cleanup2(self):
|
||||
events.append('cleanup2')
|
||||
raise MyException('other error')
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup2', 'cleanup1'])
|
||||
self.assertIs(result.errors[0][0], test)
|
||||
self.assertIn('MyException: other error', result.errors[0][1])
|
||||
self.assertIn('MyException: some error', result.errors[1][1])
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
self.addCleanup(test._tearDownAsyncioRunner)
|
||||
try:
|
||||
test.debug()
|
||||
except MyException:
|
||||
pass
|
||||
else:
|
||||
self.fail('Expected a MyException exception')
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup2'])
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup2', 'cleanup1'])
|
||||
|
||||
def test_deprecation_of_return_val_from_test(self):
|
||||
# Issue 41322 - deprecate return of value that is not None from a test
|
||||
class Nothing:
|
||||
def __eq__(self, o):
|
||||
return o is None
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test1(self):
|
||||
return 1
|
||||
async def test2(self):
|
||||
yield 1
|
||||
async def test3(self):
|
||||
return Nothing()
|
||||
|
||||
with self.assertWarns(DeprecationWarning) as w:
|
||||
Test('test1').run()
|
||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||
self.assertIn('test1', str(w.warning))
|
||||
self.assertEqual(w.filename, __file__)
|
||||
|
||||
with self.assertWarns(DeprecationWarning) as w:
|
||||
Test('test2').run()
|
||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||
self.assertIn('test2', str(w.warning))
|
||||
self.assertEqual(w.filename, __file__)
|
||||
|
||||
with self.assertWarns(DeprecationWarning) as w:
|
||||
Test('test3').run()
|
||||
self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
|
||||
self.assertIn('test3', str(w.warning))
|
||||
self.assertEqual(w.filename, __file__)
|
||||
|
||||
def test_cleanups_interleave_order(self):
|
||||
events = []
|
||||
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_func(self):
|
||||
self.addAsyncCleanup(self.on_sync_cleanup, 1)
|
||||
self.addAsyncCleanup(self.on_async_cleanup, 2)
|
||||
self.addAsyncCleanup(self.on_sync_cleanup, 3)
|
||||
self.addAsyncCleanup(self.on_async_cleanup, 4)
|
||||
|
||||
async def on_sync_cleanup(self, val):
|
||||
events.append(f'sync_cleanup {val}')
|
||||
|
||||
async def on_async_cleanup(self, val):
|
||||
events.append(f'async_cleanup {val}')
|
||||
|
||||
test = Test("test_func")
|
||||
test.run()
|
||||
self.assertEqual(events, ['async_cleanup 4',
|
||||
'sync_cleanup 3',
|
||||
'async_cleanup 2',
|
||||
'sync_cleanup 1'])
|
||||
|
||||
def test_base_exception_from_async_method(self):
|
||||
events = []
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_base(self):
|
||||
events.append("test_base")
|
||||
raise BaseException()
|
||||
events.append("not it")
|
||||
|
||||
async def test_no_err(self):
|
||||
events.append("test_no_err")
|
||||
|
||||
async def test_cancel(self):
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
test = Test("test_base")
|
||||
output = test.run()
|
||||
self.assertFalse(output.wasSuccessful())
|
||||
|
||||
test = Test("test_no_err")
|
||||
test.run()
|
||||
self.assertEqual(events, ['test_base', 'test_no_err'])
|
||||
|
||||
test = Test("test_cancel")
|
||||
output = test.run()
|
||||
self.assertFalse(output.wasSuccessful())
|
||||
|
||||
def test_cancellation_hanging_tasks(self):
|
||||
cancelled = False
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_leaking_task(self):
|
||||
async def coro():
|
||||
nonlocal cancelled
|
||||
try:
|
||||
await asyncio.sleep(1)
|
||||
except asyncio.CancelledError:
|
||||
cancelled = True
|
||||
raise
|
||||
|
||||
# Leave this running in the background
|
||||
asyncio.create_task(coro())
|
||||
|
||||
test = Test("test_leaking_task")
|
||||
output = test.run()
|
||||
self.assertTrue(cancelled)
|
||||
|
||||
def test_enterAsyncContext(self):
|
||||
events = []
|
||||
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_func(slf):
|
||||
slf.addAsyncCleanup(events.append, 'cleanup1')
|
||||
cm = TestCM(events, 42)
|
||||
self.assertEqual(await slf.enterAsyncContext(cm), 42)
|
||||
slf.addAsyncCleanup(events.append, 'cleanup2')
|
||||
events.append('test')
|
||||
|
||||
test = Test('test_func')
|
||||
output = test.run()
|
||||
self.assertTrue(output.wasSuccessful(), output)
|
||||
self.assertEqual(events, ['enter', 'test', 'cleanup2', 'exit', 'cleanup1'])
|
||||
|
||||
def test_enterAsyncContext_arg_errors(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_func(slf):
|
||||
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
|
||||
await slf.enterAsyncContext(LacksEnterAndExit())
|
||||
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
|
||||
await slf.enterAsyncContext(LacksEnter())
|
||||
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
|
||||
await slf.enterAsyncContext(LacksExit())
|
||||
|
||||
test = Test('test_func')
|
||||
output = test.run()
|
||||
self.assertTrue(output.wasSuccessful())
|
||||
|
||||
def test_debug_cleanup_same_loop(self):
|
||||
class Test(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self):
|
||||
async def coro():
|
||||
await asyncio.sleep(0)
|
||||
fut = asyncio.ensure_future(coro())
|
||||
self.addAsyncCleanup(self.cleanup, fut)
|
||||
events.append('asyncSetUp')
|
||||
|
||||
async def test_func(self):
|
||||
events.append('test')
|
||||
raise MyException()
|
||||
|
||||
async def asyncTearDown(self):
|
||||
events.append('asyncTearDown')
|
||||
|
||||
async def cleanup(self, fut):
|
||||
try:
|
||||
# Raises an exception if in different loop
|
||||
await asyncio.wait([fut])
|
||||
events.append('cleanup')
|
||||
except:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
raise
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'asyncTearDown', 'cleanup'])
|
||||
self.assertIn('MyException', result.errors[0][1])
|
||||
|
||||
events = []
|
||||
test = Test("test_func")
|
||||
self.addCleanup(test._tearDownAsyncioRunner)
|
||||
try:
|
||||
test.debug()
|
||||
except MyException:
|
||||
pass
|
||||
else:
|
||||
self.fail('Expected a MyException exception')
|
||||
self.assertEqual(events, ['asyncSetUp', 'test'])
|
||||
test.doCleanups()
|
||||
self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup'])
|
||||
|
||||
def test_setup_get_event_loop(self):
|
||||
# See https://github.com/python/cpython/issues/95736
|
||||
# Make sure the default event loop is not used
|
||||
asyncio.set_event_loop(None)
|
||||
|
||||
class TestCase1(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
asyncio.get_event_loop_policy().get_event_loop()
|
||||
|
||||
async def test_demo1(self):
|
||||
pass
|
||||
|
||||
test = TestCase1('test_demo1')
|
||||
result = test.run()
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
306
.CondaPkg/env/Lib/unittest/test/test_break.py
vendored
306
.CondaPkg/env/Lib/unittest/test/test_break.py
vendored
@@ -1,306 +0,0 @@
|
||||
import gc
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import signal
|
||||
import weakref
|
||||
import unittest
|
||||
|
||||
from test import support
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
|
||||
@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
|
||||
class TestBreak(unittest.TestCase):
|
||||
int_handler = None
|
||||
# This number was smart-guessed, previously tests were failing
|
||||
# after 7th run. So, we take `x * 2 + 1` to be sure.
|
||||
default_repeats = 15
|
||||
|
||||
def setUp(self):
|
||||
self._default_handler = signal.getsignal(signal.SIGINT)
|
||||
if self.int_handler is not None:
|
||||
signal.signal(signal.SIGINT, self.int_handler)
|
||||
|
||||
def tearDown(self):
|
||||
signal.signal(signal.SIGINT, self._default_handler)
|
||||
unittest.signals._results = weakref.WeakKeyDictionary()
|
||||
unittest.signals._interrupt_handler = None
|
||||
|
||||
|
||||
def withRepeats(self, test_function, repeats=None):
|
||||
if not support.check_impl_detail(cpython=True):
|
||||
# Override repeats count on non-cpython to execute only once.
|
||||
# Because this test only makes sense to be repeated on CPython.
|
||||
repeats = 1
|
||||
elif repeats is None:
|
||||
repeats = self.default_repeats
|
||||
|
||||
for repeat in range(repeats):
|
||||
with self.subTest(repeat=repeat):
|
||||
# We don't run `setUp` for the very first repeat
|
||||
# and we don't run `tearDown` for the very last one,
|
||||
# because they are handled by the test class itself.
|
||||
if repeat != 0:
|
||||
self.setUp()
|
||||
try:
|
||||
test_function()
|
||||
finally:
|
||||
if repeat != repeats - 1:
|
||||
self.tearDown()
|
||||
|
||||
def testInstallHandler(self):
|
||||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
unittest.installHandler()
|
||||
self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
try:
|
||||
pid = os.getpid()
|
||||
os.kill(pid, signal.SIGINT)
|
||||
except KeyboardInterrupt:
|
||||
self.fail("KeyboardInterrupt not handled")
|
||||
|
||||
self.assertTrue(unittest.signals._interrupt_handler.called)
|
||||
|
||||
def testRegisterResult(self):
|
||||
result = unittest.TestResult()
|
||||
self.assertNotIn(result, unittest.signals._results)
|
||||
|
||||
unittest.registerResult(result)
|
||||
try:
|
||||
self.assertIn(result, unittest.signals._results)
|
||||
finally:
|
||||
unittest.removeResult(result)
|
||||
|
||||
def testInterruptCaught(self):
|
||||
def test(result):
|
||||
pid = os.getpid()
|
||||
os.kill(pid, signal.SIGINT)
|
||||
result.breakCaught = True
|
||||
self.assertTrue(result.shouldStop)
|
||||
|
||||
def test_function():
|
||||
result = unittest.TestResult()
|
||||
unittest.installHandler()
|
||||
unittest.registerResult(result)
|
||||
|
||||
self.assertNotEqual(
|
||||
signal.getsignal(signal.SIGINT),
|
||||
self._default_handler,
|
||||
)
|
||||
|
||||
try:
|
||||
test(result)
|
||||
except KeyboardInterrupt:
|
||||
self.fail("KeyboardInterrupt not handled")
|
||||
self.assertTrue(result.breakCaught)
|
||||
self.withRepeats(test_function)
|
||||
|
||||
def testSecondInterrupt(self):
|
||||
# Can't use skipIf decorator because the signal handler may have
|
||||
# been changed after defining this method.
|
||||
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
|
||||
self.skipTest("test requires SIGINT to not be ignored")
|
||||
|
||||
def test(result):
|
||||
pid = os.getpid()
|
||||
os.kill(pid, signal.SIGINT)
|
||||
result.breakCaught = True
|
||||
self.assertTrue(result.shouldStop)
|
||||
os.kill(pid, signal.SIGINT)
|
||||
self.fail("Second KeyboardInterrupt not raised")
|
||||
|
||||
def test_function():
|
||||
result = unittest.TestResult()
|
||||
unittest.installHandler()
|
||||
unittest.registerResult(result)
|
||||
|
||||
with self.assertRaises(KeyboardInterrupt):
|
||||
test(result)
|
||||
self.assertTrue(result.breakCaught)
|
||||
self.withRepeats(test_function)
|
||||
|
||||
|
||||
def testTwoResults(self):
|
||||
def test_function():
|
||||
unittest.installHandler()
|
||||
|
||||
result = unittest.TestResult()
|
||||
unittest.registerResult(result)
|
||||
new_handler = signal.getsignal(signal.SIGINT)
|
||||
|
||||
result2 = unittest.TestResult()
|
||||
unittest.registerResult(result2)
|
||||
self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
|
||||
|
||||
result3 = unittest.TestResult()
|
||||
|
||||
try:
|
||||
os.kill(os.getpid(), signal.SIGINT)
|
||||
except KeyboardInterrupt:
|
||||
self.fail("KeyboardInterrupt not handled")
|
||||
|
||||
self.assertTrue(result.shouldStop)
|
||||
self.assertTrue(result2.shouldStop)
|
||||
self.assertFalse(result3.shouldStop)
|
||||
self.withRepeats(test_function)
|
||||
|
||||
|
||||
def testHandlerReplacedButCalled(self):
|
||||
# Can't use skipIf decorator because the signal handler may have
|
||||
# been changed after defining this method.
|
||||
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
|
||||
self.skipTest("test requires SIGINT to not be ignored")
|
||||
|
||||
def test_function():
|
||||
# If our handler has been replaced (is no longer installed) but is
|
||||
# called by the *new* handler, then it isn't safe to delay the
|
||||
# SIGINT and we should immediately delegate to the default handler
|
||||
unittest.installHandler()
|
||||
|
||||
handler = signal.getsignal(signal.SIGINT)
|
||||
def new_handler(frame, signum):
|
||||
handler(frame, signum)
|
||||
signal.signal(signal.SIGINT, new_handler)
|
||||
|
||||
try:
|
||||
os.kill(os.getpid(), signal.SIGINT)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
else:
|
||||
self.fail("replaced but delegated handler doesn't raise interrupt")
|
||||
self.withRepeats(test_function)
|
||||
|
||||
def testRunner(self):
|
||||
# Creating a TextTestRunner with the appropriate argument should
|
||||
# register the TextTestResult it creates
|
||||
runner = unittest.TextTestRunner(stream=io.StringIO())
|
||||
|
||||
result = runner.run(unittest.TestSuite())
|
||||
self.assertIn(result, unittest.signals._results)
|
||||
|
||||
def testWeakReferences(self):
|
||||
# Calling registerResult on a result should not keep it alive
|
||||
result = unittest.TestResult()
|
||||
unittest.registerResult(result)
|
||||
|
||||
ref = weakref.ref(result)
|
||||
del result
|
||||
|
||||
# For non-reference counting implementations
|
||||
gc.collect();gc.collect()
|
||||
self.assertIsNone(ref())
|
||||
|
||||
|
||||
def testRemoveResult(self):
|
||||
result = unittest.TestResult()
|
||||
unittest.registerResult(result)
|
||||
|
||||
unittest.installHandler()
|
||||
self.assertTrue(unittest.removeResult(result))
|
||||
|
||||
# Should this raise an error instead?
|
||||
self.assertFalse(unittest.removeResult(unittest.TestResult()))
|
||||
|
||||
try:
|
||||
pid = os.getpid()
|
||||
os.kill(pid, signal.SIGINT)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
self.assertFalse(result.shouldStop)
|
||||
|
||||
def testMainInstallsHandler(self):
|
||||
failfast = object()
|
||||
test = object()
|
||||
verbosity = object()
|
||||
result = object()
|
||||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
|
||||
class FakeRunner(object):
|
||||
initArgs = []
|
||||
runArgs = []
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.initArgs.append((args, kwargs))
|
||||
def run(self, test):
|
||||
self.runArgs.append(test)
|
||||
return result
|
||||
|
||||
class Program(unittest.TestProgram):
|
||||
def __init__(self, catchbreak):
|
||||
self.exit = False
|
||||
self.verbosity = verbosity
|
||||
self.failfast = failfast
|
||||
self.catchbreak = catchbreak
|
||||
self.tb_locals = False
|
||||
self.testRunner = FakeRunner
|
||||
self.test = test
|
||||
self.result = None
|
||||
|
||||
p = Program(False)
|
||||
p.runTests()
|
||||
|
||||
self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
|
||||
'verbosity': verbosity,
|
||||
'failfast': failfast,
|
||||
'tb_locals': False,
|
||||
'warnings': None})])
|
||||
self.assertEqual(FakeRunner.runArgs, [test])
|
||||
self.assertEqual(p.result, result)
|
||||
|
||||
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
FakeRunner.initArgs = []
|
||||
FakeRunner.runArgs = []
|
||||
p = Program(True)
|
||||
p.runTests()
|
||||
|
||||
self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
|
||||
'verbosity': verbosity,
|
||||
'failfast': failfast,
|
||||
'tb_locals': False,
|
||||
'warnings': None})])
|
||||
self.assertEqual(FakeRunner.runArgs, [test])
|
||||
self.assertEqual(p.result, result)
|
||||
|
||||
self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
def testRemoveHandler(self):
|
||||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
unittest.installHandler()
|
||||
unittest.removeHandler()
|
||||
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
# check that calling removeHandler multiple times has no ill-effect
|
||||
unittest.removeHandler()
|
||||
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
def testRemoveHandlerAsDecorator(self):
|
||||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
unittest.installHandler()
|
||||
|
||||
@unittest.removeHandler
|
||||
def test():
|
||||
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
test()
|
||||
self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
|
||||
@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
|
||||
class TestBreakDefaultIntHandler(TestBreak):
|
||||
int_handler = signal.default_int_handler
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
|
||||
@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
|
||||
class TestBreakSignalIgnored(TestBreak):
|
||||
int_handler = signal.SIG_IGN
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
|
||||
@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
|
||||
class TestBreakSignalDefault(TestBreak):
|
||||
int_handler = signal.SIG_DFL
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
1988
.CondaPkg/env/Lib/unittest/test/test_case.py
vendored
1988
.CondaPkg/env/Lib/unittest/test/test_case.py
vendored
File diff suppressed because it is too large
Load Diff
849
.CondaPkg/env/Lib/unittest/test/test_discovery.py
vendored
849
.CondaPkg/env/Lib/unittest/test/test_discovery.py
vendored
@@ -1,849 +0,0 @@
|
||||
import os.path
|
||||
from os.path import abspath
|
||||
import re
|
||||
import sys
|
||||
import types
|
||||
import pickle
|
||||
from test import support
|
||||
from test.support import import_helper
|
||||
import test.test_importlib.util
|
||||
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import unittest.test
|
||||
|
||||
|
||||
class TestableTestProgram(unittest.TestProgram):
|
||||
module = None
|
||||
exit = True
|
||||
defaultTest = failfast = catchbreak = buffer = None
|
||||
verbosity = 1
|
||||
progName = ''
|
||||
testRunner = testLoader = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
class TestDiscovery(unittest.TestCase):
|
||||
|
||||
# Heavily mocked tests so I can avoid hitting the filesystem
|
||||
def test_get_name_from_path(self):
|
||||
loader = unittest.TestLoader()
|
||||
loader._top_level_dir = '/foo'
|
||||
name = loader._get_name_from_path('/foo/bar/baz.py')
|
||||
self.assertEqual(name, 'bar.baz')
|
||||
|
||||
if not __debug__:
|
||||
# asserts are off
|
||||
return
|
||||
|
||||
with self.assertRaises(AssertionError):
|
||||
loader._get_name_from_path('/bar/baz.py')
|
||||
|
||||
def test_find_tests(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
original_listdir = os.listdir
|
||||
def restore_listdir():
|
||||
os.listdir = original_listdir
|
||||
original_isfile = os.path.isfile
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
|
||||
path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir',
|
||||
'test.foo', 'test-not-a-module.py', 'another_dir'],
|
||||
['test4.py', 'test3.py', ]]
|
||||
os.listdir = lambda path: path_lists.pop(0)
|
||||
self.addCleanup(restore_listdir)
|
||||
|
||||
def isdir(path):
|
||||
return path.endswith('dir')
|
||||
os.path.isdir = isdir
|
||||
self.addCleanup(restore_isdir)
|
||||
|
||||
def isfile(path):
|
||||
# another_dir is not a package and so shouldn't be recursed into
|
||||
return not path.endswith('dir') and not 'another_dir' in path
|
||||
os.path.isfile = isfile
|
||||
self.addCleanup(restore_isfile)
|
||||
|
||||
loader._get_module_from_name = lambda path: path + ' module'
|
||||
orig_load_tests = loader.loadTestsFromModule
|
||||
def loadTestsFromModule(module, pattern=None):
|
||||
# This is where load_tests is called.
|
||||
base = orig_load_tests(module, pattern=pattern)
|
||||
return base + [module + ' tests']
|
||||
loader.loadTestsFromModule = loadTestsFromModule
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
top_level = os.path.abspath('/foo')
|
||||
loader._top_level_dir = top_level
|
||||
suite = list(loader._find_tests(top_level, 'test*.py'))
|
||||
|
||||
# The test suites found should be sorted alphabetically for reliable
|
||||
# execution order.
|
||||
expected = [[name + ' module tests'] for name in
|
||||
('test1', 'test2', 'test_dir')]
|
||||
expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
|
||||
('test3', 'test4')])
|
||||
self.assertEqual(suite, expected)
|
||||
|
||||
def test_find_tests_socket(self):
|
||||
# A socket is neither a directory nor a regular file.
|
||||
# https://bugs.python.org/issue25320
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
original_listdir = os.listdir
|
||||
def restore_listdir():
|
||||
os.listdir = original_listdir
|
||||
original_isfile = os.path.isfile
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
|
||||
path_lists = [['socket']]
|
||||
os.listdir = lambda path: path_lists.pop(0)
|
||||
self.addCleanup(restore_listdir)
|
||||
|
||||
os.path.isdir = lambda path: False
|
||||
self.addCleanup(restore_isdir)
|
||||
|
||||
os.path.isfile = lambda path: False
|
||||
self.addCleanup(restore_isfile)
|
||||
|
||||
loader._get_module_from_name = lambda path: path + ' module'
|
||||
orig_load_tests = loader.loadTestsFromModule
|
||||
def loadTestsFromModule(module, pattern=None):
|
||||
# This is where load_tests is called.
|
||||
base = orig_load_tests(module, pattern=pattern)
|
||||
return base + [module + ' tests']
|
||||
loader.loadTestsFromModule = loadTestsFromModule
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
top_level = os.path.abspath('/foo')
|
||||
loader._top_level_dir = top_level
|
||||
suite = list(loader._find_tests(top_level, 'test*.py'))
|
||||
|
||||
self.assertEqual(suite, [])
|
||||
|
||||
def test_find_tests_with_package(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
original_listdir = os.listdir
|
||||
def restore_listdir():
|
||||
os.listdir = original_listdir
|
||||
original_isfile = os.path.isfile
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
|
||||
directories = ['a_directory', 'test_directory', 'test_directory2']
|
||||
path_lists = [directories, [], [], []]
|
||||
os.listdir = lambda path: path_lists.pop(0)
|
||||
self.addCleanup(restore_listdir)
|
||||
|
||||
os.path.isdir = lambda path: True
|
||||
self.addCleanup(restore_isdir)
|
||||
|
||||
os.path.isfile = lambda path: os.path.basename(path) not in directories
|
||||
self.addCleanup(restore_isfile)
|
||||
|
||||
class Module(object):
|
||||
paths = []
|
||||
load_tests_args = []
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.paths.append(path)
|
||||
if os.path.basename(path) == 'test_directory':
|
||||
def load_tests(loader, tests, pattern):
|
||||
self.load_tests_args.append((loader, tests, pattern))
|
||||
return [self.path + ' load_tests']
|
||||
self.load_tests = load_tests
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.path == other.path
|
||||
|
||||
loader._get_module_from_name = lambda name: Module(name)
|
||||
orig_load_tests = loader.loadTestsFromModule
|
||||
def loadTestsFromModule(module, pattern=None):
|
||||
# This is where load_tests is called.
|
||||
base = orig_load_tests(module, pattern=pattern)
|
||||
return base + [module.path + ' module tests']
|
||||
loader.loadTestsFromModule = loadTestsFromModule
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
loader._top_level_dir = '/foo'
|
||||
# this time no '.py' on the pattern so that it can match
|
||||
# a test package
|
||||
suite = list(loader._find_tests('/foo', 'test*'))
|
||||
|
||||
# We should have loaded tests from the a_directory and test_directory2
|
||||
# directly and via load_tests for the test_directory package, which
|
||||
# still calls the baseline module loader.
|
||||
self.assertEqual(suite,
|
||||
[['a_directory module tests'],
|
||||
['test_directory load_tests',
|
||||
'test_directory module tests'],
|
||||
['test_directory2 module tests']])
|
||||
|
||||
|
||||
# The test module paths should be sorted for reliable execution order
|
||||
self.assertEqual(Module.paths,
|
||||
['a_directory', 'test_directory', 'test_directory2'])
|
||||
|
||||
# load_tests should have been called once with loader, tests and pattern
|
||||
# (but there are no tests in our stub module itself, so that is [] at
|
||||
# the time of call).
|
||||
self.assertEqual(Module.load_tests_args,
|
||||
[(loader, [], 'test*')])
|
||||
|
||||
def test_find_tests_default_calls_package_load_tests(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
original_listdir = os.listdir
|
||||
def restore_listdir():
|
||||
os.listdir = original_listdir
|
||||
original_isfile = os.path.isfile
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
|
||||
directories = ['a_directory', 'test_directory', 'test_directory2']
|
||||
path_lists = [directories, [], [], []]
|
||||
os.listdir = lambda path: path_lists.pop(0)
|
||||
self.addCleanup(restore_listdir)
|
||||
|
||||
os.path.isdir = lambda path: True
|
||||
self.addCleanup(restore_isdir)
|
||||
|
||||
os.path.isfile = lambda path: os.path.basename(path) not in directories
|
||||
self.addCleanup(restore_isfile)
|
||||
|
||||
class Module(object):
|
||||
paths = []
|
||||
load_tests_args = []
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.paths.append(path)
|
||||
if os.path.basename(path) == 'test_directory':
|
||||
def load_tests(loader, tests, pattern):
|
||||
self.load_tests_args.append((loader, tests, pattern))
|
||||
return [self.path + ' load_tests']
|
||||
self.load_tests = load_tests
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.path == other.path
|
||||
|
||||
loader._get_module_from_name = lambda name: Module(name)
|
||||
orig_load_tests = loader.loadTestsFromModule
|
||||
def loadTestsFromModule(module, pattern=None):
|
||||
# This is where load_tests is called.
|
||||
base = orig_load_tests(module, pattern=pattern)
|
||||
return base + [module.path + ' module tests']
|
||||
loader.loadTestsFromModule = loadTestsFromModule
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
loader._top_level_dir = '/foo'
|
||||
# this time no '.py' on the pattern so that it can match
|
||||
# a test package
|
||||
suite = list(loader._find_tests('/foo', 'test*.py'))
|
||||
|
||||
# We should have loaded tests from the a_directory and test_directory2
|
||||
# directly and via load_tests for the test_directory package, which
|
||||
# still calls the baseline module loader.
|
||||
self.assertEqual(suite,
|
||||
[['a_directory module tests'],
|
||||
['test_directory load_tests',
|
||||
'test_directory module tests'],
|
||||
['test_directory2 module tests']])
|
||||
# The test module paths should be sorted for reliable execution order
|
||||
self.assertEqual(Module.paths,
|
||||
['a_directory', 'test_directory', 'test_directory2'])
|
||||
|
||||
|
||||
# load_tests should have been called once with loader, tests and pattern
|
||||
self.assertEqual(Module.load_tests_args,
|
||||
[(loader, [], 'test*.py')])
|
||||
|
||||
def test_find_tests_customize_via_package_pattern(self):
|
||||
# This test uses the example 'do-nothing' load_tests from
|
||||
# https://docs.python.org/3/library/unittest.html#load-tests-protocol
|
||||
# to make sure that that actually works.
|
||||
# Housekeeping
|
||||
original_listdir = os.listdir
|
||||
def restore_listdir():
|
||||
os.listdir = original_listdir
|
||||
self.addCleanup(restore_listdir)
|
||||
original_isfile = os.path.isfile
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
self.addCleanup(restore_isfile)
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
self.addCleanup(restore_isdir)
|
||||
self.addCleanup(sys.path.remove, abspath('/foo'))
|
||||
|
||||
# Test data: we expect the following:
|
||||
# a listdir to find our package, and isfile and isdir checks on it.
|
||||
# a module-from-name call to turn that into a module
|
||||
# followed by load_tests.
|
||||
# then our load_tests will call discover() which is messy
|
||||
# but that finally chains into find_tests again for the child dir -
|
||||
# which is why we don't have an infinite loop.
|
||||
# We expect to see:
|
||||
# the module load tests for both package and plain module called,
|
||||
# and the plain module result nested by the package module load_tests
|
||||
# indicating that it was processed and could have been mutated.
|
||||
vfs = {abspath('/foo'): ['my_package'],
|
||||
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
||||
def list_dir(path):
|
||||
return list(vfs[path])
|
||||
os.listdir = list_dir
|
||||
os.path.isdir = lambda path: not path.endswith('.py')
|
||||
os.path.isfile = lambda path: path.endswith('.py')
|
||||
|
||||
class Module(object):
|
||||
paths = []
|
||||
load_tests_args = []
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.paths.append(path)
|
||||
if path.endswith('test_module'):
|
||||
def load_tests(loader, tests, pattern):
|
||||
self.load_tests_args.append((loader, tests, pattern))
|
||||
return [self.path + ' load_tests']
|
||||
else:
|
||||
def load_tests(loader, tests, pattern):
|
||||
self.load_tests_args.append((loader, tests, pattern))
|
||||
# top level directory cached on loader instance
|
||||
__file__ = '/foo/my_package/__init__.py'
|
||||
this_dir = os.path.dirname(__file__)
|
||||
pkg_tests = loader.discover(
|
||||
start_dir=this_dir, pattern=pattern)
|
||||
return [self.path + ' load_tests', tests
|
||||
] + pkg_tests
|
||||
self.load_tests = load_tests
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.path == other.path
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
loader._get_module_from_name = lambda name: Module(name)
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
loader._top_level_dir = abspath('/foo')
|
||||
# this time no '.py' on the pattern so that it can match
|
||||
# a test package
|
||||
suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
|
||||
|
||||
# We should have loaded tests from both my_package and
|
||||
# my_package.test_module, and also run the load_tests hook in both.
|
||||
# (normally this would be nested TestSuites.)
|
||||
self.assertEqual(suite,
|
||||
[['my_package load_tests', [],
|
||||
['my_package.test_module load_tests']]])
|
||||
# Parents before children.
|
||||
self.assertEqual(Module.paths,
|
||||
['my_package', 'my_package.test_module'])
|
||||
|
||||
# load_tests should have been called twice with loader, tests and pattern
|
||||
self.assertEqual(Module.load_tests_args,
|
||||
[(loader, [], 'test*.py'),
|
||||
(loader, [], 'test*.py')])
|
||||
|
||||
def test_discover(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
original_isfile = os.path.isfile
|
||||
original_isdir = os.path.isdir
|
||||
def restore_isfile():
|
||||
os.path.isfile = original_isfile
|
||||
|
||||
os.path.isfile = lambda path: False
|
||||
self.addCleanup(restore_isfile)
|
||||
|
||||
orig_sys_path = sys.path[:]
|
||||
def restore_path():
|
||||
sys.path[:] = orig_sys_path
|
||||
self.addCleanup(restore_path)
|
||||
|
||||
full_path = os.path.abspath(os.path.normpath('/foo'))
|
||||
with self.assertRaises(ImportError):
|
||||
loader.discover('/foo/bar', top_level_dir='/foo')
|
||||
|
||||
self.assertEqual(loader._top_level_dir, full_path)
|
||||
self.assertIn(full_path, sys.path)
|
||||
|
||||
os.path.isfile = lambda path: True
|
||||
os.path.isdir = lambda path: True
|
||||
|
||||
def restore_isdir():
|
||||
os.path.isdir = original_isdir
|
||||
self.addCleanup(restore_isdir)
|
||||
|
||||
_find_tests_args = []
|
||||
def _find_tests(start_dir, pattern):
|
||||
_find_tests_args.append((start_dir, pattern))
|
||||
return ['tests']
|
||||
loader._find_tests = _find_tests
|
||||
loader.suiteClass = str
|
||||
|
||||
suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')
|
||||
|
||||
top_level_dir = os.path.abspath('/foo/bar')
|
||||
start_dir = os.path.abspath('/foo/bar/baz')
|
||||
self.assertEqual(suite, "['tests']")
|
||||
self.assertEqual(loader._top_level_dir, top_level_dir)
|
||||
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
|
||||
self.assertIn(top_level_dir, sys.path)
|
||||
|
||||
def test_discover_start_dir_is_package_calls_package_load_tests(self):
|
||||
# This test verifies that the package load_tests in a package is indeed
|
||||
# invoked when the start_dir is a package (and not the top level).
|
||||
# http://bugs.python.org/issue22457
|
||||
|
||||
# Test data: we expect the following:
|
||||
# an isfile to verify the package, then importing and scanning
|
||||
# as per _find_tests' normal behaviour.
|
||||
# We expect to see our load_tests hook called once.
|
||||
vfs = {abspath('/toplevel'): ['startdir'],
|
||||
abspath('/toplevel/startdir'): ['__init__.py']}
|
||||
def list_dir(path):
|
||||
return list(vfs[path])
|
||||
self.addCleanup(setattr, os, 'listdir', os.listdir)
|
||||
os.listdir = list_dir
|
||||
self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
|
||||
os.path.isfile = lambda path: path.endswith('.py')
|
||||
self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
|
||||
os.path.isdir = lambda path: not path.endswith('.py')
|
||||
self.addCleanup(sys.path.remove, abspath('/toplevel'))
|
||||
|
||||
class Module(object):
|
||||
paths = []
|
||||
load_tests_args = []
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def load_tests(self, loader, tests, pattern):
|
||||
return ['load_tests called ' + self.path]
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.path == other.path
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
loader._get_module_from_name = lambda name: Module(name)
|
||||
loader.suiteClass = lambda thing: thing
|
||||
|
||||
suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')
|
||||
|
||||
# We should have loaded tests from the package __init__.
|
||||
# (normally this would be nested TestSuites.)
|
||||
self.assertEqual(suite,
|
||||
[['load_tests called startdir']])
|
||||
|
||||
def setup_import_issue_tests(self, fakefile):
|
||||
listdir = os.listdir
|
||||
os.listdir = lambda _: [fakefile]
|
||||
isfile = os.path.isfile
|
||||
os.path.isfile = lambda _: True
|
||||
orig_sys_path = sys.path[:]
|
||||
def restore():
|
||||
os.path.isfile = isfile
|
||||
os.listdir = listdir
|
||||
sys.path[:] = orig_sys_path
|
||||
self.addCleanup(restore)
|
||||
|
||||
def setup_import_issue_package_tests(self, vfs):
|
||||
self.addCleanup(setattr, os, 'listdir', os.listdir)
|
||||
self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
|
||||
self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
|
||||
self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
|
||||
def list_dir(path):
|
||||
return list(vfs[path])
|
||||
os.listdir = list_dir
|
||||
os.path.isdir = lambda path: not path.endswith('.py')
|
||||
os.path.isfile = lambda path: path.endswith('.py')
|
||||
|
||||
def test_discover_with_modules_that_fail_to_import(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
self.setup_import_issue_tests('test_this_does_not_exist.py')
|
||||
|
||||
suite = loader.discover('.')
|
||||
self.assertIn(os.getcwd(), sys.path)
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
# Errors loading the suite are also captured for introspection.
|
||||
self.assertNotEqual([], loader.errors)
|
||||
self.assertEqual(1, len(loader.errors))
|
||||
error = loader.errors[0]
|
||||
self.assertTrue(
|
||||
'Failed to import test module: test_this_does_not_exist' in error,
|
||||
'missing error string in %r' % error)
|
||||
test = list(list(suite)[0])[0] # extract test from suite
|
||||
|
||||
with self.assertRaises(ImportError):
|
||||
test.test_this_does_not_exist()
|
||||
|
||||
def test_discover_with_init_modules_that_fail_to_import(self):
|
||||
vfs = {abspath('/foo'): ['my_package'],
|
||||
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
||||
self.setup_import_issue_package_tests(vfs)
|
||||
import_calls = []
|
||||
def _get_module_from_name(name):
|
||||
import_calls.append(name)
|
||||
raise ImportError("Cannot import Name")
|
||||
loader = unittest.TestLoader()
|
||||
loader._get_module_from_name = _get_module_from_name
|
||||
suite = loader.discover(abspath('/foo'))
|
||||
|
||||
self.assertIn(abspath('/foo'), sys.path)
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
# Errors loading the suite are also captured for introspection.
|
||||
self.assertNotEqual([], loader.errors)
|
||||
self.assertEqual(1, len(loader.errors))
|
||||
error = loader.errors[0]
|
||||
self.assertTrue(
|
||||
'Failed to import test module: my_package' in error,
|
||||
'missing error string in %r' % error)
|
||||
test = list(list(suite)[0])[0] # extract test from suite
|
||||
with self.assertRaises(ImportError):
|
||||
test.my_package()
|
||||
self.assertEqual(import_calls, ['my_package'])
|
||||
|
||||
# Check picklability
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
pickle.loads(pickle.dumps(test, proto))
|
||||
|
||||
def test_discover_with_module_that_raises_SkipTest_on_import(self):
|
||||
if not unittest.BaseTestSuite._cleanup:
|
||||
raise unittest.SkipTest("Suite cleanup is disabled")
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
def _get_module_from_name(name):
|
||||
raise unittest.SkipTest('skipperoo')
|
||||
loader._get_module_from_name = _get_module_from_name
|
||||
|
||||
self.setup_import_issue_tests('test_skip_dummy.py')
|
||||
|
||||
suite = loader.discover('.')
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
|
||||
result = unittest.TestResult()
|
||||
suite.run(result)
|
||||
self.assertEqual(len(result.skipped), 1)
|
||||
|
||||
# Check picklability
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
pickle.loads(pickle.dumps(suite, proto))
|
||||
|
||||
def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
|
||||
if not unittest.BaseTestSuite._cleanup:
|
||||
raise unittest.SkipTest("Suite cleanup is disabled")
|
||||
|
||||
vfs = {abspath('/foo'): ['my_package'],
|
||||
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
||||
self.setup_import_issue_package_tests(vfs)
|
||||
import_calls = []
|
||||
def _get_module_from_name(name):
|
||||
import_calls.append(name)
|
||||
raise unittest.SkipTest('skipperoo')
|
||||
loader = unittest.TestLoader()
|
||||
loader._get_module_from_name = _get_module_from_name
|
||||
suite = loader.discover(abspath('/foo'))
|
||||
|
||||
self.assertIn(abspath('/foo'), sys.path)
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
result = unittest.TestResult()
|
||||
suite.run(result)
|
||||
self.assertEqual(len(result.skipped), 1)
|
||||
self.assertEqual(result.testsRun, 1)
|
||||
self.assertEqual(import_calls, ['my_package'])
|
||||
|
||||
# Check picklability
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
pickle.loads(pickle.dumps(suite, proto))
|
||||
|
||||
def test_command_line_handling_parseArgs(self):
|
||||
program = TestableTestProgram()
|
||||
|
||||
args = []
|
||||
program._do_discovery = args.append
|
||||
program.parseArgs(['something', 'discover'])
|
||||
self.assertEqual(args, [[]])
|
||||
|
||||
args[:] = []
|
||||
program.parseArgs(['something', 'discover', 'foo', 'bar'])
|
||||
self.assertEqual(args, [['foo', 'bar']])
|
||||
|
||||
def test_command_line_handling_discover_by_default(self):
|
||||
program = TestableTestProgram()
|
||||
|
||||
args = []
|
||||
program._do_discovery = args.append
|
||||
program.parseArgs(['something'])
|
||||
self.assertEqual(args, [[]])
|
||||
self.assertEqual(program.verbosity, 1)
|
||||
self.assertIs(program.buffer, False)
|
||||
self.assertIs(program.catchbreak, False)
|
||||
self.assertIs(program.failfast, False)
|
||||
|
||||
def test_command_line_handling_discover_by_default_with_options(self):
|
||||
program = TestableTestProgram()
|
||||
|
||||
args = []
|
||||
program._do_discovery = args.append
|
||||
program.parseArgs(['something', '-v', '-b', '-v', '-c', '-f'])
|
||||
self.assertEqual(args, [[]])
|
||||
self.assertEqual(program.verbosity, 2)
|
||||
self.assertIs(program.buffer, True)
|
||||
self.assertIs(program.catchbreak, True)
|
||||
self.assertIs(program.failfast, True)
|
||||
|
||||
|
||||
def test_command_line_handling_do_discovery_too_many_arguments(self):
|
||||
program = TestableTestProgram()
|
||||
program.testLoader = None
|
||||
|
||||
with support.captured_stderr() as stderr, \
|
||||
self.assertRaises(SystemExit) as cm:
|
||||
# too many args
|
||||
program._do_discovery(['one', 'two', 'three', 'four'])
|
||||
self.assertEqual(cm.exception.args, (2,))
|
||||
self.assertIn('usage:', stderr.getvalue())
|
||||
|
||||
|
||||
def test_command_line_handling_do_discovery_uses_default_loader(self):
|
||||
program = object.__new__(unittest.TestProgram)
|
||||
program._initArgParsers()
|
||||
|
||||
class Loader(object):
|
||||
args = []
|
||||
def discover(self, start_dir, pattern, top_level_dir):
|
||||
self.args.append((start_dir, pattern, top_level_dir))
|
||||
return 'tests'
|
||||
|
||||
program.testLoader = Loader()
|
||||
program._do_discovery(['-v'])
|
||||
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
||||
|
||||
def test_command_line_handling_do_discovery_calls_loader(self):
|
||||
program = TestableTestProgram()
|
||||
|
||||
class Loader(object):
|
||||
args = []
|
||||
def discover(self, start_dir, pattern, top_level_dir):
|
||||
self.args.append((start_dir, pattern, top_level_dir))
|
||||
return 'tests'
|
||||
|
||||
program._do_discovery(['-v'], Loader=Loader)
|
||||
self.assertEqual(program.verbosity, 2)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['--verbose'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery([], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['fish'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['fish', 'eggs'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('fish', 'eggs', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['-s', 'fish'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['-t', 'fish'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['-p', 'fish'], Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('.', 'fish', None)])
|
||||
self.assertFalse(program.failfast)
|
||||
self.assertFalse(program.catchbreak)
|
||||
|
||||
Loader.args = []
|
||||
program = TestableTestProgram()
|
||||
program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
|
||||
Loader=Loader)
|
||||
self.assertEqual(program.test, 'tests')
|
||||
self.assertEqual(Loader.args, [('fish', 'eggs', None)])
|
||||
self.assertEqual(program.verbosity, 2)
|
||||
self.assertTrue(program.failfast)
|
||||
self.assertTrue(program.catchbreak)
|
||||
|
||||
def setup_module_clash(self):
|
||||
class Module(object):
|
||||
__file__ = 'bar/foo.py'
|
||||
sys.modules['foo'] = Module
|
||||
full_path = os.path.abspath('foo')
|
||||
original_listdir = os.listdir
|
||||
original_isfile = os.path.isfile
|
||||
original_isdir = os.path.isdir
|
||||
original_realpath = os.path.realpath
|
||||
|
||||
def cleanup():
|
||||
os.listdir = original_listdir
|
||||
os.path.isfile = original_isfile
|
||||
os.path.isdir = original_isdir
|
||||
os.path.realpath = original_realpath
|
||||
del sys.modules['foo']
|
||||
if full_path in sys.path:
|
||||
sys.path.remove(full_path)
|
||||
self.addCleanup(cleanup)
|
||||
|
||||
def listdir(_):
|
||||
return ['foo.py']
|
||||
def isfile(_):
|
||||
return True
|
||||
def isdir(_):
|
||||
return True
|
||||
os.listdir = listdir
|
||||
os.path.isfile = isfile
|
||||
os.path.isdir = isdir
|
||||
if os.name == 'nt':
|
||||
# ntpath.realpath may inject path prefixes when failing to
|
||||
# resolve real files, so we substitute abspath() here instead.
|
||||
os.path.realpath = os.path.abspath
|
||||
return full_path
|
||||
|
||||
def test_detect_module_clash(self):
|
||||
full_path = self.setup_module_clash()
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
mod_dir = os.path.abspath('bar')
|
||||
expected_dir = os.path.abspath('foo')
|
||||
msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. "
|
||||
"Is this module globally installed?" % (mod_dir, expected_dir))
|
||||
self.assertRaisesRegex(
|
||||
ImportError, '^%s$' % msg, loader.discover,
|
||||
start_dir='foo', pattern='foo.py'
|
||||
)
|
||||
self.assertEqual(sys.path[0], full_path)
|
||||
|
||||
def test_module_symlink_ok(self):
|
||||
full_path = self.setup_module_clash()
|
||||
|
||||
original_realpath = os.path.realpath
|
||||
|
||||
mod_dir = os.path.abspath('bar')
|
||||
expected_dir = os.path.abspath('foo')
|
||||
|
||||
def cleanup():
|
||||
os.path.realpath = original_realpath
|
||||
self.addCleanup(cleanup)
|
||||
|
||||
def realpath(path):
|
||||
if path == os.path.join(mod_dir, 'foo.py'):
|
||||
return os.path.join(expected_dir, 'foo.py')
|
||||
return path
|
||||
os.path.realpath = realpath
|
||||
loader = unittest.TestLoader()
|
||||
loader.discover(start_dir='foo', pattern='foo.py')
|
||||
|
||||
def test_discovery_from_dotted_path(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
tests = [self]
|
||||
expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
|
||||
|
||||
self.wasRun = False
|
||||
def _find_tests(start_dir, pattern):
|
||||
self.wasRun = True
|
||||
self.assertEqual(start_dir, expectedPath)
|
||||
return tests
|
||||
loader._find_tests = _find_tests
|
||||
suite = loader.discover('unittest.test')
|
||||
self.assertTrue(self.wasRun)
|
||||
self.assertEqual(suite._tests, tests)
|
||||
|
||||
|
||||
def test_discovery_from_dotted_path_builtin_modules(self):
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
listdir = os.listdir
|
||||
os.listdir = lambda _: ['test_this_does_not_exist.py']
|
||||
isfile = os.path.isfile
|
||||
isdir = os.path.isdir
|
||||
os.path.isdir = lambda _: False
|
||||
orig_sys_path = sys.path[:]
|
||||
def restore():
|
||||
os.path.isfile = isfile
|
||||
os.path.isdir = isdir
|
||||
os.listdir = listdir
|
||||
sys.path[:] = orig_sys_path
|
||||
self.addCleanup(restore)
|
||||
|
||||
with self.assertRaises(TypeError) as cm:
|
||||
loader.discover('sys')
|
||||
self.assertEqual(str(cm.exception),
|
||||
'Can not use builtin modules '
|
||||
'as dotted module names')
|
||||
|
||||
def test_discovery_failed_discovery(self):
|
||||
loader = unittest.TestLoader()
|
||||
package = types.ModuleType('package')
|
||||
|
||||
def _import(packagename, *args, **kwargs):
|
||||
sys.modules[packagename] = package
|
||||
return package
|
||||
|
||||
with unittest.mock.patch('builtins.__import__', _import):
|
||||
# Since loader.discover() can modify sys.path, restore it when done.
|
||||
with import_helper.DirsOnSysPath():
|
||||
# Make sure to remove 'package' from sys.modules when done.
|
||||
with test.test_importlib.util.uncache('package'):
|
||||
with self.assertRaises(TypeError) as cm:
|
||||
loader.discover('package')
|
||||
self.assertEqual(str(cm.exception),
|
||||
'don\'t know how to discover from {!r}'
|
||||
.format(package))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,148 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from unittest.test.support import LoggingResult
|
||||
|
||||
|
||||
class Test_FunctionTestCase(unittest.TestCase):
|
||||
|
||||
# "Return the number of tests represented by the this test object. For
|
||||
# TestCase instances, this will always be 1"
|
||||
def test_countTestCases(self):
|
||||
test = unittest.FunctionTestCase(lambda: None)
|
||||
|
||||
self.assertEqual(test.countTestCases(), 1)
|
||||
|
||||
# "When a setUp() method is defined, the test runner will run that method
|
||||
# prior to each test. Likewise, if a tearDown() method is defined, the
|
||||
# test runner will invoke that method after each test. In the example,
|
||||
# setUp() was used to create a fresh sequence for each test."
|
||||
#
|
||||
# Make sure the proper call order is maintained, even if setUp() raises
|
||||
# an exception.
|
||||
def test_run_call_order__error_in_setUp(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
def setUp():
|
||||
events.append('setUp')
|
||||
raise RuntimeError('raised by setUp')
|
||||
|
||||
def test():
|
||||
events.append('test')
|
||||
|
||||
def tearDown():
|
||||
events.append('tearDown')
|
||||
|
||||
expected = ['startTest', 'setUp', 'addError', 'stopTest']
|
||||
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
|
||||
self.assertEqual(events, expected)
|
||||
|
||||
# "When a setUp() method is defined, the test runner will run that method
|
||||
# prior to each test. Likewise, if a tearDown() method is defined, the
|
||||
# test runner will invoke that method after each test. In the example,
|
||||
# setUp() was used to create a fresh sequence for each test."
|
||||
#
|
||||
# Make sure the proper call order is maintained, even if the test raises
|
||||
# an error (as opposed to a failure).
|
||||
def test_run_call_order__error_in_test(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
def setUp():
|
||||
events.append('setUp')
|
||||
|
||||
def test():
|
||||
events.append('test')
|
||||
raise RuntimeError('raised by test')
|
||||
|
||||
def tearDown():
|
||||
events.append('tearDown')
|
||||
|
||||
expected = ['startTest', 'setUp', 'test',
|
||||
'addError', 'tearDown', 'stopTest']
|
||||
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
|
||||
self.assertEqual(events, expected)
|
||||
|
||||
# "When a setUp() method is defined, the test runner will run that method
|
||||
# prior to each test. Likewise, if a tearDown() method is defined, the
|
||||
# test runner will invoke that method after each test. In the example,
|
||||
# setUp() was used to create a fresh sequence for each test."
|
||||
#
|
||||
# Make sure the proper call order is maintained, even if the test signals
|
||||
# a failure (as opposed to an error).
|
||||
def test_run_call_order__failure_in_test(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
def setUp():
|
||||
events.append('setUp')
|
||||
|
||||
def test():
|
||||
events.append('test')
|
||||
self.fail('raised by test')
|
||||
|
||||
def tearDown():
|
||||
events.append('tearDown')
|
||||
|
||||
expected = ['startTest', 'setUp', 'test',
|
||||
'addFailure', 'tearDown', 'stopTest']
|
||||
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
|
||||
self.assertEqual(events, expected)
|
||||
|
||||
# "When a setUp() method is defined, the test runner will run that method
|
||||
# prior to each test. Likewise, if a tearDown() method is defined, the
|
||||
# test runner will invoke that method after each test. In the example,
|
||||
# setUp() was used to create a fresh sequence for each test."
|
||||
#
|
||||
# Make sure the proper call order is maintained, even if tearDown() raises
|
||||
# an exception.
|
||||
def test_run_call_order__error_in_tearDown(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
def setUp():
|
||||
events.append('setUp')
|
||||
|
||||
def test():
|
||||
events.append('test')
|
||||
|
||||
def tearDown():
|
||||
events.append('tearDown')
|
||||
raise RuntimeError('raised by tearDown')
|
||||
|
||||
expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
|
||||
'stopTest']
|
||||
unittest.FunctionTestCase(test, setUp, tearDown).run(result)
|
||||
self.assertEqual(events, expected)
|
||||
|
||||
# "Return a string identifying the specific test case."
|
||||
#
|
||||
# Because of the vague nature of the docs, I'm not going to lock this
|
||||
# test down too much. Really all that can be asserted is that the id()
|
||||
# will be a string (either 8-byte or unicode -- again, because the docs
|
||||
# just say "string")
|
||||
def test_id(self):
|
||||
test = unittest.FunctionTestCase(lambda: None)
|
||||
|
||||
self.assertIsInstance(test.id(), str)
|
||||
|
||||
# "Returns a one-line description of the test, or None if no description
|
||||
# has been provided. The default implementation of this method returns
|
||||
# the first line of the test method's docstring, if available, or None."
|
||||
def test_shortDescription__no_docstring(self):
|
||||
test = unittest.FunctionTestCase(lambda: None)
|
||||
|
||||
self.assertEqual(test.shortDescription(), None)
|
||||
|
||||
# "Returns a one-line description of the test, or None if no description
|
||||
# has been provided. The default implementation of this method returns
|
||||
# the first line of the test method's docstring, if available, or None."
|
||||
def test_shortDescription__singleline_docstring(self):
|
||||
desc = "this tests foo"
|
||||
test = unittest.FunctionTestCase(lambda: None, description=desc)
|
||||
|
||||
self.assertEqual(test.shortDescription(), "this tests foo")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
1642
.CondaPkg/env/Lib/unittest/test/test_loader.py
vendored
1642
.CondaPkg/env/Lib/unittest/test/test_loader.py
vendored
File diff suppressed because it is too large
Load Diff
477
.CondaPkg/env/Lib/unittest/test/test_program.py
vendored
477
.CondaPkg/env/Lib/unittest/test/test_program.py
vendored
@@ -1,477 +0,0 @@
|
||||
import io
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
from test import support
|
||||
import unittest
|
||||
import unittest.test
|
||||
from unittest.test.test_result import BufferedWriter
|
||||
|
||||
|
||||
class Test_TestProgram(unittest.TestCase):
|
||||
|
||||
def test_discovery_from_dotted_path(self):
|
||||
loader = unittest.TestLoader()
|
||||
|
||||
tests = [self]
|
||||
expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
|
||||
|
||||
self.wasRun = False
|
||||
def _find_tests(start_dir, pattern):
|
||||
self.wasRun = True
|
||||
self.assertEqual(start_dir, expectedPath)
|
||||
return tests
|
||||
loader._find_tests = _find_tests
|
||||
suite = loader.discover('unittest.test')
|
||||
self.assertTrue(self.wasRun)
|
||||
self.assertEqual(suite._tests, tests)
|
||||
|
||||
# Horrible white box test
|
||||
def testNoExit(self):
|
||||
result = object()
|
||||
test = object()
|
||||
|
||||
class FakeRunner(object):
|
||||
def run(self, test):
|
||||
self.test = test
|
||||
return result
|
||||
|
||||
runner = FakeRunner()
|
||||
|
||||
oldParseArgs = unittest.TestProgram.parseArgs
|
||||
def restoreParseArgs():
|
||||
unittest.TestProgram.parseArgs = oldParseArgs
|
||||
unittest.TestProgram.parseArgs = lambda *args: None
|
||||
self.addCleanup(restoreParseArgs)
|
||||
|
||||
def removeTest():
|
||||
del unittest.TestProgram.test
|
||||
unittest.TestProgram.test = test
|
||||
self.addCleanup(removeTest)
|
||||
|
||||
program = unittest.TestProgram(testRunner=runner, exit=False, verbosity=2)
|
||||
|
||||
self.assertEqual(program.result, result)
|
||||
self.assertEqual(runner.test, test)
|
||||
self.assertEqual(program.verbosity, 2)
|
||||
|
||||
class FooBar(unittest.TestCase):
|
||||
def testPass(self):
|
||||
pass
|
||||
def testFail(self):
|
||||
raise AssertionError
|
||||
def testError(self):
|
||||
1/0
|
||||
@unittest.skip('skipping')
|
||||
def testSkipped(self):
|
||||
raise AssertionError
|
||||
@unittest.expectedFailure
|
||||
def testExpectedFailure(self):
|
||||
raise AssertionError
|
||||
@unittest.expectedFailure
|
||||
def testUnexpectedSuccess(self):
|
||||
pass
|
||||
|
||||
class FooBarLoader(unittest.TestLoader):
|
||||
"""Test loader that returns a suite containing FooBar."""
|
||||
def loadTestsFromModule(self, module):
|
||||
return self.suiteClass(
|
||||
[self.loadTestsFromTestCase(Test_TestProgram.FooBar)])
|
||||
|
||||
def loadTestsFromNames(self, names, module):
|
||||
return self.suiteClass(
|
||||
[self.loadTestsFromTestCase(Test_TestProgram.FooBar)])
|
||||
|
||||
def test_defaultTest_with_string(self):
|
||||
class FakeRunner(object):
|
||||
def run(self, test):
|
||||
self.test = test
|
||||
return True
|
||||
|
||||
old_argv = sys.argv
|
||||
sys.argv = ['faketest']
|
||||
runner = FakeRunner()
|
||||
program = unittest.TestProgram(testRunner=runner, exit=False,
|
||||
defaultTest='unittest.test',
|
||||
testLoader=self.FooBarLoader())
|
||||
sys.argv = old_argv
|
||||
self.assertEqual(('unittest.test',), program.testNames)
|
||||
|
||||
def test_defaultTest_with_iterable(self):
|
||||
class FakeRunner(object):
|
||||
def run(self, test):
|
||||
self.test = test
|
||||
return True
|
||||
|
||||
old_argv = sys.argv
|
||||
sys.argv = ['faketest']
|
||||
runner = FakeRunner()
|
||||
program = unittest.TestProgram(
|
||||
testRunner=runner, exit=False,
|
||||
defaultTest=['unittest.test', 'unittest.test2'],
|
||||
testLoader=self.FooBarLoader())
|
||||
sys.argv = old_argv
|
||||
self.assertEqual(['unittest.test', 'unittest.test2'],
|
||||
program.testNames)
|
||||
|
||||
def test_NonExit(self):
|
||||
stream = BufferedWriter()
|
||||
program = unittest.main(exit=False,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
testLoader=self.FooBarLoader())
|
||||
self.assertTrue(hasattr(program, 'result'))
|
||||
out = stream.getvalue()
|
||||
self.assertIn('\nFAIL: testFail ', out)
|
||||
self.assertIn('\nERROR: testError ', out)
|
||||
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
||||
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
||||
'expected failures=1, unexpected successes=1)\n')
|
||||
self.assertTrue(out.endswith(expected))
|
||||
|
||||
def test_Exit(self):
|
||||
stream = BufferedWriter()
|
||||
self.assertRaises(
|
||||
SystemExit,
|
||||
unittest.main,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
exit=True,
|
||||
testLoader=self.FooBarLoader())
|
||||
out = stream.getvalue()
|
||||
self.assertIn('\nFAIL: testFail ', out)
|
||||
self.assertIn('\nERROR: testError ', out)
|
||||
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
||||
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
||||
'expected failures=1, unexpected successes=1)\n')
|
||||
self.assertTrue(out.endswith(expected))
|
||||
|
||||
def test_ExitAsDefault(self):
|
||||
stream = BufferedWriter()
|
||||
self.assertRaises(
|
||||
SystemExit,
|
||||
unittest.main,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
testLoader=self.FooBarLoader())
|
||||
out = stream.getvalue()
|
||||
self.assertIn('\nFAIL: testFail ', out)
|
||||
self.assertIn('\nERROR: testError ', out)
|
||||
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
||||
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
||||
'expected failures=1, unexpected successes=1)\n')
|
||||
self.assertTrue(out.endswith(expected))
|
||||
|
||||
|
||||
class InitialisableProgram(unittest.TestProgram):
|
||||
exit = False
|
||||
result = None
|
||||
verbosity = 1
|
||||
defaultTest = None
|
||||
tb_locals = False
|
||||
testRunner = None
|
||||
testLoader = unittest.defaultTestLoader
|
||||
module = '__main__'
|
||||
progName = 'test'
|
||||
test = 'test'
|
||||
def __init__(self, *args):
|
||||
pass
|
||||
|
||||
RESULT = object()
|
||||
|
||||
class FakeRunner(object):
|
||||
initArgs = None
|
||||
test = None
|
||||
raiseError = 0
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
FakeRunner.initArgs = kwargs
|
||||
if FakeRunner.raiseError:
|
||||
FakeRunner.raiseError -= 1
|
||||
raise TypeError
|
||||
|
||||
def run(self, test):
|
||||
FakeRunner.test = test
|
||||
return RESULT
|
||||
|
||||
|
||||
@support.requires_subprocess()
|
||||
class TestCommandLineArgs(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.program = InitialisableProgram()
|
||||
self.program.createTests = lambda: None
|
||||
FakeRunner.initArgs = None
|
||||
FakeRunner.test = None
|
||||
FakeRunner.raiseError = 0
|
||||
|
||||
def testVerbosity(self):
|
||||
program = self.program
|
||||
|
||||
for opt in '-q', '--quiet':
|
||||
program.verbosity = 1
|
||||
program.parseArgs([None, opt])
|
||||
self.assertEqual(program.verbosity, 0)
|
||||
|
||||
for opt in '-v', '--verbose':
|
||||
program.verbosity = 1
|
||||
program.parseArgs([None, opt])
|
||||
self.assertEqual(program.verbosity, 2)
|
||||
|
||||
def testBufferCatchFailfast(self):
|
||||
program = self.program
|
||||
for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'),
|
||||
('catch', 'catchbreak')):
|
||||
|
||||
setattr(program, attr, None)
|
||||
program.parseArgs([None])
|
||||
self.assertIs(getattr(program, attr), False)
|
||||
|
||||
false = []
|
||||
setattr(program, attr, false)
|
||||
program.parseArgs([None])
|
||||
self.assertIs(getattr(program, attr), false)
|
||||
|
||||
true = [42]
|
||||
setattr(program, attr, true)
|
||||
program.parseArgs([None])
|
||||
self.assertIs(getattr(program, attr), true)
|
||||
|
||||
short_opt = '-%s' % arg[0]
|
||||
long_opt = '--%s' % arg
|
||||
for opt in short_opt, long_opt:
|
||||
setattr(program, attr, None)
|
||||
program.parseArgs([None, opt])
|
||||
self.assertIs(getattr(program, attr), True)
|
||||
|
||||
setattr(program, attr, False)
|
||||
with support.captured_stderr() as stderr, \
|
||||
self.assertRaises(SystemExit) as cm:
|
||||
program.parseArgs([None, opt])
|
||||
self.assertEqual(cm.exception.args, (2,))
|
||||
|
||||
setattr(program, attr, True)
|
||||
with support.captured_stderr() as stderr, \
|
||||
self.assertRaises(SystemExit) as cm:
|
||||
program.parseArgs([None, opt])
|
||||
self.assertEqual(cm.exception.args, (2,))
|
||||
|
||||
def testWarning(self):
|
||||
"""Test the warnings argument"""
|
||||
# see #10535
|
||||
class FakeTP(unittest.TestProgram):
|
||||
def parseArgs(self, *args, **kw): pass
|
||||
def runTests(self, *args, **kw): pass
|
||||
warnoptions = sys.warnoptions[:]
|
||||
try:
|
||||
sys.warnoptions[:] = []
|
||||
# no warn options, no arg -> default
|
||||
self.assertEqual(FakeTP().warnings, 'default')
|
||||
# no warn options, w/ arg -> arg value
|
||||
self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
|
||||
sys.warnoptions[:] = ['somevalue']
|
||||
# warn options, no arg -> None
|
||||
# warn options, w/ arg -> arg value
|
||||
self.assertEqual(FakeTP().warnings, None)
|
||||
self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
|
||||
finally:
|
||||
sys.warnoptions[:] = warnoptions
|
||||
|
||||
def testRunTestsRunnerClass(self):
|
||||
program = self.program
|
||||
|
||||
program.testRunner = FakeRunner
|
||||
program.verbosity = 'verbosity'
|
||||
program.failfast = 'failfast'
|
||||
program.buffer = 'buffer'
|
||||
program.warnings = 'warnings'
|
||||
|
||||
program.runTests()
|
||||
|
||||
self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity',
|
||||
'failfast': 'failfast',
|
||||
'buffer': 'buffer',
|
||||
'tb_locals': False,
|
||||
'warnings': 'warnings'})
|
||||
self.assertEqual(FakeRunner.test, 'test')
|
||||
self.assertIs(program.result, RESULT)
|
||||
|
||||
def testRunTestsRunnerInstance(self):
|
||||
program = self.program
|
||||
|
||||
program.testRunner = FakeRunner()
|
||||
FakeRunner.initArgs = None
|
||||
|
||||
program.runTests()
|
||||
|
||||
# A new FakeRunner should not have been instantiated
|
||||
self.assertIsNone(FakeRunner.initArgs)
|
||||
|
||||
self.assertEqual(FakeRunner.test, 'test')
|
||||
self.assertIs(program.result, RESULT)
|
||||
|
||||
def test_locals(self):
|
||||
program = self.program
|
||||
|
||||
program.testRunner = FakeRunner
|
||||
program.parseArgs([None, '--locals'])
|
||||
self.assertEqual(True, program.tb_locals)
|
||||
program.runTests()
|
||||
self.assertEqual(FakeRunner.initArgs, {'buffer': False,
|
||||
'failfast': False,
|
||||
'tb_locals': True,
|
||||
'verbosity': 1,
|
||||
'warnings': None})
|
||||
|
||||
def testRunTestsOldRunnerClass(self):
|
||||
program = self.program
|
||||
|
||||
# Two TypeErrors are needed to fall all the way back to old-style
|
||||
# runners - one to fail tb_locals, one to fail buffer etc.
|
||||
FakeRunner.raiseError = 2
|
||||
program.testRunner = FakeRunner
|
||||
program.verbosity = 'verbosity'
|
||||
program.failfast = 'failfast'
|
||||
program.buffer = 'buffer'
|
||||
program.test = 'test'
|
||||
|
||||
program.runTests()
|
||||
|
||||
# If initialising raises a type error it should be retried
|
||||
# without the new keyword arguments
|
||||
self.assertEqual(FakeRunner.initArgs, {})
|
||||
self.assertEqual(FakeRunner.test, 'test')
|
||||
self.assertIs(program.result, RESULT)
|
||||
|
||||
def testCatchBreakInstallsHandler(self):
|
||||
module = sys.modules['unittest.main']
|
||||
original = module.installHandler
|
||||
def restore():
|
||||
module.installHandler = original
|
||||
self.addCleanup(restore)
|
||||
|
||||
self.installed = False
|
||||
def fakeInstallHandler():
|
||||
self.installed = True
|
||||
module.installHandler = fakeInstallHandler
|
||||
|
||||
program = self.program
|
||||
program.catchbreak = True
|
||||
|
||||
program.testRunner = FakeRunner
|
||||
|
||||
program.runTests()
|
||||
self.assertTrue(self.installed)
|
||||
|
||||
def _patch_isfile(self, names, exists=True):
|
||||
def isfile(path):
|
||||
return path in names
|
||||
original = os.path.isfile
|
||||
os.path.isfile = isfile
|
||||
def restore():
|
||||
os.path.isfile = original
|
||||
self.addCleanup(restore)
|
||||
|
||||
|
||||
def testParseArgsFileNames(self):
|
||||
# running tests with filenames instead of module names
|
||||
program = self.program
|
||||
argv = ['progname', 'foo.py', 'bar.Py', 'baz.PY', 'wing.txt']
|
||||
self._patch_isfile(argv)
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
# note that 'wing.txt' is not a Python file so the name should
|
||||
# *not* be converted to a module name
|
||||
expected = ['foo', 'bar', 'baz', 'wing.txt']
|
||||
self.assertEqual(program.testNames, expected)
|
||||
|
||||
|
||||
def testParseArgsFilePaths(self):
|
||||
program = self.program
|
||||
argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
|
||||
self._patch_isfile(argv)
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
expected = ['foo.bar.baz', 'green.red']
|
||||
self.assertEqual(program.testNames, expected)
|
||||
|
||||
|
||||
def testParseArgsNonExistentFiles(self):
|
||||
program = self.program
|
||||
argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
|
||||
self._patch_isfile([])
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
self.assertEqual(program.testNames, argv[1:])
|
||||
|
||||
def testParseArgsAbsolutePathsThatCanBeConverted(self):
|
||||
cur_dir = os.getcwd()
|
||||
program = self.program
|
||||
def _join(name):
|
||||
return os.path.join(cur_dir, name)
|
||||
argv = ['progname', _join('foo/bar/baz.py'), _join('green\\red.py')]
|
||||
self._patch_isfile(argv)
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
expected = ['foo.bar.baz', 'green.red']
|
||||
self.assertEqual(program.testNames, expected)
|
||||
|
||||
def testParseArgsAbsolutePathsThatCannotBeConverted(self):
|
||||
program = self.program
|
||||
# even on Windows '/...' is considered absolute by os.path.abspath
|
||||
argv = ['progname', '/foo/bar/baz.py', '/green/red.py']
|
||||
self._patch_isfile(argv)
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
self.assertEqual(program.testNames, argv[1:])
|
||||
|
||||
# it may be better to use platform specific functions to normalise paths
|
||||
# rather than accepting '.PY' and '\' as file separator on Linux / Mac
|
||||
# it would also be better to check that a filename is a valid module
|
||||
# identifier (we have a regex for this in loader.py)
|
||||
# for invalid filenames should we raise a useful error rather than
|
||||
# leaving the current error message (import of filename fails) in place?
|
||||
|
||||
def testParseArgsSelectedTestNames(self):
|
||||
program = self.program
|
||||
argv = ['progname', '-k', 'foo', '-k', 'bar', '-k', '*pat*']
|
||||
|
||||
program.createTests = lambda: None
|
||||
program.parseArgs(argv)
|
||||
|
||||
self.assertEqual(program.testNamePatterns, ['*foo*', '*bar*', '*pat*'])
|
||||
|
||||
def testSelectedTestNamesFunctionalTest(self):
|
||||
def run_unittest(args):
|
||||
# Use -E to ignore PYTHONSAFEPATH env var
|
||||
cmd = [sys.executable, '-E', '-m', 'unittest'] + args
|
||||
p = subprocess.Popen(cmd,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
|
||||
with p:
|
||||
_, stderr = p.communicate()
|
||||
return stderr.decode()
|
||||
|
||||
t = '_test_warnings'
|
||||
self.assertIn('Ran 7 tests', run_unittest([t]))
|
||||
self.assertIn('Ran 7 tests', run_unittest(['-k', 'TestWarnings', t]))
|
||||
self.assertIn('Ran 7 tests', run_unittest(['discover', '-p', '*_test*', '-k', 'TestWarnings']))
|
||||
self.assertIn('Ran 2 tests', run_unittest(['-k', 'f', t]))
|
||||
self.assertIn('Ran 7 tests', run_unittest(['-k', 't', t]))
|
||||
self.assertIn('Ran 3 tests', run_unittest(['-k', '*t', t]))
|
||||
self.assertIn('Ran 7 tests', run_unittest(['-k', '*test_warnings.*Warning*', t]))
|
||||
self.assertIn('Ran 1 test', run_unittest(['-k', '*test_warnings.*warning*', t]))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
1387
.CondaPkg/env/Lib/unittest/test/test_result.py
vendored
1387
.CondaPkg/env/Lib/unittest/test/test_result.py
vendored
File diff suppressed because it is too large
Load Diff
1371
.CondaPkg/env/Lib/unittest/test/test_runner.py
vendored
1371
.CondaPkg/env/Lib/unittest/test/test_runner.py
vendored
File diff suppressed because it is too large
Load Diff
507
.CondaPkg/env/Lib/unittest/test/test_setups.py
vendored
507
.CondaPkg/env/Lib/unittest/test/test_setups.py
vendored
@@ -1,507 +0,0 @@
|
||||
import io
|
||||
import sys
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
def resultFactory(*_):
|
||||
return unittest.TestResult()
|
||||
|
||||
|
||||
class TestSetups(unittest.TestCase):
|
||||
|
||||
def getRunner(self):
|
||||
return unittest.TextTestRunner(resultclass=resultFactory,
|
||||
stream=io.StringIO())
|
||||
def runTests(self, *cases):
|
||||
suite = unittest.TestSuite()
|
||||
for case in cases:
|
||||
tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
|
||||
suite.addTests(tests)
|
||||
|
||||
runner = self.getRunner()
|
||||
|
||||
# creating a nested suite exposes some potential bugs
|
||||
realSuite = unittest.TestSuite()
|
||||
realSuite.addTest(suite)
|
||||
# adding empty suites to the end exposes potential bugs
|
||||
suite.addTest(unittest.TestSuite())
|
||||
realSuite.addTest(unittest.TestSuite())
|
||||
return runner.run(realSuite)
|
||||
|
||||
def test_setup_class(self):
|
||||
class Test(unittest.TestCase):
|
||||
setUpCalled = 0
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
Test.setUpCalled += 1
|
||||
unittest.TestCase.setUpClass()
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(Test)
|
||||
|
||||
self.assertEqual(Test.setUpCalled, 1)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
def test_teardown_class(self):
|
||||
class Test(unittest.TestCase):
|
||||
tearDownCalled = 0
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.tearDownCalled += 1
|
||||
unittest.TestCase.tearDownClass()
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(Test)
|
||||
|
||||
self.assertEqual(Test.tearDownCalled, 1)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
def test_teardown_class_two_classes(self):
|
||||
class Test(unittest.TestCase):
|
||||
tearDownCalled = 0
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.tearDownCalled += 1
|
||||
unittest.TestCase.tearDownClass()
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
class Test2(unittest.TestCase):
|
||||
tearDownCalled = 0
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test2.tearDownCalled += 1
|
||||
unittest.TestCase.tearDownClass()
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(Test, Test2)
|
||||
|
||||
self.assertEqual(Test.tearDownCalled, 1)
|
||||
self.assertEqual(Test2.tearDownCalled, 1)
|
||||
self.assertEqual(result.testsRun, 4)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
def test_error_in_setupclass(self):
|
||||
class BrokenTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
raise TypeError('foo')
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(BrokenTest)
|
||||
|
||||
self.assertEqual(result.testsRun, 0)
|
||||
self.assertEqual(len(result.errors), 1)
|
||||
error, _ = result.errors[0]
|
||||
self.assertEqual(str(error),
|
||||
'setUpClass (%s.%s)' % (__name__, BrokenTest.__qualname__))
|
||||
|
||||
def test_error_in_teardown_class(self):
|
||||
class Test(unittest.TestCase):
|
||||
tornDown = 0
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.tornDown += 1
|
||||
raise TypeError('foo')
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
class Test2(unittest.TestCase):
|
||||
tornDown = 0
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test2.tornDown += 1
|
||||
raise TypeError('foo')
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(Test, Test2)
|
||||
self.assertEqual(result.testsRun, 4)
|
||||
self.assertEqual(len(result.errors), 2)
|
||||
self.assertEqual(Test.tornDown, 1)
|
||||
self.assertEqual(Test2.tornDown, 1)
|
||||
|
||||
error, _ = result.errors[0]
|
||||
self.assertEqual(str(error),
|
||||
'tearDownClass (%s.%s)' % (__name__, Test.__qualname__))
|
||||
|
||||
def test_class_not_torndown_when_setup_fails(self):
|
||||
class Test(unittest.TestCase):
|
||||
tornDown = False
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
raise TypeError
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.tornDown = True
|
||||
raise TypeError('foo')
|
||||
def test_one(self):
|
||||
pass
|
||||
|
||||
self.runTests(Test)
|
||||
self.assertFalse(Test.tornDown)
|
||||
|
||||
def test_class_not_setup_or_torndown_when_skipped(self):
|
||||
class Test(unittest.TestCase):
|
||||
classSetUp = False
|
||||
tornDown = False
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
Test.classSetUp = True
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.tornDown = True
|
||||
def test_one(self):
|
||||
pass
|
||||
|
||||
Test = unittest.skip("hop")(Test)
|
||||
self.runTests(Test)
|
||||
self.assertFalse(Test.classSetUp)
|
||||
self.assertFalse(Test.tornDown)
|
||||
|
||||
def test_setup_teardown_order_with_pathological_suite(self):
|
||||
results = []
|
||||
|
||||
class Module1(object):
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
results.append('Module1.setUpModule')
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
results.append('Module1.tearDownModule')
|
||||
|
||||
class Module2(object):
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
results.append('Module2.setUpModule')
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
results.append('Module2.tearDownModule')
|
||||
|
||||
class Test1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
results.append('setup 1')
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
results.append('teardown 1')
|
||||
def testOne(self):
|
||||
results.append('Test1.testOne')
|
||||
def testTwo(self):
|
||||
results.append('Test1.testTwo')
|
||||
|
||||
class Test2(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
results.append('setup 2')
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
results.append('teardown 2')
|
||||
def testOne(self):
|
||||
results.append('Test2.testOne')
|
||||
def testTwo(self):
|
||||
results.append('Test2.testTwo')
|
||||
|
||||
class Test3(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
results.append('setup 3')
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
results.append('teardown 3')
|
||||
def testOne(self):
|
||||
results.append('Test3.testOne')
|
||||
def testTwo(self):
|
||||
results.append('Test3.testTwo')
|
||||
|
||||
Test1.__module__ = Test2.__module__ = 'Module'
|
||||
Test3.__module__ = 'Module2'
|
||||
sys.modules['Module'] = Module1
|
||||
sys.modules['Module2'] = Module2
|
||||
|
||||
first = unittest.TestSuite((Test1('testOne'),))
|
||||
second = unittest.TestSuite((Test1('testTwo'),))
|
||||
third = unittest.TestSuite((Test2('testOne'),))
|
||||
fourth = unittest.TestSuite((Test2('testTwo'),))
|
||||
fifth = unittest.TestSuite((Test3('testOne'),))
|
||||
sixth = unittest.TestSuite((Test3('testTwo'),))
|
||||
suite = unittest.TestSuite((first, second, third, fourth, fifth, sixth))
|
||||
|
||||
runner = self.getRunner()
|
||||
result = runner.run(suite)
|
||||
self.assertEqual(result.testsRun, 6)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
self.assertEqual(results,
|
||||
['Module1.setUpModule', 'setup 1',
|
||||
'Test1.testOne', 'Test1.testTwo', 'teardown 1',
|
||||
'setup 2', 'Test2.testOne', 'Test2.testTwo',
|
||||
'teardown 2', 'Module1.tearDownModule',
|
||||
'Module2.setUpModule', 'setup 3',
|
||||
'Test3.testOne', 'Test3.testTwo',
|
||||
'teardown 3', 'Module2.tearDownModule'])
|
||||
|
||||
def test_setup_module(self):
|
||||
class Module(object):
|
||||
moduleSetup = 0
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
Module.moduleSetup += 1
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
result = self.runTests(Test)
|
||||
self.assertEqual(Module.moduleSetup, 1)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
def test_error_in_setup_module(self):
|
||||
class Module(object):
|
||||
moduleSetup = 0
|
||||
moduleTornDown = 0
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
Module.moduleSetup += 1
|
||||
raise TypeError('foo')
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
Module.moduleTornDown += 1
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
classSetUp = False
|
||||
classTornDown = False
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
Test.classSetUp = True
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.classTornDown = True
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
class Test2(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
Test.__module__ = 'Module'
|
||||
Test2.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
result = self.runTests(Test, Test2)
|
||||
self.assertEqual(Module.moduleSetup, 1)
|
||||
self.assertEqual(Module.moduleTornDown, 0)
|
||||
self.assertEqual(result.testsRun, 0)
|
||||
self.assertFalse(Test.classSetUp)
|
||||
self.assertFalse(Test.classTornDown)
|
||||
self.assertEqual(len(result.errors), 1)
|
||||
error, _ = result.errors[0]
|
||||
self.assertEqual(str(error), 'setUpModule (Module)')
|
||||
|
||||
def test_testcase_with_missing_module(self):
|
||||
class Test(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules.pop('Module', None)
|
||||
|
||||
result = self.runTests(Test)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
|
||||
def test_teardown_module(self):
|
||||
class Module(object):
|
||||
moduleTornDown = 0
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
Module.moduleTornDown += 1
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
result = self.runTests(Test)
|
||||
self.assertEqual(Module.moduleTornDown, 1)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
|
||||
def test_error_in_teardown_module(self):
|
||||
class Module(object):
|
||||
moduleTornDown = 0
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
Module.moduleTornDown += 1
|
||||
raise TypeError('foo')
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
classSetUp = False
|
||||
classTornDown = False
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
Test.classSetUp = True
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
Test.classTornDown = True
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
class Test2(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
Test.__module__ = 'Module'
|
||||
Test2.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
result = self.runTests(Test, Test2)
|
||||
self.assertEqual(Module.moduleTornDown, 1)
|
||||
self.assertEqual(result.testsRun, 4)
|
||||
self.assertTrue(Test.classSetUp)
|
||||
self.assertTrue(Test.classTornDown)
|
||||
self.assertEqual(len(result.errors), 1)
|
||||
error, _ = result.errors[0]
|
||||
self.assertEqual(str(error), 'tearDownModule (Module)')
|
||||
|
||||
def test_skiptest_in_setupclass(self):
|
||||
class Test(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
raise unittest.SkipTest('foo')
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
result = self.runTests(Test)
|
||||
self.assertEqual(result.testsRun, 0)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
self.assertEqual(len(result.skipped), 1)
|
||||
skipped = result.skipped[0][0]
|
||||
self.assertEqual(str(skipped),
|
||||
'setUpClass (%s.%s)' % (__name__, Test.__qualname__))
|
||||
|
||||
def test_skiptest_in_setupmodule(self):
|
||||
class Test(unittest.TestCase):
|
||||
def test_one(self):
|
||||
pass
|
||||
def test_two(self):
|
||||
pass
|
||||
|
||||
class Module(object):
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
raise unittest.SkipTest('foo')
|
||||
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
result = self.runTests(Test)
|
||||
self.assertEqual(result.testsRun, 0)
|
||||
self.assertEqual(len(result.errors), 0)
|
||||
self.assertEqual(len(result.skipped), 1)
|
||||
skipped = result.skipped[0][0]
|
||||
self.assertEqual(str(skipped), 'setUpModule (Module)')
|
||||
|
||||
def test_suite_debug_executes_setups_and_teardowns(self):
|
||||
ordering = []
|
||||
|
||||
class Module(object):
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
ordering.append('setUpModule')
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
ordering.append('tearDownModule')
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
ordering.append('setUpClass')
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
ordering.append('tearDownClass')
|
||||
def test_something(self):
|
||||
ordering.append('test_something')
|
||||
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
|
||||
suite.debug()
|
||||
expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule']
|
||||
self.assertEqual(ordering, expectedOrder)
|
||||
|
||||
def test_suite_debug_propagates_exceptions(self):
|
||||
class Module(object):
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
if phase == 0:
|
||||
raise Exception('setUpModule')
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
if phase == 1:
|
||||
raise Exception('tearDownModule')
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if phase == 2:
|
||||
raise Exception('setUpClass')
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
if phase == 3:
|
||||
raise Exception('tearDownClass')
|
||||
def test_something(self):
|
||||
if phase == 4:
|
||||
raise Exception('test_something')
|
||||
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
|
||||
messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something')
|
||||
for phase, msg in enumerate(messages):
|
||||
_suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
|
||||
suite = unittest.TestSuite([_suite])
|
||||
with self.assertRaisesRegex(Exception, msg):
|
||||
suite.debug()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
530
.CondaPkg/env/Lib/unittest/test/test_skipping.py
vendored
530
.CondaPkg/env/Lib/unittest/test/test_skipping.py
vendored
@@ -1,530 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from unittest.test.support import LoggingResult
|
||||
|
||||
|
||||
class Test_TestSkipping(unittest.TestCase):
|
||||
|
||||
def test_skipping(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def defaultTestResult(self):
|
||||
return LoggingResult(events)
|
||||
def test_skip_me(self):
|
||||
self.skipTest("skip")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
|
||||
events = []
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
|
||||
'stopTest', 'stopTestRun'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
self.assertEqual(result.testsRun, 1)
|
||||
|
||||
# Try letting setUp skip the test now.
|
||||
class Foo(unittest.TestCase):
|
||||
def defaultTestResult(self):
|
||||
return LoggingResult(events)
|
||||
def setUp(self):
|
||||
self.skipTest("testing")
|
||||
def test_nothing(self): pass
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_nothing")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertEqual(result.testsRun, 1)
|
||||
|
||||
events = []
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
|
||||
'stopTest', 'stopTestRun'])
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertEqual(result.testsRun, 1)
|
||||
|
||||
def test_skipping_subtests(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def defaultTestResult(self):
|
||||
return LoggingResult(events)
|
||||
def test_skip_me(self):
|
||||
with self.subTest(a=1):
|
||||
with self.subTest(b=2):
|
||||
self.skipTest("skip 1")
|
||||
self.skipTest("skip 2")
|
||||
self.skipTest("skip 3")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'addSkip',
|
||||
'addSkip', 'stopTest'])
|
||||
self.assertEqual(len(result.skipped), 3)
|
||||
subtest, msg = result.skipped[0]
|
||||
self.assertEqual(msg, "skip 1")
|
||||
self.assertIsInstance(subtest, unittest.TestCase)
|
||||
self.assertIsNot(subtest, test)
|
||||
subtest, msg = result.skipped[1]
|
||||
self.assertEqual(msg, "skip 2")
|
||||
self.assertIsInstance(subtest, unittest.TestCase)
|
||||
self.assertIsNot(subtest, test)
|
||||
self.assertEqual(result.skipped[2], (test, "skip 3"))
|
||||
|
||||
events = []
|
||||
result = test.run()
|
||||
self.assertEqual(events,
|
||||
['startTestRun', 'startTest', 'addSkip', 'addSkip',
|
||||
'addSkip', 'stopTest', 'stopTestRun'])
|
||||
self.assertEqual([msg for subtest, msg in result.skipped],
|
||||
['skip 1', 'skip 2', 'skip 3'])
|
||||
|
||||
def test_skipping_decorators(self):
|
||||
op_table = ((unittest.skipUnless, False, True),
|
||||
(unittest.skipIf, True, False))
|
||||
for deco, do_skip, dont_skip in op_table:
|
||||
class Foo(unittest.TestCase):
|
||||
def defaultTestResult(self):
|
||||
return LoggingResult(events)
|
||||
|
||||
@deco(do_skip, "testing")
|
||||
def test_skip(self): pass
|
||||
|
||||
@deco(dont_skip, "testing")
|
||||
def test_dont_skip(self): pass
|
||||
test_do_skip = Foo("test_skip")
|
||||
test_dont_skip = Foo("test_dont_skip")
|
||||
|
||||
suite = unittest.TestSuite([test_do_skip, test_dont_skip])
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(len(result.skipped), 1)
|
||||
expected = ['startTest', 'addSkip', 'stopTest',
|
||||
'startTest', 'addSuccess', 'stopTest']
|
||||
self.assertEqual(events, expected)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(result.skipped, [(test_do_skip, "testing")])
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
events = []
|
||||
result = test_do_skip.run()
|
||||
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
|
||||
'stopTest', 'stopTestRun'])
|
||||
self.assertEqual(result.skipped, [(test_do_skip, "testing")])
|
||||
|
||||
events = []
|
||||
result = test_dont_skip.run()
|
||||
self.assertEqual(events, ['startTestRun', 'startTest', 'addSuccess',
|
||||
'stopTest', 'stopTestRun'])
|
||||
self.assertEqual(result.skipped, [])
|
||||
|
||||
def test_skip_class(self):
|
||||
@unittest.skip("testing")
|
||||
class Foo(unittest.TestCase):
|
||||
def defaultTestResult(self):
|
||||
return LoggingResult(events)
|
||||
def test_1(self):
|
||||
record.append(1)
|
||||
events = []
|
||||
record = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_1")
|
||||
suite = unittest.TestSuite([test])
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertEqual(record, [])
|
||||
|
||||
events = []
|
||||
result = test.run()
|
||||
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
|
||||
'stopTest', 'stopTestRun'])
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertEqual(record, [])
|
||||
|
||||
def test_skip_non_unittest_class(self):
|
||||
@unittest.skip("testing")
|
||||
class Mixin:
|
||||
def test_1(self):
|
||||
record.append(1)
|
||||
class Foo(Mixin, unittest.TestCase):
|
||||
pass
|
||||
record = []
|
||||
result = unittest.TestResult()
|
||||
test = Foo("test_1")
|
||||
suite = unittest.TestSuite([test])
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertEqual(record, [])
|
||||
|
||||
def test_skip_in_setup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.skipTest("skip")
|
||||
def test_skip_me(self):
|
||||
self.fail("shouldn't come here")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
|
||||
def test_skip_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_skip_me(self):
|
||||
pass
|
||||
def tearDown(self):
|
||||
self.skipTest("skip")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
|
||||
def test_failure_and_skip_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_skip_me(self):
|
||||
self.fail("fail")
|
||||
def tearDown(self):
|
||||
self.skipTest("skip")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addFailure', 'addSkip', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
|
||||
def test_skipping_and_fail_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_skip_me(self):
|
||||
self.skipTest("skip")
|
||||
def tearDown(self):
|
||||
self.fail("fail")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_skip_me")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events, ['startTest', 'addSkip', 'addFailure', 'stopTest'])
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
|
||||
def test_expected_failure(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
self.fail("help me!")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addExpectedFailure', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertEqual(result.expectedFailures[0][0], test)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_expected_failure_with_wrapped_class(self):
|
||||
@unittest.expectedFailure
|
||||
class Foo(unittest.TestCase):
|
||||
def test_1(self):
|
||||
self.assertTrue(False)
|
||||
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_1")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addExpectedFailure', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertEqual(result.expectedFailures[0][0], test)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_expected_failure_with_wrapped_subclass(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_1(self):
|
||||
self.assertTrue(False)
|
||||
|
||||
@unittest.expectedFailure
|
||||
class Bar(Foo):
|
||||
pass
|
||||
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Bar("test_1")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addExpectedFailure', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertEqual(result.expectedFailures[0][0], test)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_expected_failure_subtests(self):
|
||||
# A failure in any subtest counts as the expected failure of the
|
||||
# whole test.
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
with self.subTest():
|
||||
# This one succeeds
|
||||
pass
|
||||
with self.subTest():
|
||||
self.fail("help me!")
|
||||
with self.subTest():
|
||||
# This one doesn't get executed
|
||||
self.fail("shouldn't come here")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addSubTestSuccess',
|
||||
'addExpectedFailure', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertEqual(len(result.expectedFailures), 1)
|
||||
self.assertIs(result.expectedFailures[0][0], test)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_expected_failure_and_fail_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
self.fail("help me!")
|
||||
def tearDown(self):
|
||||
self.fail("bad tearDown")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addFailure', 'stopTest'])
|
||||
self.assertEqual(len(result.failures), 1)
|
||||
self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertFalse(result.wasSuccessful())
|
||||
|
||||
def test_expected_failure_and_skip_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
self.fail("help me!")
|
||||
def tearDown(self):
|
||||
self.skipTest("skip")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_unexpected_success(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
pass
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addUnexpectedSuccess', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertEqual(result.unexpectedSuccesses, [test])
|
||||
self.assertFalse(result.wasSuccessful())
|
||||
|
||||
def test_unexpected_success_subtests(self):
|
||||
# Success in all subtests counts as the unexpected success of
|
||||
# the whole test.
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
with self.subTest():
|
||||
# This one succeeds
|
||||
pass
|
||||
with self.subTest():
|
||||
# So does this one
|
||||
pass
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest',
|
||||
'addSubTestSuccess', 'addSubTestSuccess',
|
||||
'addUnexpectedSuccess', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertEqual(result.unexpectedSuccesses, [test])
|
||||
self.assertFalse(result.wasSuccessful())
|
||||
|
||||
def test_unexpected_success_and_fail_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
pass
|
||||
def tearDown(self):
|
||||
self.fail("bad tearDown")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addFailure', 'stopTest'])
|
||||
self.assertEqual(len(result.failures), 1)
|
||||
self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertFalse(result.wasSuccessful())
|
||||
|
||||
def test_unexpected_success_and_skip_in_cleanup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.expectedFailure
|
||||
def test_die(self):
|
||||
pass
|
||||
def tearDown(self):
|
||||
self.skipTest("skip")
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test_die")
|
||||
self.assertIs(test.run(result), result)
|
||||
self.assertEqual(events,
|
||||
['startTest', 'addSkip', 'stopTest'])
|
||||
self.assertFalse(result.failures)
|
||||
self.assertFalse(result.expectedFailures)
|
||||
self.assertFalse(result.unexpectedSuccesses)
|
||||
self.assertEqual(result.skipped, [(test, "skip")])
|
||||
self.assertTrue(result.wasSuccessful())
|
||||
|
||||
def test_skip_doesnt_run_setup(self):
|
||||
class Foo(unittest.TestCase):
|
||||
wasSetUp = False
|
||||
wasTornDown = False
|
||||
def setUp(self):
|
||||
Foo.wasSetUp = True
|
||||
def tornDown(self):
|
||||
Foo.wasTornDown = True
|
||||
@unittest.skip('testing')
|
||||
def test_1(self):
|
||||
pass
|
||||
|
||||
result = unittest.TestResult()
|
||||
test = Foo("test_1")
|
||||
suite = unittest.TestSuite([test])
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
self.assertFalse(Foo.wasSetUp)
|
||||
self.assertFalse(Foo.wasTornDown)
|
||||
|
||||
def test_decorated_skip(self):
|
||||
def decorator(func):
|
||||
def inner(*a):
|
||||
return func(*a)
|
||||
return inner
|
||||
|
||||
class Foo(unittest.TestCase):
|
||||
@decorator
|
||||
@unittest.skip('testing')
|
||||
def test_1(self):
|
||||
pass
|
||||
|
||||
result = unittest.TestResult()
|
||||
test = Foo("test_1")
|
||||
suite = unittest.TestSuite([test])
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(result.skipped, [(test, "testing")])
|
||||
|
||||
def test_skip_without_reason(self):
|
||||
class Foo(unittest.TestCase):
|
||||
@unittest.skip
|
||||
def test_1(self):
|
||||
pass
|
||||
|
||||
result = unittest.TestResult()
|
||||
test = Foo("test_1")
|
||||
suite = unittest.TestSuite([test])
|
||||
self.assertIs(suite.run(result), result)
|
||||
self.assertEqual(result.skipped, [(test, "")])
|
||||
|
||||
def test_debug_skipping(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def setUp(self):
|
||||
events.append("setUp")
|
||||
def tearDown(self):
|
||||
events.append("tearDown")
|
||||
def test1(self):
|
||||
self.skipTest('skipping exception')
|
||||
events.append("test1")
|
||||
@unittest.skip("skipping decorator")
|
||||
def test2(self):
|
||||
events.append("test2")
|
||||
|
||||
events = []
|
||||
test = Foo("test1")
|
||||
with self.assertRaises(unittest.SkipTest) as cm:
|
||||
test.debug()
|
||||
self.assertIn("skipping exception", str(cm.exception))
|
||||
self.assertEqual(events, ["setUp"])
|
||||
|
||||
events = []
|
||||
test = Foo("test2")
|
||||
with self.assertRaises(unittest.SkipTest) as cm:
|
||||
test.debug()
|
||||
self.assertIn("skipping decorator", str(cm.exception))
|
||||
self.assertEqual(events, [])
|
||||
|
||||
def test_debug_skipping_class(self):
|
||||
@unittest.skip("testing")
|
||||
class Foo(unittest.TestCase):
|
||||
def setUp(self):
|
||||
events.append("setUp")
|
||||
def tearDown(self):
|
||||
events.append("tearDown")
|
||||
def test(self):
|
||||
events.append("test")
|
||||
|
||||
events = []
|
||||
test = Foo("test")
|
||||
with self.assertRaises(unittest.SkipTest) as cm:
|
||||
test.debug()
|
||||
self.assertIn("testing", str(cm.exception))
|
||||
self.assertEqual(events, [])
|
||||
|
||||
def test_debug_skipping_subtests(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def setUp(self):
|
||||
events.append("setUp")
|
||||
def tearDown(self):
|
||||
events.append("tearDown")
|
||||
def test(self):
|
||||
with self.subTest(a=1):
|
||||
events.append('subtest')
|
||||
self.skipTest("skip subtest")
|
||||
events.append('end subtest')
|
||||
events.append('end test')
|
||||
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
test = Foo("test")
|
||||
with self.assertRaises(unittest.SkipTest) as cm:
|
||||
test.debug()
|
||||
self.assertIn("skip subtest", str(cm.exception))
|
||||
self.assertEqual(events, ['setUp', 'subtest'])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
447
.CondaPkg/env/Lib/unittest/test/test_suite.py
vendored
447
.CondaPkg/env/Lib/unittest/test/test_suite.py
vendored
@@ -1,447 +0,0 @@
|
||||
import unittest
|
||||
|
||||
import gc
|
||||
import sys
|
||||
import weakref
|
||||
from unittest.test.support import LoggingResult, TestEquality
|
||||
|
||||
|
||||
### Support code for Test_TestSuite
|
||||
################################################################
|
||||
|
||||
class Test(object):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_1(self): pass
|
||||
def test_2(self): pass
|
||||
def test_3(self): pass
|
||||
def runTest(self): pass
|
||||
|
||||
def _mk_TestSuite(*names):
|
||||
return unittest.TestSuite(Test.Foo(n) for n in names)
|
||||
|
||||
################################################################
|
||||
|
||||
|
||||
class Test_TestSuite(unittest.TestCase, TestEquality):
|
||||
|
||||
### Set up attributes needed by inherited tests
|
||||
################################################################
|
||||
|
||||
# Used by TestEquality.test_eq
|
||||
eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
|
||||
,(unittest.TestSuite(), unittest.TestSuite([]))
|
||||
,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
|
||||
|
||||
# Used by TestEquality.test_ne
|
||||
ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
|
||||
,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
|
||||
,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
|
||||
,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
|
||||
|
||||
################################################################
|
||||
### /Set up attributes needed by inherited tests
|
||||
|
||||
### Tests for TestSuite.__init__
|
||||
################################################################
|
||||
|
||||
# "class TestSuite([tests])"
|
||||
#
|
||||
# The tests iterable should be optional
|
||||
def test_init__tests_optional(self):
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
|
||||
# "class TestSuite([tests])"
|
||||
# ...
|
||||
# "If tests is given, it must be an iterable of individual test cases
|
||||
# or other test suites that will be used to build the suite initially"
|
||||
#
|
||||
# TestSuite should deal with empty tests iterables by allowing the
|
||||
# creation of an empty suite
|
||||
def test_init__empty_tests(self):
|
||||
suite = unittest.TestSuite([])
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
|
||||
# "class TestSuite([tests])"
|
||||
# ...
|
||||
# "If tests is given, it must be an iterable of individual test cases
|
||||
# or other test suites that will be used to build the suite initially"
|
||||
#
|
||||
# TestSuite should allow any iterable to provide tests
|
||||
def test_init__tests_from_any_iterable(self):
|
||||
def tests():
|
||||
yield unittest.FunctionTestCase(lambda: None)
|
||||
yield unittest.FunctionTestCase(lambda: None)
|
||||
|
||||
suite_1 = unittest.TestSuite(tests())
|
||||
self.assertEqual(suite_1.countTestCases(), 2)
|
||||
|
||||
suite_2 = unittest.TestSuite(suite_1)
|
||||
self.assertEqual(suite_2.countTestCases(), 2)
|
||||
|
||||
suite_3 = unittest.TestSuite(set(suite_1))
|
||||
self.assertEqual(suite_3.countTestCases(), 2)
|
||||
|
||||
# countTestCases() still works after tests are run
|
||||
suite_1.run(unittest.TestResult())
|
||||
self.assertEqual(suite_1.countTestCases(), 2)
|
||||
suite_2.run(unittest.TestResult())
|
||||
self.assertEqual(suite_2.countTestCases(), 2)
|
||||
suite_3.run(unittest.TestResult())
|
||||
self.assertEqual(suite_3.countTestCases(), 2)
|
||||
|
||||
# "class TestSuite([tests])"
|
||||
# ...
|
||||
# "If tests is given, it must be an iterable of individual test cases
|
||||
# or other test suites that will be used to build the suite initially"
|
||||
#
|
||||
# Does TestSuite() also allow other TestSuite() instances to be present
|
||||
# in the tests iterable?
|
||||
def test_init__TestSuite_instances_in_tests(self):
|
||||
def tests():
|
||||
ftc = unittest.FunctionTestCase(lambda: None)
|
||||
yield unittest.TestSuite([ftc])
|
||||
yield unittest.FunctionTestCase(lambda: None)
|
||||
|
||||
suite = unittest.TestSuite(tests())
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
|
||||
################################################################
|
||||
### /Tests for TestSuite.__init__
|
||||
|
||||
# Container types should support the iter protocol
|
||||
def test_iter(self):
|
||||
test1 = unittest.FunctionTestCase(lambda: None)
|
||||
test2 = unittest.FunctionTestCase(lambda: None)
|
||||
suite = unittest.TestSuite((test1, test2))
|
||||
|
||||
self.assertEqual(list(suite), [test1, test2])
|
||||
|
||||
# "Return the number of tests represented by the this test object.
|
||||
# ...this method is also implemented by the TestSuite class, which can
|
||||
# return larger [greater than 1] values"
|
||||
#
|
||||
# Presumably an empty TestSuite returns 0?
|
||||
def test_countTestCases_zero_simple(self):
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
|
||||
# "Return the number of tests represented by the this test object.
|
||||
# ...this method is also implemented by the TestSuite class, which can
|
||||
# return larger [greater than 1] values"
|
||||
#
|
||||
# Presumably an empty TestSuite (even if it contains other empty
|
||||
# TestSuite instances) returns 0?
|
||||
def test_countTestCases_zero_nested(self):
|
||||
class Test1(unittest.TestCase):
|
||||
def test(self):
|
||||
pass
|
||||
|
||||
suite = unittest.TestSuite([unittest.TestSuite()])
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 0)
|
||||
|
||||
# "Return the number of tests represented by the this test object.
|
||||
# ...this method is also implemented by the TestSuite class, which can
|
||||
# return larger [greater than 1] values"
|
||||
def test_countTestCases_simple(self):
|
||||
test1 = unittest.FunctionTestCase(lambda: None)
|
||||
test2 = unittest.FunctionTestCase(lambda: None)
|
||||
suite = unittest.TestSuite((test1, test2))
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
|
||||
# "Return the number of tests represented by the this test object.
|
||||
# ...this method is also implemented by the TestSuite class, which can
|
||||
# return larger [greater than 1] values"
|
||||
#
|
||||
# Make sure this holds for nested TestSuite instances, too
|
||||
def test_countTestCases_nested(self):
|
||||
class Test1(unittest.TestCase):
|
||||
def test1(self): pass
|
||||
def test2(self): pass
|
||||
|
||||
test2 = unittest.FunctionTestCase(lambda: None)
|
||||
test3 = unittest.FunctionTestCase(lambda: None)
|
||||
child = unittest.TestSuite((Test1('test2'), test2))
|
||||
parent = unittest.TestSuite((test3, child, Test1('test1')))
|
||||
|
||||
self.assertEqual(parent.countTestCases(), 4)
|
||||
# countTestCases() still works after tests are run
|
||||
parent.run(unittest.TestResult())
|
||||
self.assertEqual(parent.countTestCases(), 4)
|
||||
self.assertEqual(child.countTestCases(), 2)
|
||||
|
||||
# "Run the tests associated with this suite, collecting the result into
|
||||
# the test result object passed as result."
|
||||
#
|
||||
# And if there are no tests? What then?
|
||||
def test_run__empty_suite(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
suite.run(result)
|
||||
|
||||
self.assertEqual(events, [])
|
||||
|
||||
# "Note that unlike TestCase.run(), TestSuite.run() requires the
|
||||
# "result object to be passed in."
|
||||
def test_run__requires_result(self):
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
try:
|
||||
suite.run()
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
self.fail("Failed to raise TypeError")
|
||||
|
||||
# "Run the tests associated with this suite, collecting the result into
|
||||
# the test result object passed as result."
|
||||
def test_run(self):
|
||||
events = []
|
||||
result = LoggingResult(events)
|
||||
|
||||
class LoggingCase(unittest.TestCase):
|
||||
def run(self, result):
|
||||
events.append('run %s' % self._testMethodName)
|
||||
|
||||
def test1(self): pass
|
||||
def test2(self): pass
|
||||
|
||||
tests = [LoggingCase('test1'), LoggingCase('test2')]
|
||||
|
||||
unittest.TestSuite(tests).run(result)
|
||||
|
||||
self.assertEqual(events, ['run test1', 'run test2'])
|
||||
|
||||
# "Add a TestCase ... to the suite"
|
||||
def test_addTest__TestCase(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test(self): pass
|
||||
|
||||
test = Foo('test')
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
suite.addTest(test)
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
self.assertEqual(list(suite), [test])
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
|
||||
# "Add a ... TestSuite to the suite"
|
||||
def test_addTest__TestSuite(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test(self): pass
|
||||
|
||||
suite_2 = unittest.TestSuite([Foo('test')])
|
||||
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(suite_2)
|
||||
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
self.assertEqual(list(suite), [suite_2])
|
||||
# countTestCases() still works after tests are run
|
||||
suite.run(unittest.TestResult())
|
||||
self.assertEqual(suite.countTestCases(), 1)
|
||||
|
||||
# "Add all the tests from an iterable of TestCase and TestSuite
|
||||
# instances to this test suite."
|
||||
#
|
||||
# "This is equivalent to iterating over tests, calling addTest() for
|
||||
# each element"
|
||||
def test_addTests(self):
|
||||
class Foo(unittest.TestCase):
|
||||
def test_1(self): pass
|
||||
def test_2(self): pass
|
||||
|
||||
test_1 = Foo('test_1')
|
||||
test_2 = Foo('test_2')
|
||||
inner_suite = unittest.TestSuite([test_2])
|
||||
|
||||
def gen():
|
||||
yield test_1
|
||||
yield test_2
|
||||
yield inner_suite
|
||||
|
||||
suite_1 = unittest.TestSuite()
|
||||
suite_1.addTests(gen())
|
||||
|
||||
self.assertEqual(list(suite_1), list(gen()))
|
||||
|
||||
# "This is equivalent to iterating over tests, calling addTest() for
|
||||
# each element"
|
||||
suite_2 = unittest.TestSuite()
|
||||
for t in gen():
|
||||
suite_2.addTest(t)
|
||||
|
||||
self.assertEqual(suite_1, suite_2)
|
||||
|
||||
# "Add all the tests from an iterable of TestCase and TestSuite
|
||||
# instances to this test suite."
|
||||
#
|
||||
# What happens if it doesn't get an iterable?
|
||||
def test_addTest__noniterable(self):
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
try:
|
||||
suite.addTests(5)
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
self.fail("Failed to raise TypeError")
|
||||
|
||||
def test_addTest__noncallable(self):
|
||||
suite = unittest.TestSuite()
|
||||
self.assertRaises(TypeError, suite.addTest, 5)
|
||||
|
||||
def test_addTest__casesuiteclass(self):
|
||||
suite = unittest.TestSuite()
|
||||
self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
|
||||
self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
|
||||
|
||||
def test_addTests__string(self):
|
||||
suite = unittest.TestSuite()
|
||||
self.assertRaises(TypeError, suite.addTests, "foo")
|
||||
|
||||
def test_function_in_suite(self):
|
||||
def f(_):
|
||||
pass
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(f)
|
||||
|
||||
# when the bug is fixed this line will not crash
|
||||
suite.run(unittest.TestResult())
|
||||
|
||||
def test_remove_test_at_index(self):
|
||||
if not unittest.BaseTestSuite._cleanup:
|
||||
raise unittest.SkipTest("Suite cleanup is disabled")
|
||||
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
suite._tests = [1, 2, 3]
|
||||
suite._removeTestAtIndex(1)
|
||||
|
||||
self.assertEqual([1, None, 3], suite._tests)
|
||||
|
||||
def test_remove_test_at_index_not_indexable(self):
|
||||
if not unittest.BaseTestSuite._cleanup:
|
||||
raise unittest.SkipTest("Suite cleanup is disabled")
|
||||
|
||||
suite = unittest.TestSuite()
|
||||
suite._tests = None
|
||||
|
||||
# if _removeAtIndex raises for noniterables this next line will break
|
||||
suite._removeTestAtIndex(2)
|
||||
|
||||
def assert_garbage_collect_test_after_run(self, TestSuiteClass):
|
||||
if not unittest.BaseTestSuite._cleanup:
|
||||
raise unittest.SkipTest("Suite cleanup is disabled")
|
||||
|
||||
class Foo(unittest.TestCase):
|
||||
def test_nothing(self):
|
||||
pass
|
||||
|
||||
test = Foo('test_nothing')
|
||||
wref = weakref.ref(test)
|
||||
|
||||
suite = TestSuiteClass([wref()])
|
||||
suite.run(unittest.TestResult())
|
||||
|
||||
del test
|
||||
|
||||
# for the benefit of non-reference counting implementations
|
||||
gc.collect()
|
||||
|
||||
self.assertEqual(suite._tests, [None])
|
||||
self.assertIsNone(wref())
|
||||
|
||||
def test_garbage_collect_test_after_run_BaseTestSuite(self):
|
||||
self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)
|
||||
|
||||
def test_garbage_collect_test_after_run_TestSuite(self):
|
||||
self.assert_garbage_collect_test_after_run(unittest.TestSuite)
|
||||
|
||||
def test_basetestsuite(self):
|
||||
class Test(unittest.TestCase):
|
||||
wasSetUp = False
|
||||
wasTornDown = False
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.wasSetUp = True
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.wasTornDown = True
|
||||
def testPass(self):
|
||||
pass
|
||||
def testFail(self):
|
||||
fail
|
||||
class Module(object):
|
||||
wasSetUp = False
|
||||
wasTornDown = False
|
||||
@staticmethod
|
||||
def setUpModule():
|
||||
Module.wasSetUp = True
|
||||
@staticmethod
|
||||
def tearDownModule():
|
||||
Module.wasTornDown = True
|
||||
|
||||
Test.__module__ = 'Module'
|
||||
sys.modules['Module'] = Module
|
||||
self.addCleanup(sys.modules.pop, 'Module')
|
||||
|
||||
suite = unittest.BaseTestSuite()
|
||||
suite.addTests([Test('testPass'), Test('testFail')])
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
|
||||
result = unittest.TestResult()
|
||||
suite.run(result)
|
||||
self.assertFalse(Module.wasSetUp)
|
||||
self.assertFalse(Module.wasTornDown)
|
||||
self.assertFalse(Test.wasSetUp)
|
||||
self.assertFalse(Test.wasTornDown)
|
||||
self.assertEqual(len(result.errors), 1)
|
||||
self.assertEqual(len(result.failures), 0)
|
||||
self.assertEqual(result.testsRun, 2)
|
||||
self.assertEqual(suite.countTestCases(), 2)
|
||||
|
||||
|
||||
def test_overriding_call(self):
|
||||
class MySuite(unittest.TestSuite):
|
||||
called = False
|
||||
def __call__(self, *args, **kw):
|
||||
self.called = True
|
||||
unittest.TestSuite.__call__(self, *args, **kw)
|
||||
|
||||
suite = MySuite()
|
||||
result = unittest.TestResult()
|
||||
wrapper = unittest.TestSuite()
|
||||
wrapper.addTest(suite)
|
||||
wrapper(result)
|
||||
self.assertTrue(suite.called)
|
||||
|
||||
# reusing results should be permitted even if abominable
|
||||
self.assertFalse(result._testRunEntered)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,17 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
|
||||
here = os.path.dirname(__file__)
|
||||
loader = unittest.defaultTestLoader
|
||||
|
||||
def load_tests(*args):
|
||||
suite = unittest.TestSuite()
|
||||
for fn in os.listdir(here):
|
||||
if fn.startswith("test") and fn.endswith(".py"):
|
||||
modname = "unittest.test.testmock." + fn[:-3]
|
||||
__import__(modname)
|
||||
module = sys.modules[modname]
|
||||
suite.addTest(loader.loadTestsFromModule(module))
|
||||
return suite
|
||||
@@ -1,18 +0,0 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
|
||||
def load_tests(loader, standard_tests, pattern):
|
||||
# top level directory cached on loader instance
|
||||
this_dir = os.path.dirname(__file__)
|
||||
pattern = pattern or "test*.py"
|
||||
# We are inside unittest.test.testmock, so the top-level is three notches up
|
||||
top_level_dir = os.path.dirname(os.path.dirname(os.path.dirname(this_dir)))
|
||||
package_tests = loader.discover(start_dir=this_dir, pattern=pattern,
|
||||
top_level_dir=top_level_dir)
|
||||
standard_tests.addTests(package_tests)
|
||||
return standard_tests
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,16 +0,0 @@
|
||||
target = {'foo': 'FOO'}
|
||||
|
||||
|
||||
def is_instance(obj, klass):
|
||||
"""Version of is_instance that doesn't access __class__"""
|
||||
return issubclass(type(obj), klass)
|
||||
|
||||
|
||||
class SomeClass(object):
|
||||
class_attribute = None
|
||||
|
||||
def wibble(self): pass
|
||||
|
||||
|
||||
class X(object):
|
||||
pass
|
||||
1090
.CondaPkg/env/Lib/unittest/test/testmock/testasync.py
vendored
1090
.CondaPkg/env/Lib/unittest/test/testmock/testasync.py
vendored
File diff suppressed because it is too large
Load Diff
@@ -1,150 +0,0 @@
|
||||
# Copyright (C) 2007-2012 Michael Foord & the mock team
|
||||
# E-mail: fuzzyman AT voidspace DOT org DOT uk
|
||||
# http://www.voidspace.org.uk/python/mock/
|
||||
|
||||
import unittest
|
||||
from unittest.test.testmock.support import is_instance, X, SomeClass
|
||||
|
||||
from unittest.mock import (
|
||||
Mock, MagicMock, NonCallableMagicMock,
|
||||
NonCallableMock, patch, create_autospec,
|
||||
CallableMixin
|
||||
)
|
||||
|
||||
|
||||
|
||||
class TestCallable(unittest.TestCase):
|
||||
|
||||
def assertNotCallable(self, mock):
|
||||
self.assertTrue(is_instance(mock, NonCallableMagicMock))
|
||||
self.assertFalse(is_instance(mock, CallableMixin))
|
||||
|
||||
|
||||
def test_non_callable(self):
|
||||
for mock in NonCallableMagicMock(), NonCallableMock():
|
||||
self.assertRaises(TypeError, mock)
|
||||
self.assertFalse(hasattr(mock, '__call__'))
|
||||
self.assertIn(mock.__class__.__name__, repr(mock))
|
||||
|
||||
|
||||
def test_hierarchy(self):
|
||||
self.assertTrue(issubclass(MagicMock, Mock))
|
||||
self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock))
|
||||
|
||||
|
||||
def test_attributes(self):
|
||||
one = NonCallableMock()
|
||||
self.assertTrue(issubclass(type(one.one), Mock))
|
||||
|
||||
two = NonCallableMagicMock()
|
||||
self.assertTrue(issubclass(type(two.two), MagicMock))
|
||||
|
||||
|
||||
def test_subclasses(self):
|
||||
class MockSub(Mock):
|
||||
pass
|
||||
|
||||
one = MockSub()
|
||||
self.assertTrue(issubclass(type(one.one), MockSub))
|
||||
|
||||
class MagicSub(MagicMock):
|
||||
pass
|
||||
|
||||
two = MagicSub()
|
||||
self.assertTrue(issubclass(type(two.two), MagicSub))
|
||||
|
||||
|
||||
def test_patch_spec(self):
|
||||
patcher = patch('%s.X' % __name__, spec=True)
|
||||
mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
instance = mock()
|
||||
mock.assert_called_once_with()
|
||||
|
||||
self.assertNotCallable(instance)
|
||||
self.assertRaises(TypeError, instance)
|
||||
|
||||
|
||||
def test_patch_spec_set(self):
|
||||
patcher = patch('%s.X' % __name__, spec_set=True)
|
||||
mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
instance = mock()
|
||||
mock.assert_called_once_with()
|
||||
|
||||
self.assertNotCallable(instance)
|
||||
self.assertRaises(TypeError, instance)
|
||||
|
||||
|
||||
def test_patch_spec_instance(self):
|
||||
patcher = patch('%s.X' % __name__, spec=X())
|
||||
mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
self.assertNotCallable(mock)
|
||||
self.assertRaises(TypeError, mock)
|
||||
|
||||
|
||||
def test_patch_spec_set_instance(self):
|
||||
patcher = patch('%s.X' % __name__, spec_set=X())
|
||||
mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
self.assertNotCallable(mock)
|
||||
self.assertRaises(TypeError, mock)
|
||||
|
||||
|
||||
def test_patch_spec_callable_class(self):
|
||||
class CallableX(X):
|
||||
def __call__(self): pass
|
||||
|
||||
class Sub(CallableX):
|
||||
pass
|
||||
|
||||
class Multi(SomeClass, Sub):
|
||||
pass
|
||||
|
||||
for arg in 'spec', 'spec_set':
|
||||
for Klass in CallableX, Sub, Multi:
|
||||
with patch('%s.X' % __name__, **{arg: Klass}) as mock:
|
||||
instance = mock()
|
||||
mock.assert_called_once_with()
|
||||
|
||||
self.assertTrue(is_instance(instance, MagicMock))
|
||||
# inherited spec
|
||||
self.assertRaises(AttributeError, getattr, instance,
|
||||
'foobarbaz')
|
||||
|
||||
result = instance()
|
||||
# instance is callable, result has no spec
|
||||
instance.assert_called_once_with()
|
||||
|
||||
result(3, 2, 1)
|
||||
result.assert_called_once_with(3, 2, 1)
|
||||
result.foo(3, 2, 1)
|
||||
result.foo.assert_called_once_with(3, 2, 1)
|
||||
|
||||
|
||||
def test_create_autospec(self):
|
||||
mock = create_autospec(X)
|
||||
instance = mock()
|
||||
self.assertRaises(TypeError, instance)
|
||||
|
||||
mock = create_autospec(X())
|
||||
self.assertRaises(TypeError, mock)
|
||||
|
||||
|
||||
def test_create_autospec_instance(self):
|
||||
mock = create_autospec(SomeClass, instance=True)
|
||||
|
||||
self.assertRaises(TypeError, mock)
|
||||
mock.wibble()
|
||||
mock.wibble.assert_called_once_with()
|
||||
|
||||
self.assertRaises(TypeError, mock.wibble, 'some', 'args')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
1127
.CondaPkg/env/Lib/unittest/test/testmock/testhelpers.py
vendored
1127
.CondaPkg/env/Lib/unittest/test/testmock/testhelpers.py
vendored
File diff suppressed because it is too large
Load Diff
@@ -1,509 +0,0 @@
|
||||
import math
|
||||
import unittest
|
||||
import os
|
||||
from asyncio import iscoroutinefunction
|
||||
from unittest.mock import AsyncMock, Mock, MagicMock, _magics
|
||||
|
||||
|
||||
|
||||
class TestMockingMagicMethods(unittest.TestCase):
|
||||
|
||||
def test_deleting_magic_methods(self):
|
||||
mock = Mock()
|
||||
self.assertFalse(hasattr(mock, '__getitem__'))
|
||||
|
||||
mock.__getitem__ = Mock()
|
||||
self.assertTrue(hasattr(mock, '__getitem__'))
|
||||
|
||||
del mock.__getitem__
|
||||
self.assertFalse(hasattr(mock, '__getitem__'))
|
||||
|
||||
|
||||
def test_magicmock_del(self):
|
||||
mock = MagicMock()
|
||||
# before using getitem
|
||||
del mock.__getitem__
|
||||
self.assertRaises(TypeError, lambda: mock['foo'])
|
||||
|
||||
mock = MagicMock()
|
||||
# this time use it first
|
||||
mock['foo']
|
||||
del mock.__getitem__
|
||||
self.assertRaises(TypeError, lambda: mock['foo'])
|
||||
|
||||
|
||||
def test_magic_method_wrapping(self):
|
||||
mock = Mock()
|
||||
def f(self, name):
|
||||
return self, 'fish'
|
||||
|
||||
mock.__getitem__ = f
|
||||
self.assertIsNot(mock.__getitem__, f)
|
||||
self.assertEqual(mock['foo'], (mock, 'fish'))
|
||||
self.assertEqual(mock.__getitem__('foo'), (mock, 'fish'))
|
||||
|
||||
mock.__getitem__ = mock
|
||||
self.assertIs(mock.__getitem__, mock)
|
||||
|
||||
|
||||
def test_magic_methods_isolated_between_mocks(self):
|
||||
mock1 = Mock()
|
||||
mock2 = Mock()
|
||||
|
||||
mock1.__iter__ = Mock(return_value=iter([]))
|
||||
self.assertEqual(list(mock1), [])
|
||||
self.assertRaises(TypeError, lambda: list(mock2))
|
||||
|
||||
|
||||
def test_repr(self):
|
||||
mock = Mock()
|
||||
self.assertEqual(repr(mock), "<Mock id='%s'>" % id(mock))
|
||||
mock.__repr__ = lambda s: 'foo'
|
||||
self.assertEqual(repr(mock), 'foo')
|
||||
|
||||
|
||||
def test_str(self):
|
||||
mock = Mock()
|
||||
self.assertEqual(str(mock), object.__str__(mock))
|
||||
mock.__str__ = lambda s: 'foo'
|
||||
self.assertEqual(str(mock), 'foo')
|
||||
|
||||
|
||||
def test_dict_methods(self):
|
||||
mock = Mock()
|
||||
|
||||
self.assertRaises(TypeError, lambda: mock['foo'])
|
||||
def _del():
|
||||
del mock['foo']
|
||||
def _set():
|
||||
mock['foo'] = 3
|
||||
self.assertRaises(TypeError, _del)
|
||||
self.assertRaises(TypeError, _set)
|
||||
|
||||
_dict = {}
|
||||
def getitem(s, name):
|
||||
return _dict[name]
|
||||
def setitem(s, name, value):
|
||||
_dict[name] = value
|
||||
def delitem(s, name):
|
||||
del _dict[name]
|
||||
|
||||
mock.__setitem__ = setitem
|
||||
mock.__getitem__ = getitem
|
||||
mock.__delitem__ = delitem
|
||||
|
||||
self.assertRaises(KeyError, lambda: mock['foo'])
|
||||
mock['foo'] = 'bar'
|
||||
self.assertEqual(_dict, {'foo': 'bar'})
|
||||
self.assertEqual(mock['foo'], 'bar')
|
||||
del mock['foo']
|
||||
self.assertEqual(_dict, {})
|
||||
|
||||
|
||||
def test_numeric(self):
|
||||
original = mock = Mock()
|
||||
mock.value = 0
|
||||
|
||||
self.assertRaises(TypeError, lambda: mock + 3)
|
||||
|
||||
def add(self, other):
|
||||
mock.value += other
|
||||
return self
|
||||
mock.__add__ = add
|
||||
self.assertEqual(mock + 3, mock)
|
||||
self.assertEqual(mock.value, 3)
|
||||
|
||||
del mock.__add__
|
||||
def iadd(mock):
|
||||
mock += 3
|
||||
self.assertRaises(TypeError, iadd, mock)
|
||||
mock.__iadd__ = add
|
||||
mock += 6
|
||||
self.assertEqual(mock, original)
|
||||
self.assertEqual(mock.value, 9)
|
||||
|
||||
self.assertRaises(TypeError, lambda: 3 + mock)
|
||||
mock.__radd__ = add
|
||||
self.assertEqual(7 + mock, mock)
|
||||
self.assertEqual(mock.value, 16)
|
||||
|
||||
def test_division(self):
|
||||
original = mock = Mock()
|
||||
mock.value = 32
|
||||
self.assertRaises(TypeError, lambda: mock / 2)
|
||||
|
||||
def truediv(self, other):
|
||||
mock.value /= other
|
||||
return self
|
||||
mock.__truediv__ = truediv
|
||||
self.assertEqual(mock / 2, mock)
|
||||
self.assertEqual(mock.value, 16)
|
||||
|
||||
del mock.__truediv__
|
||||
def itruediv(mock):
|
||||
mock /= 4
|
||||
self.assertRaises(TypeError, itruediv, mock)
|
||||
mock.__itruediv__ = truediv
|
||||
mock /= 8
|
||||
self.assertEqual(mock, original)
|
||||
self.assertEqual(mock.value, 2)
|
||||
|
||||
self.assertRaises(TypeError, lambda: 8 / mock)
|
||||
mock.__rtruediv__ = truediv
|
||||
self.assertEqual(0.5 / mock, mock)
|
||||
self.assertEqual(mock.value, 4)
|
||||
|
||||
def test_hash(self):
|
||||
mock = Mock()
|
||||
# test delegation
|
||||
self.assertEqual(hash(mock), Mock.__hash__(mock))
|
||||
|
||||
def _hash(s):
|
||||
return 3
|
||||
mock.__hash__ = _hash
|
||||
self.assertEqual(hash(mock), 3)
|
||||
|
||||
|
||||
def test_nonzero(self):
|
||||
m = Mock()
|
||||
self.assertTrue(bool(m))
|
||||
|
||||
m.__bool__ = lambda s: False
|
||||
self.assertFalse(bool(m))
|
||||
|
||||
|
||||
def test_comparison(self):
|
||||
mock = Mock()
|
||||
def comp(s, o):
|
||||
return True
|
||||
mock.__lt__ = mock.__gt__ = mock.__le__ = mock.__ge__ = comp
|
||||
self. assertTrue(mock < 3)
|
||||
self. assertTrue(mock > 3)
|
||||
self. assertTrue(mock <= 3)
|
||||
self. assertTrue(mock >= 3)
|
||||
|
||||
self.assertRaises(TypeError, lambda: MagicMock() < object())
|
||||
self.assertRaises(TypeError, lambda: object() < MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() < MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() > object())
|
||||
self.assertRaises(TypeError, lambda: object() > MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() > MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() <= object())
|
||||
self.assertRaises(TypeError, lambda: object() <= MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() >= object())
|
||||
self.assertRaises(TypeError, lambda: object() >= MagicMock())
|
||||
self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock())
|
||||
|
||||
|
||||
def test_equality(self):
|
||||
for mock in Mock(), MagicMock():
|
||||
self.assertEqual(mock == mock, True)
|
||||
self.assertIsInstance(mock == mock, bool)
|
||||
self.assertEqual(mock != mock, False)
|
||||
self.assertIsInstance(mock != mock, bool)
|
||||
self.assertEqual(mock == object(), False)
|
||||
self.assertEqual(mock != object(), True)
|
||||
|
||||
def eq(self, other):
|
||||
return other == 3
|
||||
mock.__eq__ = eq
|
||||
self.assertTrue(mock == 3)
|
||||
self.assertFalse(mock == 4)
|
||||
|
||||
def ne(self, other):
|
||||
return other == 3
|
||||
mock.__ne__ = ne
|
||||
self.assertTrue(mock != 3)
|
||||
self.assertFalse(mock != 4)
|
||||
|
||||
mock = MagicMock()
|
||||
mock.__eq__.return_value = True
|
||||
self.assertIsInstance(mock == 3, bool)
|
||||
self.assertEqual(mock == 3, True)
|
||||
|
||||
mock.__ne__.return_value = False
|
||||
self.assertIsInstance(mock != 3, bool)
|
||||
self.assertEqual(mock != 3, False)
|
||||
|
||||
|
||||
def test_len_contains_iter(self):
|
||||
mock = Mock()
|
||||
|
||||
self.assertRaises(TypeError, len, mock)
|
||||
self.assertRaises(TypeError, iter, mock)
|
||||
self.assertRaises(TypeError, lambda: 'foo' in mock)
|
||||
|
||||
mock.__len__ = lambda s: 6
|
||||
self.assertEqual(len(mock), 6)
|
||||
|
||||
mock.__contains__ = lambda s, o: o == 3
|
||||
self.assertIn(3, mock)
|
||||
self.assertNotIn(6, mock)
|
||||
|
||||
mock.__iter__ = lambda s: iter('foobarbaz')
|
||||
self.assertEqual(list(mock), list('foobarbaz'))
|
||||
|
||||
|
||||
def test_magicmock(self):
|
||||
mock = MagicMock()
|
||||
|
||||
mock.__iter__.return_value = iter([1, 2, 3])
|
||||
self.assertEqual(list(mock), [1, 2, 3])
|
||||
|
||||
getattr(mock, '__bool__').return_value = False
|
||||
self.assertFalse(hasattr(mock, '__nonzero__'))
|
||||
self.assertFalse(bool(mock))
|
||||
|
||||
for entry in _magics:
|
||||
self.assertTrue(hasattr(mock, entry))
|
||||
self.assertFalse(hasattr(mock, '__imaginary__'))
|
||||
|
||||
|
||||
def test_magic_mock_equality(self):
|
||||
mock = MagicMock()
|
||||
self.assertIsInstance(mock == object(), bool)
|
||||
self.assertIsInstance(mock != object(), bool)
|
||||
|
||||
self.assertEqual(mock == object(), False)
|
||||
self.assertEqual(mock != object(), True)
|
||||
self.assertEqual(mock == mock, True)
|
||||
self.assertEqual(mock != mock, False)
|
||||
|
||||
def test_asyncmock_defaults(self):
|
||||
mock = AsyncMock()
|
||||
self.assertEqual(int(mock), 1)
|
||||
self.assertEqual(complex(mock), 1j)
|
||||
self.assertEqual(float(mock), 1.0)
|
||||
self.assertNotIn(object(), mock)
|
||||
self.assertEqual(len(mock), 0)
|
||||
self.assertEqual(list(mock), [])
|
||||
self.assertEqual(hash(mock), object.__hash__(mock))
|
||||
self.assertEqual(str(mock), object.__str__(mock))
|
||||
self.assertTrue(bool(mock))
|
||||
self.assertEqual(round(mock), mock.__round__())
|
||||
self.assertEqual(math.trunc(mock), mock.__trunc__())
|
||||
self.assertEqual(math.floor(mock), mock.__floor__())
|
||||
self.assertEqual(math.ceil(mock), mock.__ceil__())
|
||||
self.assertTrue(iscoroutinefunction(mock.__aexit__))
|
||||
self.assertTrue(iscoroutinefunction(mock.__aenter__))
|
||||
self.assertIsInstance(mock.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(mock.__aexit__, AsyncMock)
|
||||
|
||||
# in Python 3 oct and hex use __index__
|
||||
# so these tests are for __index__ in py3k
|
||||
self.assertEqual(oct(mock), '0o1')
|
||||
self.assertEqual(hex(mock), '0x1')
|
||||
# how to test __sizeof__ ?
|
||||
|
||||
def test_magicmock_defaults(self):
|
||||
mock = MagicMock()
|
||||
self.assertEqual(int(mock), 1)
|
||||
self.assertEqual(complex(mock), 1j)
|
||||
self.assertEqual(float(mock), 1.0)
|
||||
self.assertNotIn(object(), mock)
|
||||
self.assertEqual(len(mock), 0)
|
||||
self.assertEqual(list(mock), [])
|
||||
self.assertEqual(hash(mock), object.__hash__(mock))
|
||||
self.assertEqual(str(mock), object.__str__(mock))
|
||||
self.assertTrue(bool(mock))
|
||||
self.assertEqual(round(mock), mock.__round__())
|
||||
self.assertEqual(math.trunc(mock), mock.__trunc__())
|
||||
self.assertEqual(math.floor(mock), mock.__floor__())
|
||||
self.assertEqual(math.ceil(mock), mock.__ceil__())
|
||||
self.assertTrue(iscoroutinefunction(mock.__aexit__))
|
||||
self.assertTrue(iscoroutinefunction(mock.__aenter__))
|
||||
self.assertIsInstance(mock.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(mock.__aexit__, AsyncMock)
|
||||
|
||||
# in Python 3 oct and hex use __index__
|
||||
# so these tests are for __index__ in py3k
|
||||
self.assertEqual(oct(mock), '0o1')
|
||||
self.assertEqual(hex(mock), '0x1')
|
||||
# how to test __sizeof__ ?
|
||||
|
||||
|
||||
def test_magic_methods_fspath(self):
|
||||
mock = MagicMock()
|
||||
expected_path = mock.__fspath__()
|
||||
mock.reset_mock()
|
||||
|
||||
self.assertEqual(os.fspath(mock), expected_path)
|
||||
mock.__fspath__.assert_called_once()
|
||||
|
||||
|
||||
def test_magic_methods_and_spec(self):
|
||||
class Iterable(object):
|
||||
def __iter__(self): pass
|
||||
|
||||
mock = Mock(spec=Iterable)
|
||||
self.assertRaises(AttributeError, lambda: mock.__iter__)
|
||||
|
||||
mock.__iter__ = Mock(return_value=iter([]))
|
||||
self.assertEqual(list(mock), [])
|
||||
|
||||
class NonIterable(object):
|
||||
pass
|
||||
mock = Mock(spec=NonIterable)
|
||||
self.assertRaises(AttributeError, lambda: mock.__iter__)
|
||||
|
||||
def set_int():
|
||||
mock.__int__ = Mock(return_value=iter([]))
|
||||
self.assertRaises(AttributeError, set_int)
|
||||
|
||||
mock = MagicMock(spec=Iterable)
|
||||
self.assertEqual(list(mock), [])
|
||||
self.assertRaises(AttributeError, set_int)
|
||||
|
||||
|
||||
def test_magic_methods_and_spec_set(self):
|
||||
class Iterable(object):
|
||||
def __iter__(self): pass
|
||||
|
||||
mock = Mock(spec_set=Iterable)
|
||||
self.assertRaises(AttributeError, lambda: mock.__iter__)
|
||||
|
||||
mock.__iter__ = Mock(return_value=iter([]))
|
||||
self.assertEqual(list(mock), [])
|
||||
|
||||
class NonIterable(object):
|
||||
pass
|
||||
mock = Mock(spec_set=NonIterable)
|
||||
self.assertRaises(AttributeError, lambda: mock.__iter__)
|
||||
|
||||
def set_int():
|
||||
mock.__int__ = Mock(return_value=iter([]))
|
||||
self.assertRaises(AttributeError, set_int)
|
||||
|
||||
mock = MagicMock(spec_set=Iterable)
|
||||
self.assertEqual(list(mock), [])
|
||||
self.assertRaises(AttributeError, set_int)
|
||||
|
||||
|
||||
def test_setting_unsupported_magic_method(self):
|
||||
mock = MagicMock()
|
||||
def set_setattr():
|
||||
mock.__setattr__ = lambda self, name: None
|
||||
self.assertRaisesRegex(AttributeError,
|
||||
"Attempting to set unsupported magic method '__setattr__'.",
|
||||
set_setattr
|
||||
)
|
||||
|
||||
|
||||
def test_attributes_and_return_value(self):
|
||||
mock = MagicMock()
|
||||
attr = mock.foo
|
||||
def _get_type(obj):
|
||||
# the type of every mock (or magicmock) is a custom subclass
|
||||
# so the real type is the second in the mro
|
||||
return type(obj).__mro__[1]
|
||||
self.assertEqual(_get_type(attr), MagicMock)
|
||||
|
||||
returned = mock()
|
||||
self.assertEqual(_get_type(returned), MagicMock)
|
||||
|
||||
|
||||
def test_magic_methods_are_magic_mocks(self):
|
||||
mock = MagicMock()
|
||||
self.assertIsInstance(mock.__getitem__, MagicMock)
|
||||
|
||||
mock[1][2].__getitem__.return_value = 3
|
||||
self.assertEqual(mock[1][2][3], 3)
|
||||
|
||||
|
||||
def test_magic_method_reset_mock(self):
|
||||
mock = MagicMock()
|
||||
str(mock)
|
||||
self.assertTrue(mock.__str__.called)
|
||||
mock.reset_mock()
|
||||
self.assertFalse(mock.__str__.called)
|
||||
|
||||
|
||||
def test_dir(self):
|
||||
# overriding the default implementation
|
||||
for mock in Mock(), MagicMock():
|
||||
def _dir(self):
|
||||
return ['foo']
|
||||
mock.__dir__ = _dir
|
||||
self.assertEqual(dir(mock), ['foo'])
|
||||
|
||||
|
||||
def test_bound_methods(self):
|
||||
m = Mock()
|
||||
|
||||
# XXXX should this be an expected failure instead?
|
||||
|
||||
# this seems like it should work, but is hard to do without introducing
|
||||
# other api inconsistencies. Failure message could be better though.
|
||||
m.__iter__ = [3].__iter__
|
||||
self.assertRaises(TypeError, iter, m)
|
||||
|
||||
|
||||
def test_magic_method_type(self):
|
||||
class Foo(MagicMock):
|
||||
pass
|
||||
|
||||
foo = Foo()
|
||||
self.assertIsInstance(foo.__int__, Foo)
|
||||
|
||||
|
||||
def test_descriptor_from_class(self):
|
||||
m = MagicMock()
|
||||
type(m).__str__.return_value = 'foo'
|
||||
self.assertEqual(str(m), 'foo')
|
||||
|
||||
|
||||
def test_iterable_as_iter_return_value(self):
|
||||
m = MagicMock()
|
||||
m.__iter__.return_value = [1, 2, 3]
|
||||
self.assertEqual(list(m), [1, 2, 3])
|
||||
self.assertEqual(list(m), [1, 2, 3])
|
||||
|
||||
m.__iter__.return_value = iter([4, 5, 6])
|
||||
self.assertEqual(list(m), [4, 5, 6])
|
||||
self.assertEqual(list(m), [])
|
||||
|
||||
|
||||
def test_matmul(self):
|
||||
m = MagicMock()
|
||||
self.assertIsInstance(m @ 1, MagicMock)
|
||||
m.__matmul__.return_value = 42
|
||||
m.__rmatmul__.return_value = 666
|
||||
m.__imatmul__.return_value = 24
|
||||
self.assertEqual(m @ 1, 42)
|
||||
self.assertEqual(1 @ m, 666)
|
||||
m @= 24
|
||||
self.assertEqual(m, 24)
|
||||
|
||||
def test_divmod_and_rdivmod(self):
|
||||
m = MagicMock()
|
||||
self.assertIsInstance(divmod(5, m), MagicMock)
|
||||
m.__divmod__.return_value = (2, 1)
|
||||
self.assertEqual(divmod(m, 2), (2, 1))
|
||||
m = MagicMock()
|
||||
foo = divmod(2, m)
|
||||
self.assertIsInstance(foo, MagicMock)
|
||||
foo_direct = m.__divmod__(2)
|
||||
self.assertIsInstance(foo_direct, MagicMock)
|
||||
bar = divmod(m, 2)
|
||||
self.assertIsInstance(bar, MagicMock)
|
||||
bar_direct = m.__rdivmod__(2)
|
||||
self.assertIsInstance(bar_direct, MagicMock)
|
||||
|
||||
# http://bugs.python.org/issue23310
|
||||
# Check if you can change behaviour of magic methods in MagicMock init
|
||||
def test_magic_in_initialization(self):
|
||||
m = MagicMock(**{'__str__.return_value': "12"})
|
||||
self.assertEqual(str(m), "12")
|
||||
|
||||
def test_changing_magic_set_in_initialization(self):
|
||||
m = MagicMock(**{'__str__.return_value': "12"})
|
||||
m.__str__.return_value = "13"
|
||||
self.assertEqual(str(m), "13")
|
||||
m = MagicMock(**{'__str__.return_value': "12"})
|
||||
m.configure_mock(**{'__str__.return_value': "14"})
|
||||
self.assertEqual(str(m), "14")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
2278
.CondaPkg/env/Lib/unittest/test/testmock/testmock.py
vendored
2278
.CondaPkg/env/Lib/unittest/test/testmock/testmock.py
vendored
File diff suppressed because it is too large
Load Diff
1953
.CondaPkg/env/Lib/unittest/test/testmock/testpatch.py
vendored
1953
.CondaPkg/env/Lib/unittest/test/testmock/testpatch.py
vendored
File diff suppressed because it is too large
Load Diff
@@ -1,237 +0,0 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
|
||||
class SampleObject:
|
||||
|
||||
def method_sample1(self): pass
|
||||
|
||||
def method_sample2(self): pass
|
||||
|
||||
|
||||
class TestSealable(unittest.TestCase):
|
||||
|
||||
def test_attributes_return_more_mocks_by_default(self):
|
||||
m = mock.Mock()
|
||||
|
||||
self.assertIsInstance(m.test, mock.Mock)
|
||||
self.assertIsInstance(m.test(), mock.Mock)
|
||||
self.assertIsInstance(m.test().test2(), mock.Mock)
|
||||
|
||||
def test_new_attributes_cannot_be_accessed_on_seal(self):
|
||||
m = mock.Mock()
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test
|
||||
with self.assertRaises(AttributeError):
|
||||
m()
|
||||
|
||||
def test_new_attributes_cannot_be_set_on_seal(self):
|
||||
m = mock.Mock()
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test = 1
|
||||
|
||||
def test_existing_attributes_can_be_set_on_seal(self):
|
||||
m = mock.Mock()
|
||||
m.test.test2 = 1
|
||||
|
||||
mock.seal(m)
|
||||
m.test.test2 = 2
|
||||
self.assertEqual(m.test.test2, 2)
|
||||
|
||||
def test_new_attributes_cannot_be_set_on_child_of_seal(self):
|
||||
m = mock.Mock()
|
||||
m.test.test2 = 1
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test.test3 = 1
|
||||
|
||||
def test_existing_attributes_allowed_after_seal(self):
|
||||
m = mock.Mock()
|
||||
|
||||
m.test.return_value = 3
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.test(), 3)
|
||||
|
||||
def test_initialized_attributes_allowed_after_seal(self):
|
||||
m = mock.Mock(test_value=1)
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.test_value, 1)
|
||||
|
||||
def test_call_on_sealed_mock_fails(self):
|
||||
m = mock.Mock()
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError):
|
||||
m()
|
||||
|
||||
def test_call_on_defined_sealed_mock_succeeds(self):
|
||||
m = mock.Mock(return_value=5)
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m(), 5)
|
||||
|
||||
def test_seals_recurse_on_added_attributes(self):
|
||||
m = mock.Mock()
|
||||
|
||||
m.test1.test2().test3 = 4
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.test1.test2().test3, 4)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test1.test2().test4
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test1.test3
|
||||
|
||||
def test_seals_recurse_on_magic_methods(self):
|
||||
m = mock.MagicMock()
|
||||
|
||||
m.test1.test2["a"].test3 = 4
|
||||
m.test1.test3[2:5].test3 = 4
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.test1.test2["a"].test3, 4)
|
||||
self.assertEqual(m.test1.test2[2:5].test3, 4)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test1.test2["a"].test4
|
||||
with self.assertRaises(AttributeError):
|
||||
m.test1.test3[2:5].test4
|
||||
|
||||
def test_seals_dont_recurse_on_manual_attributes(self):
|
||||
m = mock.Mock(name="root_mock")
|
||||
|
||||
m.test1.test2 = mock.Mock(name="not_sealed")
|
||||
m.test1.test2.test3 = 4
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.test1.test2.test3, 4)
|
||||
m.test1.test2.test4 # Does not raise
|
||||
m.test1.test2.test4 = 1 # Does not raise
|
||||
|
||||
def test_integration_with_spec_att_definition(self):
|
||||
"""You are not restricted when using mock with spec"""
|
||||
m = mock.Mock(SampleObject)
|
||||
|
||||
m.attr_sample1 = 1
|
||||
m.attr_sample3 = 3
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.attr_sample1, 1)
|
||||
self.assertEqual(m.attr_sample3, 3)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.attr_sample2
|
||||
|
||||
def test_integration_with_spec_method_definition(self):
|
||||
"""You need to define the methods, even if they are in the spec"""
|
||||
m = mock.Mock(SampleObject)
|
||||
|
||||
m.method_sample1.return_value = 1
|
||||
|
||||
mock.seal(m)
|
||||
self.assertEqual(m.method_sample1(), 1)
|
||||
with self.assertRaises(AttributeError):
|
||||
m.method_sample2()
|
||||
|
||||
def test_integration_with_spec_method_definition_respects_spec(self):
|
||||
"""You cannot define methods out of the spec"""
|
||||
m = mock.Mock(SampleObject)
|
||||
|
||||
with self.assertRaises(AttributeError):
|
||||
m.method_sample3.return_value = 3
|
||||
|
||||
def test_sealed_exception_has_attribute_name(self):
|
||||
m = mock.Mock()
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
m.SECRETE_name
|
||||
self.assertIn("SECRETE_name", str(cm.exception))
|
||||
|
||||
def test_attribute_chain_is_maintained(self):
|
||||
m = mock.Mock(name="mock_name")
|
||||
m.test1.test2.test3.test4
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
m.test1.test2.test3.test4.boom
|
||||
self.assertIn("mock_name.test1.test2.test3.test4.boom", str(cm.exception))
|
||||
|
||||
def test_call_chain_is_maintained(self):
|
||||
m = mock.Mock()
|
||||
m.test1().test2.test3().test4
|
||||
|
||||
mock.seal(m)
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
m.test1().test2.test3().test4()
|
||||
self.assertIn("mock.test1().test2.test3().test4", str(cm.exception))
|
||||
|
||||
def test_seal_with_autospec(self):
|
||||
# https://bugs.python.org/issue45156
|
||||
class Foo:
|
||||
foo = 0
|
||||
def bar1(self):
|
||||
return 1
|
||||
def bar2(self):
|
||||
return 2
|
||||
|
||||
class Baz:
|
||||
baz = 3
|
||||
def ban(self):
|
||||
return 4
|
||||
|
||||
for spec_set in (True, False):
|
||||
with self.subTest(spec_set=spec_set):
|
||||
foo = mock.create_autospec(Foo, spec_set=spec_set)
|
||||
foo.bar1.return_value = 'a'
|
||||
foo.Baz.ban.return_value = 'b'
|
||||
|
||||
mock.seal(foo)
|
||||
|
||||
self.assertIsInstance(foo.foo, mock.NonCallableMagicMock)
|
||||
self.assertIsInstance(foo.bar1, mock.MagicMock)
|
||||
self.assertIsInstance(foo.bar2, mock.MagicMock)
|
||||
self.assertIsInstance(foo.Baz, mock.MagicMock)
|
||||
self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock)
|
||||
self.assertIsInstance(foo.Baz.ban, mock.MagicMock)
|
||||
|
||||
self.assertEqual(foo.bar1(), 'a')
|
||||
foo.bar1.return_value = 'new_a'
|
||||
self.assertEqual(foo.bar1(), 'new_a')
|
||||
self.assertEqual(foo.Baz.ban(), 'b')
|
||||
foo.Baz.ban.return_value = 'new_b'
|
||||
self.assertEqual(foo.Baz.ban(), 'new_b')
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
foo.foo()
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.bar = 1
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.bar2()
|
||||
|
||||
foo.bar2.return_value = 'bar2'
|
||||
self.assertEqual(foo.bar2(), 'bar2')
|
||||
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.missing_attr
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.missing_attr = 1
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.missing_method()
|
||||
with self.assertRaises(TypeError):
|
||||
foo.Baz.baz()
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.Baz.missing_attr
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.Baz.missing_attr = 1
|
||||
with self.assertRaises(AttributeError):
|
||||
foo.Baz.missing_method()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,41 +0,0 @@
|
||||
import unittest
|
||||
import copy
|
||||
import pickle
|
||||
from unittest.mock import sentinel, DEFAULT
|
||||
|
||||
|
||||
class SentinelTest(unittest.TestCase):
|
||||
|
||||
def testSentinels(self):
|
||||
self.assertEqual(sentinel.whatever, sentinel.whatever,
|
||||
'sentinel not stored')
|
||||
self.assertNotEqual(sentinel.whatever, sentinel.whateverelse,
|
||||
'sentinel should be unique')
|
||||
|
||||
|
||||
def testSentinelName(self):
|
||||
self.assertEqual(str(sentinel.whatever), 'sentinel.whatever',
|
||||
'sentinel name incorrect')
|
||||
|
||||
|
||||
def testDEFAULT(self):
|
||||
self.assertIs(DEFAULT, sentinel.DEFAULT)
|
||||
|
||||
def testBases(self):
|
||||
# If this doesn't raise an AttributeError then help(mock) is broken
|
||||
self.assertRaises(AttributeError, lambda: sentinel.__bases__)
|
||||
|
||||
def testPickle(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL+1):
|
||||
with self.subTest(protocol=proto):
|
||||
pickled = pickle.dumps(sentinel.whatever, proto)
|
||||
unpickled = pickle.loads(pickled)
|
||||
self.assertIs(unpickled, sentinel.whatever)
|
||||
|
||||
def testCopy(self):
|
||||
self.assertIs(copy.copy(sentinel.whatever), sentinel.whatever)
|
||||
self.assertIs(copy.deepcopy(sentinel.whatever), sentinel.whatever)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
347
.CondaPkg/env/Lib/unittest/test/testmock/testwith.py
vendored
347
.CondaPkg/env/Lib/unittest/test/testmock/testwith.py
vendored
@@ -1,347 +0,0 @@
|
||||
import unittest
|
||||
from warnings import catch_warnings
|
||||
|
||||
from unittest.test.testmock.support import is_instance
|
||||
from unittest.mock import MagicMock, Mock, patch, sentinel, mock_open, call
|
||||
|
||||
|
||||
|
||||
something = sentinel.Something
|
||||
something_else = sentinel.SomethingElse
|
||||
|
||||
|
||||
class SampleException(Exception): pass
|
||||
|
||||
|
||||
class WithTest(unittest.TestCase):
|
||||
|
||||
def test_with_statement(self):
|
||||
with patch('%s.something' % __name__, sentinel.Something2):
|
||||
self.assertEqual(something, sentinel.Something2, "unpatched")
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
|
||||
|
||||
def test_with_statement_exception(self):
|
||||
with self.assertRaises(SampleException):
|
||||
with patch('%s.something' % __name__, sentinel.Something2):
|
||||
self.assertEqual(something, sentinel.Something2, "unpatched")
|
||||
raise SampleException()
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
|
||||
|
||||
def test_with_statement_as(self):
|
||||
with patch('%s.something' % __name__) as mock_something:
|
||||
self.assertEqual(something, mock_something, "unpatched")
|
||||
self.assertTrue(is_instance(mock_something, MagicMock),
|
||||
"patching wrong type")
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
|
||||
|
||||
def test_patch_object_with_statement(self):
|
||||
class Foo(object):
|
||||
something = 'foo'
|
||||
original = Foo.something
|
||||
with patch.object(Foo, 'something'):
|
||||
self.assertNotEqual(Foo.something, original, "unpatched")
|
||||
self.assertEqual(Foo.something, original)
|
||||
|
||||
|
||||
def test_with_statement_nested(self):
|
||||
with catch_warnings(record=True):
|
||||
with patch('%s.something' % __name__) as mock_something, patch('%s.something_else' % __name__) as mock_something_else:
|
||||
self.assertEqual(something, mock_something, "unpatched")
|
||||
self.assertEqual(something_else, mock_something_else,
|
||||
"unpatched")
|
||||
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
self.assertEqual(something_else, sentinel.SomethingElse)
|
||||
|
||||
|
||||
def test_with_statement_specified(self):
|
||||
with patch('%s.something' % __name__, sentinel.Patched) as mock_something:
|
||||
self.assertEqual(something, mock_something, "unpatched")
|
||||
self.assertEqual(mock_something, sentinel.Patched, "wrong patch")
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
|
||||
|
||||
def testContextManagerMocking(self):
|
||||
mock = Mock()
|
||||
mock.__enter__ = Mock()
|
||||
mock.__exit__ = Mock()
|
||||
mock.__exit__.return_value = False
|
||||
|
||||
with mock as m:
|
||||
self.assertEqual(m, mock.__enter__.return_value)
|
||||
mock.__enter__.assert_called_with()
|
||||
mock.__exit__.assert_called_with(None, None, None)
|
||||
|
||||
|
||||
def test_context_manager_with_magic_mock(self):
|
||||
mock = MagicMock()
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
with mock:
|
||||
'foo' + 3
|
||||
mock.__enter__.assert_called_with()
|
||||
self.assertTrue(mock.__exit__.called)
|
||||
|
||||
|
||||
def test_with_statement_same_attribute(self):
|
||||
with patch('%s.something' % __name__, sentinel.Patched) as mock_something:
|
||||
self.assertEqual(something, mock_something, "unpatched")
|
||||
|
||||
with patch('%s.something' % __name__) as mock_again:
|
||||
self.assertEqual(something, mock_again, "unpatched")
|
||||
|
||||
self.assertEqual(something, mock_something,
|
||||
"restored with wrong instance")
|
||||
|
||||
self.assertEqual(something, sentinel.Something, "not restored")
|
||||
|
||||
|
||||
def test_with_statement_imbricated(self):
|
||||
with patch('%s.something' % __name__) as mock_something:
|
||||
self.assertEqual(something, mock_something, "unpatched")
|
||||
|
||||
with patch('%s.something_else' % __name__) as mock_something_else:
|
||||
self.assertEqual(something_else, mock_something_else,
|
||||
"unpatched")
|
||||
|
||||
self.assertEqual(something, sentinel.Something)
|
||||
self.assertEqual(something_else, sentinel.SomethingElse)
|
||||
|
||||
|
||||
def test_dict_context_manager(self):
|
||||
foo = {}
|
||||
with patch.dict(foo, {'a': 'b'}):
|
||||
self.assertEqual(foo, {'a': 'b'})
|
||||
self.assertEqual(foo, {})
|
||||
|
||||
with self.assertRaises(NameError):
|
||||
with patch.dict(foo, {'a': 'b'}):
|
||||
self.assertEqual(foo, {'a': 'b'})
|
||||
raise NameError('Konrad')
|
||||
|
||||
self.assertEqual(foo, {})
|
||||
|
||||
def test_double_patch_instance_method(self):
|
||||
class C:
|
||||
def f(self): pass
|
||||
|
||||
c = C()
|
||||
|
||||
with patch.object(c, 'f') as patch1:
|
||||
with patch.object(c, 'f') as patch2:
|
||||
c.f()
|
||||
self.assertEqual(patch2.call_count, 1)
|
||||
self.assertEqual(patch1.call_count, 0)
|
||||
c.f()
|
||||
self.assertEqual(patch1.call_count, 1)
|
||||
|
||||
|
||||
class TestMockOpen(unittest.TestCase):
|
||||
|
||||
def test_mock_open(self):
|
||||
mock = mock_open()
|
||||
with patch('%s.open' % __name__, mock, create=True) as patched:
|
||||
self.assertIs(patched, mock)
|
||||
open('foo')
|
||||
|
||||
mock.assert_called_once_with('foo')
|
||||
|
||||
|
||||
def test_mock_open_context_manager(self):
|
||||
mock = mock_open()
|
||||
handle = mock.return_value
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
with open('foo') as f:
|
||||
f.read()
|
||||
|
||||
expected_calls = [call('foo'), call().__enter__(), call().read(),
|
||||
call().__exit__(None, None, None)]
|
||||
self.assertEqual(mock.mock_calls, expected_calls)
|
||||
self.assertIs(f, handle)
|
||||
|
||||
def test_mock_open_context_manager_multiple_times(self):
|
||||
mock = mock_open()
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
with open('foo') as f:
|
||||
f.read()
|
||||
with open('bar') as f:
|
||||
f.read()
|
||||
|
||||
expected_calls = [
|
||||
call('foo'), call().__enter__(), call().read(),
|
||||
call().__exit__(None, None, None),
|
||||
call('bar'), call().__enter__(), call().read(),
|
||||
call().__exit__(None, None, None)]
|
||||
self.assertEqual(mock.mock_calls, expected_calls)
|
||||
|
||||
def test_explicit_mock(self):
|
||||
mock = MagicMock()
|
||||
mock_open(mock)
|
||||
|
||||
with patch('%s.open' % __name__, mock, create=True) as patched:
|
||||
self.assertIs(patched, mock)
|
||||
open('foo')
|
||||
|
||||
mock.assert_called_once_with('foo')
|
||||
|
||||
|
||||
def test_read_data(self):
|
||||
mock = mock_open(read_data='foo')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
result = h.read()
|
||||
|
||||
self.assertEqual(result, 'foo')
|
||||
|
||||
|
||||
def test_readline_data(self):
|
||||
# Check that readline will return all the lines from the fake file
|
||||
# And that once fully consumed, readline will return an empty string.
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
line1 = h.readline()
|
||||
line2 = h.readline()
|
||||
line3 = h.readline()
|
||||
self.assertEqual(line1, 'foo\n')
|
||||
self.assertEqual(line2, 'bar\n')
|
||||
self.assertEqual(line3, 'baz\n')
|
||||
self.assertEqual(h.readline(), '')
|
||||
|
||||
# Check that we properly emulate a file that doesn't end in a newline
|
||||
mock = mock_open(read_data='foo')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
result = h.readline()
|
||||
self.assertEqual(result, 'foo')
|
||||
self.assertEqual(h.readline(), '')
|
||||
|
||||
|
||||
def test_dunder_iter_data(self):
|
||||
# Check that dunder_iter will return all the lines from the fake file.
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
lines = [l for l in h]
|
||||
self.assertEqual(lines[0], 'foo\n')
|
||||
self.assertEqual(lines[1], 'bar\n')
|
||||
self.assertEqual(lines[2], 'baz\n')
|
||||
self.assertEqual(h.readline(), '')
|
||||
with self.assertRaises(StopIteration):
|
||||
next(h)
|
||||
|
||||
def test_next_data(self):
|
||||
# Check that next will correctly return the next available
|
||||
# line and plays well with the dunder_iter part.
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
line1 = next(h)
|
||||
line2 = next(h)
|
||||
lines = [l for l in h]
|
||||
self.assertEqual(line1, 'foo\n')
|
||||
self.assertEqual(line2, 'bar\n')
|
||||
self.assertEqual(lines[0], 'baz\n')
|
||||
self.assertEqual(h.readline(), '')
|
||||
|
||||
def test_readlines_data(self):
|
||||
# Test that emulating a file that ends in a newline character works
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
result = h.readlines()
|
||||
self.assertEqual(result, ['foo\n', 'bar\n', 'baz\n'])
|
||||
|
||||
# Test that files without a final newline will also be correctly
|
||||
# emulated
|
||||
mock = mock_open(read_data='foo\nbar\nbaz')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
result = h.readlines()
|
||||
|
||||
self.assertEqual(result, ['foo\n', 'bar\n', 'baz'])
|
||||
|
||||
|
||||
def test_read_bytes(self):
|
||||
mock = mock_open(read_data=b'\xc6')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
with open('abc', 'rb') as f:
|
||||
result = f.read()
|
||||
self.assertEqual(result, b'\xc6')
|
||||
|
||||
|
||||
def test_readline_bytes(self):
|
||||
m = mock_open(read_data=b'abc\ndef\nghi\n')
|
||||
with patch('%s.open' % __name__, m, create=True):
|
||||
with open('abc', 'rb') as f:
|
||||
line1 = f.readline()
|
||||
line2 = f.readline()
|
||||
line3 = f.readline()
|
||||
self.assertEqual(line1, b'abc\n')
|
||||
self.assertEqual(line2, b'def\n')
|
||||
self.assertEqual(line3, b'ghi\n')
|
||||
|
||||
|
||||
def test_readlines_bytes(self):
|
||||
m = mock_open(read_data=b'abc\ndef\nghi\n')
|
||||
with patch('%s.open' % __name__, m, create=True):
|
||||
with open('abc', 'rb') as f:
|
||||
result = f.readlines()
|
||||
self.assertEqual(result, [b'abc\n', b'def\n', b'ghi\n'])
|
||||
|
||||
|
||||
def test_mock_open_read_with_argument(self):
|
||||
# At one point calling read with an argument was broken
|
||||
# for mocks returned by mock_open
|
||||
some_data = 'foo\nbar\nbaz'
|
||||
mock = mock_open(read_data=some_data)
|
||||
self.assertEqual(mock().read(10), some_data[:10])
|
||||
self.assertEqual(mock().read(10), some_data[:10])
|
||||
|
||||
f = mock()
|
||||
self.assertEqual(f.read(10), some_data[:10])
|
||||
self.assertEqual(f.read(10), some_data[10:])
|
||||
|
||||
|
||||
def test_interleaved_reads(self):
|
||||
# Test that calling read, readline, and readlines pulls data
|
||||
# sequentially from the data we preload with
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
line1 = h.readline()
|
||||
rest = h.readlines()
|
||||
self.assertEqual(line1, 'foo\n')
|
||||
self.assertEqual(rest, ['bar\n', 'baz\n'])
|
||||
|
||||
mock = mock_open(read_data='foo\nbar\nbaz\n')
|
||||
with patch('%s.open' % __name__, mock, create=True):
|
||||
h = open('bar')
|
||||
line1 = h.readline()
|
||||
rest = h.read()
|
||||
self.assertEqual(line1, 'foo\n')
|
||||
self.assertEqual(rest, 'bar\nbaz\n')
|
||||
|
||||
|
||||
def test_overriding_return_values(self):
|
||||
mock = mock_open(read_data='foo')
|
||||
handle = mock()
|
||||
|
||||
handle.read.return_value = 'bar'
|
||||
handle.readline.return_value = 'bar'
|
||||
handle.readlines.return_value = ['bar']
|
||||
|
||||
self.assertEqual(handle.read(), 'bar')
|
||||
self.assertEqual(handle.readline(), 'bar')
|
||||
self.assertEqual(handle.readlines(), ['bar'])
|
||||
|
||||
# call repeatedly to check that a StopIteration is not propagated
|
||||
self.assertEqual(handle.readline(), 'bar')
|
||||
self.assertEqual(handle.readline(), 'bar')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
170
.CondaPkg/env/Lib/unittest/util.py
vendored
170
.CondaPkg/env/Lib/unittest/util.py
vendored
@@ -1,170 +0,0 @@
|
||||
"""Various utility functions."""
|
||||
|
||||
from collections import namedtuple, Counter
|
||||
from os.path import commonprefix
|
||||
|
||||
__unittest = True
|
||||
|
||||
_MAX_LENGTH = 80
|
||||
_PLACEHOLDER_LEN = 12
|
||||
_MIN_BEGIN_LEN = 5
|
||||
_MIN_END_LEN = 5
|
||||
_MIN_COMMON_LEN = 5
|
||||
_MIN_DIFF_LEN = _MAX_LENGTH - \
|
||||
(_MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN +
|
||||
_PLACEHOLDER_LEN + _MIN_END_LEN)
|
||||
assert _MIN_DIFF_LEN >= 0
|
||||
|
||||
def _shorten(s, prefixlen, suffixlen):
|
||||
skip = len(s) - prefixlen - suffixlen
|
||||
if skip > _PLACEHOLDER_LEN:
|
||||
s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:])
|
||||
return s
|
||||
|
||||
def _common_shorten_repr(*args):
|
||||
args = tuple(map(safe_repr, args))
|
||||
maxlen = max(map(len, args))
|
||||
if maxlen <= _MAX_LENGTH:
|
||||
return args
|
||||
|
||||
prefix = commonprefix(args)
|
||||
prefixlen = len(prefix)
|
||||
|
||||
common_len = _MAX_LENGTH - \
|
||||
(maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
|
||||
if common_len > _MIN_COMMON_LEN:
|
||||
assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
|
||||
(maxlen - prefixlen) < _MAX_LENGTH
|
||||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
|
||||
return tuple(prefix + s[prefixlen:] for s in args)
|
||||
|
||||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
|
||||
return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
|
||||
for s in args)
|
||||
|
||||
def safe_repr(obj, short=False):
|
||||
try:
|
||||
result = repr(obj)
|
||||
except Exception:
|
||||
result = object.__repr__(obj)
|
||||
if not short or len(result) < _MAX_LENGTH:
|
||||
return result
|
||||
return result[:_MAX_LENGTH] + ' [truncated]...'
|
||||
|
||||
def strclass(cls):
|
||||
return "%s.%s" % (cls.__module__, cls.__qualname__)
|
||||
|
||||
def sorted_list_difference(expected, actual):
|
||||
"""Finds elements in only one or the other of two, sorted input lists.
|
||||
|
||||
Returns a two-element tuple of lists. The first list contains those
|
||||
elements in the "expected" list but not in the "actual" list, and the
|
||||
second contains those elements in the "actual" list but not in the
|
||||
"expected" list. Duplicate elements in either input list are ignored.
|
||||
"""
|
||||
i = j = 0
|
||||
missing = []
|
||||
unexpected = []
|
||||
while True:
|
||||
try:
|
||||
e = expected[i]
|
||||
a = actual[j]
|
||||
if e < a:
|
||||
missing.append(e)
|
||||
i += 1
|
||||
while expected[i] == e:
|
||||
i += 1
|
||||
elif e > a:
|
||||
unexpected.append(a)
|
||||
j += 1
|
||||
while actual[j] == a:
|
||||
j += 1
|
||||
else:
|
||||
i += 1
|
||||
try:
|
||||
while expected[i] == e:
|
||||
i += 1
|
||||
finally:
|
||||
j += 1
|
||||
while actual[j] == a:
|
||||
j += 1
|
||||
except IndexError:
|
||||
missing.extend(expected[i:])
|
||||
unexpected.extend(actual[j:])
|
||||
break
|
||||
return missing, unexpected
|
||||
|
||||
|
||||
def unorderable_list_difference(expected, actual):
|
||||
"""Same behavior as sorted_list_difference but
|
||||
for lists of unorderable items (like dicts).
|
||||
|
||||
As it does a linear search per item (remove) it
|
||||
has O(n*n) performance."""
|
||||
missing = []
|
||||
while expected:
|
||||
item = expected.pop()
|
||||
try:
|
||||
actual.remove(item)
|
||||
except ValueError:
|
||||
missing.append(item)
|
||||
|
||||
# anything left in actual is unexpected
|
||||
return missing, actual
|
||||
|
||||
def three_way_cmp(x, y):
|
||||
"""Return -1 if x < y, 0 if x == y and 1 if x > y"""
|
||||
return (x > y) - (x < y)
|
||||
|
||||
_Mismatch = namedtuple('Mismatch', 'actual expected value')
|
||||
|
||||
def _count_diff_all_purpose(actual, expected):
|
||||
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
|
||||
# elements need not be hashable
|
||||
s, t = list(actual), list(expected)
|
||||
m, n = len(s), len(t)
|
||||
NULL = object()
|
||||
result = []
|
||||
for i, elem in enumerate(s):
|
||||
if elem is NULL:
|
||||
continue
|
||||
cnt_s = cnt_t = 0
|
||||
for j in range(i, m):
|
||||
if s[j] == elem:
|
||||
cnt_s += 1
|
||||
s[j] = NULL
|
||||
for j, other_elem in enumerate(t):
|
||||
if other_elem == elem:
|
||||
cnt_t += 1
|
||||
t[j] = NULL
|
||||
if cnt_s != cnt_t:
|
||||
diff = _Mismatch(cnt_s, cnt_t, elem)
|
||||
result.append(diff)
|
||||
|
||||
for i, elem in enumerate(t):
|
||||
if elem is NULL:
|
||||
continue
|
||||
cnt_t = 0
|
||||
for j in range(i, n):
|
||||
if t[j] == elem:
|
||||
cnt_t += 1
|
||||
t[j] = NULL
|
||||
diff = _Mismatch(0, cnt_t, elem)
|
||||
result.append(diff)
|
||||
return result
|
||||
|
||||
def _count_diff_hashable(actual, expected):
|
||||
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
|
||||
# elements must be hashable
|
||||
s, t = Counter(actual), Counter(expected)
|
||||
result = []
|
||||
for elem, cnt_s in s.items():
|
||||
cnt_t = t.get(elem, 0)
|
||||
if cnt_s != cnt_t:
|
||||
diff = _Mismatch(cnt_s, cnt_t, elem)
|
||||
result.append(diff)
|
||||
for elem, cnt_t in t.items():
|
||||
if elem not in s:
|
||||
diff = _Mismatch(0, cnt_t, elem)
|
||||
result.append(diff)
|
||||
return result
|
||||
Reference in New Issue
Block a user