Module: testing.decorators — IPython 5.3.0 documentation

# Module: `testing.decorators`

Decorators for labeling test objects.

Decorators that merely return a modified version of the original function object are straightforward. Decorators that return a new function object need to use nose.tools.make_decorator(original_function)(decorator) in returning the decorator, in order to preserve metadata such as function name, setup and teardown functions and so on - see nose.tools for more information.

This module provides a set of useful decorators meant to be ready to use in your own tests. See the bottom of the file for the ready-made ones, and if you find yourself writing a new one that may be of generic use, add it here.

Included decorators:

Lightweight testing that remains unittest-compatible.

- An @as_unittest decorator can be used to tag any normal parameter-less function as a unittest TestCase. Then, both nose and normal unittest will recognize it as such. This will make it easier to migrate away from Nose if we ever need/want to while maintaining very lightweight tests.

NOTE: This file contains IPython-specific decorators. Using the machinery in IPython.external.decorators, we import either numpy.testing.decorators if numpy is available, OR use equivalent code in IPython.external._decorators, which we’ve copied verbatim from numpy.

## 11 Functions

`IPython.testing.decorators.` `as_unittest`(func)

  Decorator to make a simple function into a normal test via unittest.

`IPython.testing.decorators.` `apply_wrapper`(wrapper, func)

  Apply a wrapper to a function for decoration.

  This mixes Michele Simionato’s decorator tool with nose’s make_decorator, to apply a wrapper in a decorator so that all nose attributes, as well as function signature and other properties, survive the decoration cleanly. This will ensure that wrapped functions can still be well introspected via IPython, for example.

`IPython.testing.decorators.` `make_label_dec`(label, ds=None)

  Factory function to create a decorator that applies one or more labels.

  Parameters: | label (string or sequence) – or more labels that will be applied by the decorator to the functions (One) –

  it decorates. Labels are attributes of the decorated function with their value set to True.

  > ds : string An optional docstring for the resulting decorator. If not given, a default docstring is auto-generated.

  Returns: |
  Return type: | A decorator.

  Examples

  A simple labeling decorator:

      >>> slow = make_label_dec('slow')
      >>> slow.__doc__
      "Labels a test as 'slow'."

  And one that uses multiple labels and a custom docstring:

      >>> rare = make_label_dec(['slow','hard'],
      ... "Mix labels 'slow' and 'hard' for rare tests.")
      >>> rare.__doc__
      "Mix labels 'slow' and 'hard' for rare tests."

  Now, let’s test using this one: >>> @rare ... def f(): pass ... >>> >>> f.slow True >>> f.hard True

`IPython.testing.decorators.` `skipif`(skip_condition, msg=None)

  Make function raise SkipTest exception if skip_condition is true

  Parameters: | skip_condition (bool or callable) – Flag to determine whether to skip test. If the condition is a callable, it is used at runtime to dynamically make the decision. This is useful for tests that may require costly imports, to delay the cost until the test suite is actually executed. msg (string) – Message to give on raising a SkipTest exception.
  Returns: | decorator – Decorator, which, when applied to a function, causes SkipTest to be raised when the skip_condition was True, and the function to be called normally otherwise.
  Return type: | function

  Notes

  You will see from the code that we had to further decorate the decorator with the nose.tools.make_decorator function in order to transmit function name, and various other metadata.

`IPython.testing.decorators.` `skip`(msg=None)

  Decorator factory - mark a test function for skipping from test suite.

  Parameters: | msg (string) – Optional message to be added.
  Returns: | decorator – Decorator, which, when applied to a function, causes SkipTest to be raised, with the optional message added.
  Return type: | function

`IPython.testing.decorators.` `onlyif`(condition, msg)

  The reverse from skipif, see skipif for details.

`IPython.testing.decorators.` `module_not_available`(module)

  Can module be imported? Returns true if module does NOT import.

  This is used to make a decorator to skip tests that require module to be available, but delay the ‘import numpy’ to test execution time.

`IPython.testing.decorators.` `decorated_dummy`(dec, name)

  Return a dummy function decorated with dec, with the given name.

  Examples

  import IPython.testing.decorators as dec setup = dec.decorated_dummy(dec.skip_if_no_x11, __name__)

`IPython.testing.decorators.` `skip_file_no_x11`(name)

`IPython.testing.decorators.` `onlyif_cmds_exist`(*commands)

  Decorator to skip test when at least one of `commands` is not found.

`IPython.testing.decorators.` `onlyif_any_cmd_exists`(*commands)

  Decorator to skip test unless at least one of `commands` is found.