| """Implements the Astropy TestRunner which is a thin wrapper around py.test.""" |
|
|
| import inspect |
| import os |
| import glob |
| import copy |
| import shlex |
| import sys |
| import tempfile |
| import warnings |
| import importlib |
| from collections import OrderedDict |
| from importlib.util import find_spec |
| from functools import wraps |
|
|
| from astropy.config.paths import set_temp_config, set_temp_cache |
| from astropy.utils import find_current_module |
| from astropy.utils.exceptions import AstropyWarning, AstropyDeprecationWarning |
|
|
| __all__ = ['TestRunner', 'TestRunnerBase', 'keyword'] |
|
|
|
|
| class keyword: |
| """ |
| A decorator to mark a method as keyword argument for the ``TestRunner``. |
| |
| Parameters |
| ---------- |
| default_value : `object` |
| The default value for the keyword argument. (Default: `None`) |
| |
| priority : `int` |
| keyword argument methods are executed in order of descending priority. |
| """ |
|
|
| def __init__(self, default_value=None, priority=0): |
| self.default_value = default_value |
| self.priority = priority |
|
|
| def __call__(self, f): |
| def keyword(*args, **kwargs): |
| return f(*args, **kwargs) |
|
|
| keyword._default_value = self.default_value |
| keyword._priority = self.priority |
| |
| |
| keyword.__doc__ = f.__doc__ |
|
|
| return keyword |
|
|
|
|
| class TestRunnerBase: |
| """ |
| The base class for the TestRunner. |
| |
| A test runner can be constructed by creating a subclass of this class and |
| defining 'keyword' methods. These are methods that have the |
| `~astropy.tests.runner.keyword` decorator, these methods are used to |
| construct allowed keyword arguments to the |
| `~astropy.tests.runner.TestRunnerBase.run_tests` method as a way to allow |
| customization of individual keyword arguments (and associated logic) |
| without having to re-implement the whole |
| `~astropy.tests.runner.TestRunnerBase.run_tests` method. |
| |
| Examples |
| -------- |
| |
| A simple keyword method:: |
| |
| class MyRunner(TestRunnerBase): |
| |
| @keyword('default_value'): |
| def spam(self, spam, kwargs): |
| \"\"\" |
| spam : `str` |
| The parameter description for the run_tests docstring. |
| \"\"\" |
| # Return value must be a list with a CLI parameter for pytest. |
| return ['--spam={}'.format(spam)] |
| """ |
|
|
| def __init__(self, base_path): |
| self.base_path = os.path.abspath(base_path) |
|
|
| def __new__(cls, *args, **kwargs): |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| functions = inspect.getmembers(cls, predicate=inspect.isfunction) |
|
|
| |
| keywords = filter(lambda func: func[1].__name__ == 'keyword', functions) |
| |
| sorted_keywords = sorted(keywords, key=lambda x: x[1]._priority, reverse=True) |
|
|
| cls.keywords = OrderedDict() |
| doc_keywords = "" |
| for name, func in sorted_keywords: |
| |
| |
| |
| |
| |
| |
| |
| try: |
| |
| |
| if func(None, False, None) is NotImplemented: |
| continue |
| except Exception: |
| pass |
|
|
| |
| cls.keywords[name] = func._default_value |
| if func.__doc__: |
| doc_keywords += ' '*8 |
| doc_keywords += func.__doc__.strip() |
| doc_keywords += '\n\n' |
|
|
| cls.run_tests.__doc__ = cls.RUN_TESTS_DOCSTRING.format(keywords=doc_keywords) |
|
|
| return super().__new__(cls) |
|
|
| def _generate_args(self, **kwargs): |
| |
| |
| keywords = copy.deepcopy(self.keywords) |
| keywords.update(kwargs) |
| |
| args = [] |
| for keyword in keywords.keys(): |
| func = getattr(self, keyword) |
| result = func(keywords[keyword], keywords) |
|
|
| |
| if result is NotImplemented: |
| raise TypeError("run_tests() got an unexpected keyword argument {}".format(keyword)) |
|
|
| |
| if not isinstance(result, list): |
| raise TypeError("{} keyword method must return a list".format(keyword)) |
|
|
| args += result |
|
|
| return args |
|
|
| RUN_TESTS_DOCSTRING = \ |
| """ |
| Run the tests for the package. |
| |
| This method builds arguments for and then calls ``pytest.main``. |
| |
| Parameters |
| ---------- |
| {keywords} |
| |
| """ |
|
|
| _required_dependancies = ['pytest', 'pytest_remotedata', 'pytest_doctestplus'] |
| _missing_dependancy_error = "Test dependencies are missing. You should install the 'pytest-astropy' package." |
|
|
| @classmethod |
| def _has_test_dependencies(cls): |
| |
| |
| for module in cls._required_dependancies: |
| spec = find_spec(module) |
| |
| if spec is None or spec.loader is None: |
| raise RuntimeError(cls._missing_dependancy_error) |
|
|
| def run_tests(self, **kwargs): |
|
|
| |
| |
| |
| |
| |
| if kwargs.pop('add_local_eggs_to_path', False): |
|
|
| |
| for egg in glob.glob(os.path.join('.eggs', '*.egg')): |
| sys.path.insert(0, egg) |
|
|
| |
| |
| import pkg_resources |
| importlib.reload(pkg_resources) |
|
|
| self._has_test_dependencies() |
|
|
| |
| |
|
|
| |
| import pytest |
|
|
| |
| allowed_kwargs = set(self.keywords.keys()) |
| passed_kwargs = set(kwargs.keys()) |
| if not passed_kwargs.issubset(allowed_kwargs): |
| wrong_kwargs = list(passed_kwargs.difference(allowed_kwargs)) |
| raise TypeError("run_tests() got an unexpected keyword argument {}".format(wrong_kwargs[0])) |
|
|
| args = self._generate_args(**kwargs) |
|
|
| if kwargs.get('plugins', None) is not None: |
| plugins = kwargs.pop('plugins') |
| elif self.keywords.get('plugins', None) is not None: |
| plugins = self.keywords['plugins'] |
| else: |
| plugins = [] |
|
|
| |
| |
| |
| |
| |
| |
| |
| astropy_config = tempfile.mkdtemp('astropy_config') |
| astropy_cache = tempfile.mkdtemp('astropy_cache') |
|
|
| |
| |
| |
| |
| with set_temp_config(astropy_config, delete=True): |
| with set_temp_cache(astropy_cache, delete=True): |
| return pytest.main(args=args, plugins=plugins) |
|
|
| @classmethod |
| def make_test_runner_in(cls, path): |
| """ |
| Constructs a `TestRunner` to run in the given path, and returns a |
| ``test()`` function which takes the same arguments as |
| `TestRunner.run_tests`. |
| |
| The returned ``test()`` function will be defined in the module this |
| was called from. This is used to implement the ``astropy.test()`` |
| function (or the equivalent for affiliated packages). |
| """ |
|
|
| runner = cls(path) |
|
|
| @wraps(runner.run_tests, ('__doc__',)) |
| def test(**kwargs): |
| return runner.run_tests(**kwargs) |
|
|
| module = find_current_module(2) |
| if module is not None: |
| test.__module__ = module.__name__ |
|
|
| |
| |
| |
| |
| |
| |
| if hasattr(test, '__wrapped__'): |
| del test.__wrapped__ |
|
|
| test.__test__ = False |
| return test |
|
|
|
|
| class TestRunner(TestRunnerBase): |
| """ |
| A test runner for astropy tests |
| """ |
|
|
| def packages_path(self, packages, base_path, error=None, warning=None): |
| """ |
| Generates the path for multiple packages. |
| |
| Parameters |
| ---------- |
| packages : str |
| Comma separated string of packages. |
| base_path : str |
| Base path to the source code or documentation. |
| error : str |
| Error message to be raised as ``ValueError``. Individual package |
| name and path can be accessed by ``{name}`` and ``{path}`` |
| respectively. No error is raised if `None`. (Default: `None`) |
| warning : str |
| Warning message to be issued. Individual package |
| name and path can be accessed by ``{name}`` and ``{path}`` |
| respectively. No warning is issues if `None`. (Default: `None`) |
| |
| Returns |
| ------- |
| paths : list of str |
| List of stings of existing package paths. |
| """ |
| packages = packages.split(",") |
|
|
| paths = [] |
| for package in packages: |
| path = os.path.join( |
| base_path, package.replace('.', os.path.sep)) |
| if not os.path.isdir(path): |
| info = {'name': package, 'path': path} |
| if error is not None: |
| raise ValueError(error.format(**info)) |
| if warning is not None: |
| warnings.warn(warning.format(**info)) |
| else: |
| paths.append(path) |
|
|
| return paths |
|
|
| |
| @keyword(priority=1000) |
| def coverage(self, coverage, kwargs): |
| if coverage: |
| warnings.warn( |
| "The coverage option is ignored on run_tests, since it " |
| "can not be made to work in that context. Use " |
| "'python setup.py test --coverage' instead.", |
| AstropyWarning) |
|
|
| return [] |
|
|
| |
| |
| @keyword(priority=1) |
| def package(self, package, kwargs): |
| """ |
| package : str, optional |
| The name of a specific package to test, e.g. 'io.fits' or |
| 'utils'. Accepts comma separated string to specify multiple |
| packages. If nothing is specified all default tests are run. |
| """ |
| if package is None: |
| self.package_path = [self.base_path] |
| else: |
| error_message = ('package to test is not found: {name} ' |
| '(at path {path}).') |
| self.package_path = self.packages_path(package, self.base_path, |
| error=error_message) |
|
|
| if not kwargs['test_path']: |
| return self.package_path |
|
|
| return [] |
|
|
| @keyword() |
| def test_path(self, test_path, kwargs): |
| """ |
| test_path : str, optional |
| Specify location to test by path. May be a single file or |
| directory. Must be specified absolutely or relative to the |
| calling directory. |
| """ |
| all_args = [] |
| |
| self.package(kwargs['package'], kwargs) |
| if test_path: |
| base, ext = os.path.splitext(test_path) |
|
|
| if ext in ('.rst', ''): |
| if kwargs['docs_path'] is None: |
| |
| raise ValueError( |
| "Can not test .rst files without a docs_path " |
| "specified.") |
|
|
| abs_docs_path = os.path.abspath(kwargs['docs_path']) |
| abs_test_path = os.path.abspath( |
| os.path.join(abs_docs_path, os.pardir, test_path)) |
|
|
| common = os.path.commonprefix((abs_docs_path, abs_test_path)) |
|
|
| if os.path.exists(abs_test_path) and common == abs_docs_path: |
| |
| all_args.append('--doctest-rst') |
| test_path = abs_test_path |
|
|
| |
| |
| |
| if not (os.path.isdir(test_path) or ('.py' in test_path or '.rst' in test_path)): |
| raise ValueError("Test path must be a directory or a path to " |
| "a .py or .rst file") |
|
|
| return all_args + [test_path] |
|
|
| return [] |
|
|
| @keyword() |
| def args(self, args, kwargs): |
| """ |
| args : str, optional |
| Additional arguments to be passed to ``pytest.main`` in the ``args`` |
| keyword argument. |
| """ |
| if args: |
| return shlex.split(args, posix=not sys.platform.startswith('win')) |
|
|
| return [] |
|
|
| @keyword(default_value=['astropy.tests.plugins.display']) |
| def plugins(self, plugins, kwargs): |
| """ |
| plugins : list, optional |
| Plugins to be passed to ``pytest.main`` in the ``plugins`` keyword |
| argument. |
| """ |
| |
| |
| return [] |
|
|
| @keyword() |
| def verbose(self, verbose, kwargs): |
| """ |
| verbose : bool, optional |
| Convenience option to turn on verbose output from py.test. Passing |
| True is the same as specifying ``-v`` in ``args``. |
| """ |
| if verbose: |
| return ['-v'] |
|
|
| return [] |
|
|
| @keyword() |
| def pastebin(self, pastebin, kwargs): |
| """ |
| pastebin : ('failed', 'all', None), optional |
| Convenience option for turning on py.test pastebin output. Set to |
| 'failed' to upload info for failed tests, or 'all' to upload info |
| for all tests. |
| """ |
| if pastebin is not None: |
| if pastebin in ['failed', 'all']: |
| return ['--pastebin={0}'.format(pastebin)] |
| else: |
| raise ValueError("pastebin should be 'failed' or 'all'") |
|
|
| return [] |
|
|
| @keyword(default_value='none') |
| def remote_data(self, remote_data, kwargs): |
| """ |
| remote_data : {'none', 'astropy', 'any'}, optional |
| Controls whether to run tests marked with @pytest.mark.remote_data. This can be |
| set to run no tests with remote data (``none``), only ones that use |
| data from http://data.astropy.org (``astropy``), or all tests that |
| use remote data (``any``). The default is ``none``. |
| """ |
|
|
| if remote_data is True: |
| remote_data = 'any' |
| elif remote_data is False: |
| remote_data = 'none' |
| elif remote_data not in ('none', 'astropy', 'any'): |
| warnings.warn("The remote_data option should be one of " |
| "none/astropy/any (found {0}). For backward-compatibility, " |
| "assuming 'any', but you should change the option to be " |
| "one of the supported ones to avoid issues in " |
| "future.".format(remote_data), |
| AstropyDeprecationWarning) |
| remote_data = 'any' |
|
|
| return ['--remote-data={0}'.format(remote_data)] |
|
|
| @keyword() |
| def pep8(self, pep8, kwargs): |
| """ |
| pep8 : bool, optional |
| Turn on PEP8 checking via the pytest-pep8 plugin and disable normal |
| tests. Same as specifying ``--pep8 -k pep8`` in ``args``. |
| """ |
| if pep8: |
| try: |
| import pytest_pep8 |
| except ImportError: |
| raise ImportError('PEP8 checking requires pytest-pep8 plugin: ' |
| 'http://pypi.python.org/pypi/pytest-pep8') |
| else: |
| return ['--pep8', '-k', 'pep8'] |
|
|
| return [] |
|
|
| @keyword() |
| def pdb(self, pdb, kwargs): |
| """ |
| pdb : bool, optional |
| Turn on PDB post-mortem analysis for failing tests. Same as |
| specifying ``--pdb`` in ``args``. |
| """ |
| if pdb: |
| return ['--pdb'] |
| return [] |
|
|
| @keyword() |
| def open_files(self, open_files, kwargs): |
| """ |
| open_files : bool, optional |
| Fail when any tests leave files open. Off by default, because |
| this adds extra run time to the test suite. Requires the |
| ``psutil`` package. |
| """ |
| if open_files: |
| if kwargs['parallel'] != 0: |
| raise SystemError( |
| "open file detection may not be used in conjunction with " |
| "parallel testing.") |
|
|
| try: |
| import psutil |
| except ImportError: |
| raise SystemError( |
| "open file detection requested, but psutil package " |
| "is not installed.") |
|
|
| return ['--open-files'] |
|
|
| print("Checking for unclosed files") |
|
|
| return [] |
|
|
| @keyword(0) |
| def parallel(self, parallel, kwargs): |
| """ |
| parallel : int or 'auto', optional |
| When provided, run the tests in parallel on the specified |
| number of CPUs. If parallel is ``'auto'``, it will use the all |
| the cores on the machine. Requires the ``pytest-xdist`` plugin. |
| """ |
| if parallel != 0: |
| try: |
| from xdist import plugin |
| except ImportError: |
| raise SystemError( |
| "running tests in parallel requires the pytest-xdist package") |
|
|
| return ['-n', str(parallel)] |
|
|
| return [] |
|
|
| @keyword() |
| def docs_path(self, docs_path, kwargs): |
| """ |
| docs_path : str, optional |
| The path to the documentation .rst files. |
| """ |
|
|
| paths = [] |
| if docs_path is not None and not kwargs['skip_docs']: |
| if kwargs['package'] is not None: |
| warning_message = ("Can not test .rst docs for {name}, since " |
| "docs path ({path}) does not exist.") |
| paths = self.packages_path(kwargs['package'], docs_path, |
| warning=warning_message) |
| elif not kwargs['test_path']: |
| paths = [docs_path, ] |
|
|
| if len(paths) and not kwargs['test_path']: |
| paths.append('--doctest-rst') |
|
|
| return paths |
|
|
| @keyword() |
| def skip_docs(self, skip_docs, kwargs): |
| """ |
| skip_docs : `bool`, optional |
| When `True`, skips running the doctests in the .rst files. |
| """ |
| |
| return [] |
|
|
| @keyword() |
| def repeat(self, repeat, kwargs): |
| """ |
| repeat : `int`, optional |
| If set, specifies how many times each test should be run. This is |
| useful for diagnosing sporadic failures. |
| """ |
| if repeat: |
| return ['--repeat={0}'.format(repeat)] |
|
|
| return [] |
|
|
| |
| def run_tests(self, **kwargs): |
|
|
| |
| |
| |
| from astropy.table import Table |
|
|
| return super().run_tests(**kwargs) |
|
|