Module Contents¶
The public interface of the unittest_expander
module consists of
the following functions, classes, objects and constants.
(See: Narrative Documentation – for a much richer description of most of them, including a lot of usage examples…)
The expand()
class decorator¶
-
@
expand
¶ Apply this decorator to a test class to generate actual parametrized test methods, i.e., to “expand” parameter collections which have been bound (by applying
foreach()
) to original test methods.―
The public interface of
expand()
includes also the attributes described below.Two of them make it possible to customize how names of parametrized test methods are generated:
-
expand.
global_name_formatter
¶ Type: string.Formatter
-like object orNone
Value: None
(use the default formatter)
Other two allow – respectively – to switch the context ordering style, and to decide whether a certain deprecated, signature-introspection-based feature shall be enabled:
-
The foreach()
method decorator¶
-
@
foreach
(param_collection)¶
or
-
@
foreach
(*param_collection_items, **param_collection_labeled_items) Call this function, specifying parameter collections to be bound to a test method, and then apply the resultant decorator to that method (only then it will be possible – by applying
expand()
to the test class owning the method – to generate actual parametrized test methods).To learn more about what needs to be passed to
foreach()
, see the description (below) of theparamseq
’s constructor (note that the call signatures offoreach()
and that constructor are the same).
The paramseq
class¶
-
class
paramseq
(param_collection)¶ param_collection must be a parameter collection – that is, one of:
a
paramseq
instance,a sequence not being a
tuple
/str
/unicode
/bytes
/bytearray
(in other words, such an object for whomisinstance(obj, collections.abc.Sequence) and not isinstance(obj, (tuple, str, bytes, bytearray))
returnsTrue
in Python 3) – for example, alist
,a mapping (i.e., such an object that
isinstance(obj, collections.abc.Mapping)
returnsTrue
in Python 3) – for example, adict
,a set (i.e., such an object that
isinstance(obj, collections.abc.Set)
returnsTrue
in Python 3) – for example, aset
orfrozenset
,a callable (i.e., such an object that
callable(obj)
returnsTrue
) which is supposed to:- accept one positional argument (the test class) or no arguments at all,
- return an iterable object (i.e., an object that could be
used as a
for
loop’s subject, able to yield consecutive items)
– for example, a generator function.
Each item of a parameter collection is supposed to be:
or
-
class
paramseq
(*param_collection_items, **param_collection_labeled_items) The total number of given arguments (positional and/or keyword ones) must be greater than 1. Each argument will be treated as a parameter collection’s item (see above); for each keyword argument (if any), its name will be used to
label()
the item it refers to.―
A
paramseq
instance is the canonical form of a parameter collection.Its public interface includes the following methods:
-
__add__
(param_collection)¶ Returns a new
paramseq
instance – being a result of concatenation of theparamseq
instance we operate on and the given param_collection (see the above description of theparamseq
constructor’s argument param_collection…).
-
__radd__
(param_collection)¶ Returns a new
paramseq
instance – being a result of concatenation of the given param_collection (see the above description of theparamseq
constructor’s argument param_collection…) and theparamseq
instance we operate on.
-
context
(context_manager_factory, *its_args, **its_kwargs, _enable_exc_suppress_=False)¶ Returns a new
paramseq
instance containing clones of the items of the instance we operate on – each cloned with aparam.context()
call (see below…) to which all given arguments are passed.
-
The param
class¶
-
class
param
(*args, **kwargs)¶ args and kwargs specify actual (positional and keyword) arguments to be passed to test method call(s).
―
A
param
instance is the canonical form of a parameter collection’s item. It represents a single combination of test parameter values.Its public interface includes the following methods:
-
context
(context_manager_factory, *its_args, **its_kwargs, _enable_exc_suppress_=False)¶ Returns a new
param
instance being a clone of the the instance we operate on, with the specified context manager factory (and its arguments) attached.By default, the possibility to suppress exceptions by returning a true value from context manager’s
__exit__()
is disabled (exceptions are propagated even if__exit__()
returnsTrue
); to enable this possibility you need to set the _enable_exc_suppress_ keyword argument toTrue
.
-
The current
special object¶
-
current
¶ A special singleton object which, when used during execution of a parametrized test method, provides access (in a thread-local manner) to the following properties of the (currently executed) test:
-
current.
label
¶ Type: str
Value: the test’s label (which was automatically generated or explicitly specified with param.label()
…)
-
current.
context_targets
¶ Type: Sequence
Value: the test contexts’ as-targets (i.e., objects returned by __enter__()
of each of the context managers which were specified withparam.context()
…)
-
current.
all_args
¶ Type: Sequence
Value: all positional arguments obtained by the currently executed parametrized test method (in particular, including all positional arguments which were passed to the param
constructor…)
-
current.
all_kwargs
¶ Type: Mapping
Value: all keyword arguments obtained by the currently executed parametrized test method (in particular, including all keyword arguments which were passed to the param
constructor…)
-
Non-essential constants and classes¶
-
__version__
¶ The version of
unittest_expander
as astr
being a PEP 440-compliant identifier.
-
class
Substitute
(actual_object)¶ A kind of attribute-access-proxying wrapper, automatically applied by the machinery of
expand()
to each test method previously decorated withforeach()
.The sole constructor argument (actual_object) is the object (typically, a test method) to be proxied.
Apart from exposing in a transparent way nearly all attributes of the proxied object, the public interface of a
Substitute
includes the following instance attribute:-
actual_object
¶ The proxied object itself (unwrapped).
Note
A
Substitute
instance is never callable – even though (typically) the proxied object is.-