def _make_tarball(self, target_name): tmpdir = self.mkdtemp() self.write_file([tmpdir, 'file1'], 'xxx') self.write_file([tmpdir, 'file2'], 'xxx') os.mkdir(os.path.join(tmpdir, 'sub')) self.write_file([tmpdir, 'sub', 'file3'], 'xxx') tmpdir2 = self.mkdtemp() unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], 'source and target should be on same drive') base_name = os.path.join(tmpdir2, target_name) old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(splitdrive(base_name)[1], '.') finally: os.chdir(old_dir) tarball = base_name + '.tar.gz' self.assertTrue(os.path.exists(tarball)) base_name = os.path.join(tmpdir2, target_name) old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(splitdrive(base_name)[1], '.', compress=None) finally: os.chdir(old_dir) tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball)) return
def _make_tarball(self, tmpdir, target_name, suffix, **kwargs): tmpdir2 = self.mkdtemp() unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive") base_name = os.path.join(tmpdir2, target_name) # working with relative paths to avoid tar warnings with change_cwd(tmpdir): make_tarball(splitdrive(base_name)[1], 'dist', **kwargs) # check if the compressed tarball was created tarball = base_name + suffix self.assertTrue(os.path.exists(tarball)) self.assertEqual(self._tarinfo(tarball), self._created_files)
def skip_if_missing_requirements(*requirements): try: pkg_resources.require(*requirements) msg = '' except pkg_resources.DistributionNotFound: msg = 'Missing one or more requirements (%s)' % '|'.join(requirements) return skipUnless(msg == '', msg)
def make_reviewed(fn): """ Just setting the WIDGY_MEZZANINE_SITE is not enough during tests, because WidgyPage.root_node has been set to point to VersionTracker. We have to manually point it to ReviewedVersionTracker. We do this only in tests because on a normal run, you are never going to be changing what models a ForeignKey points to (I would hope). """ from widgy.contrib.widgy_mezzanine.admin import publish_page_on_approve site = reviewed_widgy_site rel = WidgyPage._meta.get_field('root_node').rel old_model = rel.model dispatch_uid = str(uuid.uuid4()) fn = override_settings(WIDGY_MEZZANINE_SITE=site)(fn) fn = skipUnless(REVIEW_QUEUE_INSTALLED, 'review_queue is not installed')(fn) def up(): # BBB Django 1.8 compatiblity if django.VERSION < (1, 9): rel.to = site.get_version_tracker_model() else: rel.model = site.get_version_tracker_model() post_save.connect(publish_page_on_approve, sender=site.get_version_tracker_model().commit_model, dispatch_uid=dispatch_uid) def down(): # BBB Django 1.8 compatiblity if django.VERSION < (1, 9): rel.to = old_model else: rel.model = old_model post_save.disconnect(dispatch_uid=dispatch_uid) if isinstance(fn, type): old_pre_setup = fn._pre_setup old_post_teardown = fn._post_teardown def _pre_setup(self): up() old_pre_setup(self) def _post_teardown(self): old_post_teardown(self) down() fn._pre_setup = _pre_setup fn._post_teardown = _post_teardown return fn else: def change_foreign_key(*args, **kwargs): up() try: return fn(*args, **kwargs) finally: down() return change_foreign_key
def create_simple_test_method(validator, expected, value, num): if expected is not None and issubclass(expected, Exception): test_mask = 'test_%s_raises_error_%d' def test_func(self): # assertRaises not used, so as to be able to produce an error message # containing the tested value try: validator(value) except expected: pass else: self.fail("%s not raised when validating '%s'" % ( expected.__name__, value)) else: test_mask = 'test_%s_%d' def test_func(self): try: self.assertEqual(expected, validator(value)) except ValidationError as e: self.fail("Validation of '%s' failed. Error message was: %s" % ( value, str(e))) if isinstance(validator, types.FunctionType): val_name = validator.__name__ else: val_name = validator.__class__.__name__ test_name = test_mask % (val_name, num) if validator is validate_image_file_extension: SKIP_MSG = "Pillow is required to test validate_image_file_extension" test_func = skipUnless(PILLOW_IS_INSTALLED, SKIP_MSG)(test_func) return test_name, test_func
def add_test_based_on_test_class(class_, **attr): attr.update(create_storage=create_storage) new_class = class_.__class__( prefix + class_.__name__, (class_, ), attr, ) new_class = unittest.skipUnless(storage_is_available, "Storage not available")(new_class) suite.addTest(unittest.makeSuite(new_class))
def skipUnlessGit(test_item): '''Decorator that skips test outside git repo. There are very few tests that an be run only in git. One example is correctness of example code that won't get included in RPM. ''' # pylint: disable=invalid-name return unittest.skipUnless(in_git, 'outside git tree')(test_item)
def skip_unless_master(func_or_class): """ Only run the decorated test for code on master or destined for master. Use this to skip tests that we expect to fail on a named release branch. Please use carefully! """ return unittest.skipUnless(RELEASE_LINE == "master", "Test often fails on named releases")(func_or_class)
def skipUnlessDom0(test_item): '''Decorator that skips test outside dom0. Some tests (especially integration tests) have to be run in more or less working dom0. This is checked by connecting to libvirt. ''' # pylint: disable=invalid-name return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
def skipUnlessEnv(varname): '''Decorator generator for skipping tests without environment variable set. Some tests require working X11 display, like those using GTK library, which segfaults without connection to X. Other require their own, custom variables. ''' return unittest.skipUnless(os.getenv(varname), 'no {} set'.format(varname))
def requires_docker_image(image_name): """ Mark a test as requiring a Docker image to run. """ return unittest.skipUnless( docker_image_available(image_name), "Docker image {0} is required.".format(image_name) )
def skip_unless_pacemaker_features(version_tuple, feature): return skipUnless( is_minimum_pacemaker_features(*version_tuple), "Pacemaker must support feature set version {version} to test {feature}" .format( version=format_version(version_tuple), feature=feature ) )
def get_installed_locales(self, codes, msg=None): def normalize(code): # OS X and Ubuntu (at the very least) differ slightly in locale code formatting return code.strip().replace("-", "").lower() try: installed_codes = dict(((normalize(code), code) for code in subprocess.check_output(["locale", "-a"]).split())) except (subprocess.CalledProcessError, OSError): raise unittest.SkipTest("locale command not available, cannot test") if msg is None: msg = "One of %s tested locales is not installed" % (codes,) available_codes = [] for code in codes: if normalize(code) in installed_codes: available_codes.append(installed_codes[normalize(code)]) unittest.skipUnless(available_codes, msg) return available_codes
def skip_if_not_supported(y): msg = "strftime() is limited to [1; 9999] with Visual Studio" # Check that it doesn't crash for year > 9999 try: time.strftime('%Y', (y,) + (0,) * 8) except ValueError: cond = False else: cond = True return unittest.skipUnless(cond, msg)
def skipUnlessImported(module, obj): import importlib try: m = importlib.import_module(module) except ImportError: m = None return unittest.skipUnless( obj in dir(m), "Skipping test because {} could not be imported from {}".format( obj, module))
def _wrapper(*args, **kwargs): task = args[0].task task_args = inspect.getargs(task.func_code).args if isinstance(param, str): condition = param in task_args reason = "%s doesn't have parameter '%s'" % (task.__name__, param) else: # should be a list condition = all([p in task_args for p in param]) reason = "%s doesn't have parameter %s" % (task.__name__, map(lambda x: "'" + str(x) + "'", param)) return unittest.skipUnless(condition, reason)(func)(*args, **kwargs)
def dont_run_with_thread(obj): '''Decorator for disabling process based test cases when the test suite runs in threading, rather than processing, mode. ''' actor = pulsar.get_actor() if actor: d = unittest.skipUnless(actor.cfg.concurrency == 'process', 'Run only when concurrency is process') return d(obj) else: return obj
def test_make_tarball(self): # creating something to tar tmpdir = self.mkdtemp() self.write_file([tmpdir, "file1"], "xxx") self.write_file([tmpdir, "file2"], "xxx") os.mkdir(os.path.join(tmpdir, "sub")) self.write_file([tmpdir, "sub", "file3"], "xxx") tmpdir2 = self.mkdtemp() # force shutil to create the directory os.rmdir(tmpdir2) unittest.skipUnless( splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive" ) base_name = os.path.join(tmpdir2, "archive") # working with relative paths to avoid tar warnings old_dir = os.getcwd() os.chdir(tmpdir) try: _make_tarball(splitdrive(base_name)[1], ".") finally: os.chdir(old_dir) # check if the compressed tarball was created tarball = base_name + ".tar.gz" self.assertTrue(os.path.exists(tarball)) # trying an uncompressed one base_name = os.path.join(tmpdir2, "archive") old_dir = os.getcwd() os.chdir(tmpdir) try: _make_tarball(splitdrive(base_name)[1], ".", compress=None) finally: os.chdir(old_dir) tarball = base_name + ".tar" self.assertTrue(os.path.exists(tarball))
def skip_unless_pacemaker_version(version_tuple, feature): current_version = get_current_pacemaker_version() return skipUnless( is_version_sufficient(current_version, version_tuple), ( "Pacemaker version is too old (current: {current_version}," " must be >= {minimal_version}) to test {feature}" ).format( current_version=format_version(current_version), minimal_version=format_version(version_tuple), feature=feature ) )
def skipUnless(condition, reason): """ Skip a test unless the condition is true. """ if hasattr(condition, '__call__'): def decorator(test_item): test_item.__unittest_async_skip_unless__ = condition test_item.__unittest_skip_why__ = reason return test_item return decorator else: return unittest.skipUnless(condition, reason)
def django_hosts_installed_setup(func): func = override_settings( DEFAULT_HOST='default', ROOT_HOSTCONF='tenancy.tests.hosts', MIDDLEWARE_CLASSES=( 'django_hosts.middleware.HostsMiddleware', 'tenancy.middleware.TenantHostMiddleware' ) )(func) return skipUnless( django_hosts, 'django-hosts is not installed.' )(func)
def test_make_tarball(self): # creating something to tar tmpdir = self.mkdtemp() write_file((tmpdir, 'file1'), 'xxx') write_file((tmpdir, 'file2'), 'xxx') os.mkdir(os.path.join(tmpdir, 'sub')) write_file((tmpdir, 'sub', 'file3'), 'xxx') tmpdir2 = self.mkdtemp() # force shutil to create the directory os.rmdir(tmpdir2) unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive") base_name = os.path.join(tmpdir2, 'archive') # working with relative paths to avoid tar warnings old_dir = os.getcwd() os.chdir(tmpdir) try: _make_tarball(splitdrive(base_name)[1], '.') finally: os.chdir(old_dir) # check if the compressed tarball was created tarball = base_name + '.tar.gz' self.assertTrue(os.path.exists(tarball)) # trying an uncompressed one base_name = os.path.join(tmpdir2, 'archive') old_dir = os.getcwd() os.chdir(tmpdir) try: _make_tarball(splitdrive(base_name)[1], '.', compress=None) finally: os.chdir(old_dir) tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball))
def requires_tcl(*version): if len(version) <= 2: return unittest.skipUnless(tcl_version >= version, 'requires Tcl version >= ' + '.'.join(map(str, version))) def deco(test): @functools.wraps(test) def newtest(self): if get_tk_patchlevel() < (8, 6, 5): self.skipTest('requires Tcl version >= ' + '.'.join(map(str, get_tk_patchlevel()))) test(self) return newtest return deco
def django_hosts_installed_setup(func): func = override_settings( DEFAULT_HOST='default', ROOT_HOSTCONF='tests.hosts', **{MIDDLEWARE_SETTING: [ 'django_hosts.middleware.HostsRequestMiddleware', 'tenancy.middleware.TenantHostMiddleware', 'django_hosts.middleware.HostsResponseMiddleware', ]} )(func) return skipUnless( django_hosts, 'django-hosts is not installed.' )(func)
def wrapper(*args, **kwargs): config = get_blockdevice_config() configured_backend = config.pop('backend') skipper = skipUnless( configured_backend == required_backend, 'The backend in the supplied configuration ' 'is not suitable for this test. ' 'Found: {!r}. Required: {!r}.'.format( configured_backend, required_backend ) ) decorated_object = skipper(undecorated_object) result = decorated_object(*args, **kwargs) return result
def with_resolwe_host(wrapped_method, instance, args, kwargs): """Decorate unit test to give it access to a live Resolwe host. This is a slightly modified version of Resolwe's :func:`~resolwe.test.utils.with_resolwe_host` decorator which skips the decorated tests on non-Linux systems since accessing live Resolwe host from a Docker container on non-Linux systems is not possible yet. """ return unittest.skipUnless( sys.platform.startswith('linux'), "Accessing live Resolwe host from a Docker container on non-Linux systems is not possible " "yet." )(resolwe_with_resolwe_host)(wrapped_method)(*args, **kwargs)
def _make_tarball(self, target_name): # creating something to tar tmpdir = self.mkdtemp() self.write_file([tmpdir, 'file1'], 'xxx') self.write_file([tmpdir, 'file2'], 'xxx') os.mkdir(os.path.join(tmpdir, 'sub')) self.write_file([tmpdir, 'sub', 'file3'], 'xxx') tmpdir2 = self.mkdtemp() unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "Source and target should be on same drive") base_name = os.path.join(tmpdir2, target_name) # working with relative paths to avoid tar warnings old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(splitdrive(base_name)[1], '.') finally: os.chdir(old_dir) # check if the compressed tarball was created tarball = base_name + '.tar.gz' self.assertTrue(os.path.exists(tarball)) # trying an uncompressed one base_name = os.path.join(tmpdir2, target_name) old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(splitdrive(base_name)[1], '.', compress=None) finally: os.chdir(old_dir) tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball))
def Unless(condition, reason): """A decorator that skips test evaluation if the condition dot not hold. See documentation for the `If` decorator for more information. Args: condition: If false, the test is going to be skipped. reason: A reason why the test needed to be skipped. Returns: A decorator that can be applied to test method or classes. """ if callable(condition): return _UnlessLazy(condition, reason) else: return unittest.skipUnless(condition, reason)
def make_image_test(name, img, expected_root=None, expected_options=None, *extra_methods): """Test inspection of an image""" def setUp(self): self.img = image_for(img) self.img.converter.inspect() def testInspect(self): inspected = ET.fromstring(self.img.converter.inspect()) root = inspected.xpath(u"/guestconv/root[@name='{root}']" .format(root=expected_root)) self.assertEqual(len(root), 1, u'Expected to find root: {expected}\n{xml}' .format(expected=expected_root, xml=ET.tostring(inspected))) root = root[0] options = root.xpath(u'options') self.assertEqual(len(options), 1, u'No options in returned inspection xml') options = options[0] for name in expected_options: values = expected_options[name] option = options.xpath(u"option[@name='{}']".format(name)) self.assertEqual(len(option), 1, u'No {} option'.format(name)) option = option[0] for value in values: v = option.xpath(u"value[. = '{}']".format(value)) self.assertEqual(len(v), 1, u'value {} not found for option {}'. format(value, name)) methods = { 'setUp': setUp } if expected_root is not None and expected_options is not None: methods['testInspect'] = testInspect for method_set in extra_methods: methods.update(method_set) return unittest.skipUnless(os.path.exists(img), '{img} does not exist'.format(img=img))( type(name, (unittest.TestCase,), methods))
def skip_unless_path_separator_matches(func): if not PLATFORM_OS: return unittest.skip( 'this test uses path manipulation, but we cannot detect the OS ' 'targeted by the configuration so the path separator is unknown.' )(func) if PLATFORM_OS == 'windows': target_separator = '\\' else: target_separator = '/' return unittest.skipUnless( os.path.sep == target_separator, reason=( 'this test uses path manipulation, but the configuration targets ' 'an OS with a different path separator than your system.' ) )(func)
def skipUnlessTorch(testfn, reason='pytorch is not installed'): """Decorate a test to skip if torch is not installed.""" return unittest.skipUnless(TORCH_AVAILABLE, reason)(testfn)
def skipUnlessBPE(testfn, reason='Test requires a GPU'): """Decorate a test to skip if BPE is not installed.""" return unittest.skipUnless(BPE_INSTALLED, reason)(testfn)
def skipUnlessIronPython(): """Skips the test unless currently running on IronPython""" return unittest.skipUnless(is_cli, 'IronPython specific test')
""" try: socket.socket(family=socket.AF_UNIX).connect(DOCKER_SOCKET_PATH) except socket.error as e: if e.errno == errno.EACCES: return False if e.errno == errno.ENOENT: # Docker is not installed return False raise else: return True if_docker_configured = skipUnless( docker_accessible(), "User '{}' does not have permission " "to access the Docker server socket '{}'".format( pwd.getpwuid(os.geteuid()).pw_name, DOCKER_SOCKET_PATH)) def wait_for_unit_state(docker_client, unit_name, expected_activation_states): """ Wait until a unit is in the requested state. :param docker_client: A ``DockerClient`` instance. :param unicode unit_name: The name of the unit. :param expected_activation_states: Activation states to wait for. :return: ``Deferred`` that fires when required state has been reached. """ def is_in_states(units): for unit in units:
import tempfile from test.support import (captured_stdout, captured_stderr, requires_zlib, can_symlink, EnvironmentVarGuard, rmtree) import threading import unittest import venv try: import ctypes except ImportError: ctypes = None # Platforms that set sys._base_executable can create venvs from within # another venv, so no need to skip tests that require venv.create(). requireVenvCreate = unittest.skipUnless( hasattr(sys, '_base_executable') or sys.prefix == sys.base_prefix, 'cannot run venv.create from within a venv on this platform') def check_output(cmd, encoding=None): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding=encoding) out, err = p.communicate() if p.returncode: raise subprocess.CalledProcessError(p.returncode, cmd, out, err) return out, err class BaseTest(unittest.TestCase):
threading = test_support.import_module('threading') HOST = test_support.HOST PORT = 0 SUPPORTS_SSL = False if hasattr(poplib, 'POP3_SSL'): import ssl SUPPORTS_SSL = True CERTFILE = os.path.join( os.path.dirname(__file__) or os.curdir, "keycert3.pem") CAFILE = os.path.join( os.path.dirname(__file__) or os.curdir, "pycacert.pem") requires_ssl = skipUnless(SUPPORTS_SSL, 'SSL not supported') # the dummy data returned by server when LIST and RETR commands are issued LIST_RESP = b'1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n' RETR_RESP = b"""From: [email protected]\ \r\nContent-Type: text/plain\r\n\ MIME-Version: 1.0\r\n\ Subject: Dummy\r\n\ \r\n\ line1\r\n\ line2\r\n\ line3\r\n\ .\r\n""" class DummyPOP3Handler(asynchat.async_chat):
HAS_ALARM = hasattr(signal, "alarm") LONG_SELECT = 0.2 SHORT_SELECT = 0.01 # Tolerance values for timer/speed fluctuations. TOLERANCE = 0.75 # Detect whether we're running on Travis or AppVeyor. This # is used to skip some verification points inside of tests to # not randomly fail our CI due to wild timer/speed differences. TRAVIS_CI = "TRAVIS" in os.environ APPVEYOR = "APPVEYOR" in os.environ skipUnlessHasSelector = skipUnless(selectors.HAS_SELECT, "Platform doesn't have a selector") skipUnlessHasENOSYS = skipUnless(hasattr(errno, 'ENOSYS'), "Platform doesn't have errno.ENOSYS") skipUnlessHasAlarm = skipUnless(hasattr(signal, 'alarm'), "Platform doesn't have signal.alarm()") def patch_select_module(testcase, *keep, **replace): """ Helper function that removes all selectors from the select module except those listed in *keep and **replace. Those in keep will be kept if they exist in the select module and those in replace will be patched with the value that is given regardless if they exist or not. Cleanup will restore previous state. This helper also resets the selectors module so that a call to DefaultSelector() will do feature detection again. """ selectors._DEFAULT_SELECTOR = None for s in ['select', 'poll', 'epoll', 'kqueue']:
URL_LIB_COMMIT = URL_LIB_DETAIL + 'commit/' # Commit (POST) or revert (DELETE) all pending changes to this library URL_LIB_BLOCKS = URL_LIB_DETAIL + 'blocks/' # Get the list of XBlocks in this library, or add a new one URL_LIB_TEAM = URL_LIB_DETAIL + 'team/' # Get the list of users/groups authorized to use this library URL_LIB_TEAM_USER = URL_LIB_TEAM + 'user/{username}/' # Add/edit/remove a user's permission to use this library URL_LIB_TEAM_GROUP = URL_LIB_TEAM + 'group/{group_name}/' # Add/edit/remove a group's permission to use this library URL_LIB_BLOCK = URL_PREFIX + 'blocks/{block_key}/' # Get data about a block, or delete it URL_LIB_BLOCK_OLX = URL_LIB_BLOCK + 'olx/' # Get or set the OLX of the specified XBlock URL_LIB_BLOCK_ASSETS = URL_LIB_BLOCK + 'assets/' # List the static asset files of the specified XBlock URL_LIB_BLOCK_ASSET_FILE = URL_LIB_BLOCK + 'assets/{file_name}' # Get, delete, or upload a specific static asset file URL_BLOCK_RENDER_VIEW = '/api/xblock/v2/xblocks/{block_key}/view/{view_name}/' URL_BLOCK_GET_HANDLER_URL = '/api/xblock/v2/xblocks/{block_key}/handler_url/{handler_name}/' URL_BLOCK_METADATA_URL = '/api/xblock/v2/xblocks/{block_key}/' # Decorator for tests that require blockstore requires_blockstore = unittest.skipUnless( settings.RUN_BLOCKSTORE_TESTS, "Requires a running Blockstore server") def elasticsearch_test(func): """ Decorator for tests which connect to elasticsearch when needed """ # This is disabled by default. Set to True if the elasticsearch engine is needed to test parts of code. if settings.ENABLE_ELASTICSEARCH_FOR_TESTS: func = override_settings( SEARCH_ENGINE="search.elastic.ElasticSearchEngine")(func) func = override_settings( ELASTIC_SEARCH_CONFIG=[{ 'use_ssl': settings.TEST_ELASTICSEARCH_USE_SSL, 'host': settings.TEST_ELASTICSEARCH_HOST, 'port': settings.TEST_ELASTICSEARCH_PORT,
import socketserver import test.support from test.support import reap_children, verbose from test.support import os_helper from test.support import socket_helper from test.support import threading_helper test.support.requires("network") TEST_STR = b"hello world\n" HOST = socket_helper.HOST HAVE_UNIX_SOCKETS = hasattr(socket, "AF_UNIX") requires_unix_sockets = unittest.skipUnless(HAVE_UNIX_SOCKETS, 'requires Unix sockets') HAVE_FORKING = hasattr(os, "fork") requires_forking = unittest.skipUnless(HAVE_FORKING, 'requires forking') def signal_alarm(n): """Call signal.alarm when it exists (i.e. not on Windows).""" if hasattr(signal, 'alarm'): signal.alarm(n) # Remember real select() to avoid interferences with mocking _real_select = select.select def receive(sock, n, timeout=test.support.SHORT_TIMEOUT): r, w, x = _real_select([sock], [], [], timeout) if sock in r: return sock.recv(n)
class Only: """ Limit tests to given devices """ softhsm2 = unittest.skipUnless(Is.softhsm2, "SoftHSMv2 only")
def skip_if_no_cuda(testfn, reason="Cuda is not available"): return unittest.skipUnless(CUDA_AVAILBLE, reason)(testfn)
def skip_if_no_network(testfn, reason="Network is not available"): return unittest.skipUnless(NETWORK_AVAILABLE, reason)(testfn)
def _impl(reason=None): if reason is None: if default_reason is None: raise ValueError('%s requires a reason for skipping.' % name) reason = default_reason return unittest.skipUnless(guard(), reason)
def skip_unless_lms(func): """ Only run the decorated test in the LMS test suite """ return skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in LMS')(func)
from test import support from test.support import _4G, bigmemtest, import_fresh_module from http.client import HTTPException # Were we compiled --with-pydebug or with #define Py_DEBUG? COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) try: import _blake2 except ImportError: _blake2 = None requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2') try: import _sha3 except ImportError: _sha3 = None requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3') def hexstr(s): assert isinstance(s, bytes), repr(s) h = "0123456789abcdef" r = '' for i in s: r += h[(i >> 4) & 0xF] + h[i & 0xF]
validate_schema_agreement(node, is_upgraded) def compact_sstable(self, node, sstable): mbean = make_mbean('db', type='CompactionManager') with JolokiaAgent(node) as jmx: jmx.execute_method(mbean, 'forceUserDefinedCompaction', [sstable]) def get_all_sstables(self, node): # note that node.get_sstables(...) only returns current version sstables keyspace_dirs = [ os.path.join(node.get_path(), "data{0}".format(x), "test13294") for x in xrange(0, node.cluster.data_dir_count) ] files = [] for d in keyspace_dirs: for f in glob.glob(d + "/*/*Data*"): files.append(f) return files for path in build_upgrade_pairs(): gen_class_name = TestForRegressions.__name__ + path.name assert_not_in(gen_class_name, globals()) spec = {'UPGRADE_PATH': path, '__test__': True} upgrade_applies_to_env = RUN_STATIC_UPGRADE_MATRIX or path.upgrade_meta.matches_current_env_version_family globals()[gen_class_name] = skipUnless(upgrade_applies_to_env, 'test not applicable to env.')(type( gen_class_name, (TestForRegressions, ), spec))
def skipUnlessGIMarshallingTests(func): return unittest.skipUnless(GIMarshallingTests, "GIMarshallingTests missing")(func)
def define_skip_condition(flag: str, explanation: str): return unittest.skipUnless( os.environ.get(flag, 0) or os.environ.get("TEST_ALL", 0), explanation + f" Set `{flag}=1` or `TEST_ALL=1` to run.", )
import unittest from test import support import binascii import copy import pickle import random import sys from test.support import bigmemtest, _1G, _4G, import_helper zlib = import_helper.import_module('zlib') requires_Compress_copy = unittest.skipUnless( hasattr(zlib.compressobj(), "copy"), 'requires Compress.copy()') requires_Decompress_copy = unittest.skipUnless( hasattr(zlib.decompressobj(), "copy"), 'requires Decompress.copy()') class VersionTestCase(unittest.TestCase): # TODO: RUSTPYTHON @unittest.expectedFailure def test_library_version(self): # Test that the major version of the actual library in use matches the # major version that we were compiled against. We can't guarantee that # the minor versions will match (even on the machine on which the module # was compiled), and the API is stable between minor versions, so # testing only the major versions avoids spurious failures. self.assertEqual(zlib.ZLIB_RUNTIME_VERSION[0], zlib.ZLIB_VERSION[0]) class ChecksumTestCase(unittest.TestCase):
from test.support import (captured_stdout, captured_stderr, requires_zlib, can_symlink, EnvironmentVarGuard, rmtree, import_module) import unittest import venv from unittest.mock import patch try: import ctypes except ImportError: ctypes = None # Platforms that set sys._base_executable can create venvs from within # another venv, so no need to skip tests that require venv.create(). requireVenvCreate = unittest.skipUnless( sys.prefix == sys.base_prefix or sys._base_executable != sys.executable, 'cannot run venv.create from within a venv on this platform') def check_output(cmd, encoding=None): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding=encoding) out, err = p.communicate() if p.returncode: raise subprocess.CalledProcessError(p.returncode, cmd, out, err) return out, err class BaseTest(unittest.TestCase):
from test import support from test.support import findfile, run_unittest, TESTFN TEST_XMLFILE = findfile('test.xml', subdir='xmltestdata') TEST_XMLFILE_OUT = findfile('test.xml.out', subdir='xmltestdata') try: TEST_XMLFILE.encode('utf-8') TEST_XMLFILE_OUT.encode('utf-8') except UnicodeEncodeError: raise unittest.SkipTest('filename is not encodable to utf8') supports_nonascii_filenames = True if not os.path.supports_unicode_filenames: try: support.TESTFN_UNICODE.encode(support.TESTFN_ENCODING) except (UnicodeError, TypeError): supports_nonascii_filenames = False requires_nonascii_filenames = unittest.skipUnless( supports_nonascii_filenames, 'Requires non-ascii filenames support') ns_uri = 'http://www.python.org/xml-ns/saxtest/' class XmlTestBase(unittest.TestCase): def verify_empty_attrs(self, attrs): self.assertRaises(KeyError, attrs.getValue, 'attr') self.assertRaises(KeyError, attrs.getValueByQName, 'attr') self.assertRaises(KeyError, attrs.getNameByQName, 'attr') self.assertRaises(KeyError, attrs.getQNameByName, 'attr') self.assertRaises(KeyError, attrs.__getitem__, 'attr') self.assertEqual(attrs.getLength(), 0) self.assertEqual(attrs.getNames(), []) self.assertEqual(attrs.getQNames(), []) self.assertEqual(len(attrs), 0) self.assertNotIn('attr', attrs)
import test.test_support as support from test.test_support import findfile, run_unittest, TESTFN import unittest TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata") TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata") supports_unicode_filenames = True if not os.path.supports_unicode_filenames: try: support.TESTFN_UNICODE.encode(support.TESTFN_ENCODING) except (AttributeError, UnicodeError, TypeError): # Either the file system encoding is None, or the file name # cannot be encoded in the file system encoding. supports_unicode_filenames = False requires_unicode_filenames = unittest.skipUnless( supports_unicode_filenames, 'Requires unicode filenames support') ns_uri = "http://www.python.org/xml-ns/saxtest/" class XmlTestBase(unittest.TestCase): def verify_empty_attrs(self, attrs): self.assertRaises(KeyError, attrs.getValue, "attr") self.assertRaises(KeyError, attrs.getValueByQName, "attr") self.assertRaises(KeyError, attrs.getNameByQName, "attr") self.assertRaises(KeyError, attrs.getQNameByName, "attr") self.assertRaises(KeyError, attrs.__getitem__, "attr") self.assertEqual(attrs.getLength(), 0) self.assertEqual(attrs.getNames(), []) self.assertEqual(attrs.getQNames(), []) self.assertEqual(len(attrs), 0)
# -*- encoding:utf-8 -*- from __future__ import ( absolute_import, division, print_function, unicode_literals, ) import sys from contextlib import contextmanager from unittest import skipUnless from django.db import DEFAULT_DB_ALIAS, connection, connections from django.test.utils import CaptureQueriesContext from django.utils import six requiresPython2 = skipUnless(six.PY2, "Python 2 only") # Copied from Django 1.8 @contextmanager def captured_output(stream_name): """Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO. Note: This function and the following ``captured_std*`` are copied from CPython's ``test.support`` module.""" orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, six.StringIO()) try: yield getattr(sys, stream_name) finally: setattr(sys, stream_name, orig_stdout)
logger = logging.getLogger(logger_name) orig = getattr(logger, log_level) setattr(logger, log_level, replacement) try: yield calls finally: setattr(logger, log_level, orig) # On OSes that don't provide tzset (Windows), we can't set the timezone # in which the program runs. As a consequence, we must skip tests that # don't enforce a specific timezone (with timezone.override or equivalent), # or attempt to interpret naive datetimes in the default timezone. requires_tz_support = skipUnless( TZ_SUPPORT, "This test relies on the ability to run a program in an arbitrary " "time zone, but your operating system isn't able to do that.") @contextmanager def extend_sys_path(*paths): """Context manager to temporarily add paths to sys.path.""" _orig_sys_path = sys.path[:] sys.path.extend(paths) try: yield finally: sys.path = _orig_sys_path @contextmanager
def skipUnlessGPU(testfn, reason='Test requires a GPU'): """Decorate a test to skip if no GPU is available.""" return unittest.skipUnless(GPU_AVAILABLE, reason)(testfn)
def skipUnlessPathExists(path): return unittest.skipUnless(os.path.exists(path), ('File or directory at path "%s" used in test is' ' not available in the system' % path))
def skip_database(): return unittest.skipUnless(os.environ.get('INTELMQ_TEST_DATABASES'), 'Skipping database tests.')
def requires_tcl(*version): return unittest.skipUnless( tcl_version >= version, 'requires Tcl version >= ' + '.'.join(map(str, version)))
from decimal import Decimal from test.test_support import run_unittest import math import numbers import operator import fractions import unittest from copy import copy, deepcopy from cPickle import dumps, loads F = fractions.Fraction gcd = fractions.gcd # decorator for skipping tests on non-IEEE 754 platforms requires_IEEE_754 = unittest.skipUnless( float.__getformat__("double").startswith("IEEE"), "test requires IEEE 754 doubles") class DummyFloat(object): """Dummy float class for testing comparisons with Fractions""" def __init__(self, value): if not isinstance(value, float): raise TypeError("DummyFloat can only be initialized from float") self.value = value def _richcmp(self, other, op): if isinstance(other, numbers.Rational): return op(F.from_float(self.value), other) elif isinstance(other, DummyFloat): return op(self.value, other.value)
# Some simple queue module tests, plus some failure conditions # to ensure the Queue locks remain stable. import itertools import random import threading import time import unittest import weakref from test import support from test.support import import_helper from test.support import threading_helper py_queue = import_helper.import_fresh_module('queue', blocked=['_queue']) c_queue = import_helper.import_fresh_module('queue', fresh=['_queue']) need_c_queue = unittest.skipUnless(c_queue, "No _queue module found") QUEUE_SIZE = 5 def qfull(q): return q.maxsize > 0 and q.qsize() == q.maxsize # A thread to run a function that unclogs a blocked Queue. class _TriggerThread(threading.Thread): def __init__(self, fn, args): self.fn = fn self.args = args self.startedEvent = threading.Event() threading.Thread.__init__(self)