run_test¶
-
plasmapy.utils.pytest_helpers.run_test(func, args: Any = (), kwargs: Dict = {}, expected_outcome: Any = None, rtol: float = 0.0, atol: float = 0.0)¶ Test that a function or class returns the expected result, raises the expected exception, or issues an expected warning for the supplied positional and keyword arguments.
Parameters: - func (callable, list, or tuple) – The
callableto be tested. The first (and sole) argument torun_testmay alternatively be a list or tuple containing these arguments (optionally omittingkwargsif thelenreturns 3). - args (tuple or object) – The positional arguments to
func. - kwargs (dict) – The keyword arguments to
func. - expected_outcome (object) – The expected result, exception, or warning from
func(*args, **kwargs). This may also be atupleof length two that contains the expected result as the first item and the expected warning as the second item. - rtol (float) – The relative tolerance to be used by
allclosein an element-wise comparison, defaulting to0. - atol (float) – The absolute tolerance to be used by
allclosein an element-wise comparison, defaulting to0.
Returns: Return type: Raises: UnexpectedResultError– If the test returns a result that is different from the expected result.InconsistentTypeError– If the actual result is of a different type than the expected result.UnexpectedExceptionError– If an exception occurs when no exception or a different exception is expected.MissingExceptionError– If no exception is raised when an exception is expected.MissingWarningError– An expected warning is not issued.UnitsError– If the result has different units than expected.TypeError– If the equality of the actual result and expected result cannot be determined (e.g., for a class lacking an__eq__method.
Examples
The simplest way to use
run_testis with inputs for the function to be tests, the positional arguments in atupleorlist, the keyword arguments in adict, and then finally the expected result or outcome.>>> args = tuple() >>> kwargs = dict() >>> run_test(lambda: 0, args, kwargs, 0)
If
expectedis a an exception or warning, thenrun_testwill raise an exception if the expected exception is not raised or the expected warning is not issued.>>> from warnings import warn
>>> issue_warning = lambda: warn("Electrons are weird!", UserWarning) >>> run_test(issue_warning, args, kwargs, UserWarning)
>>> def raise_exception(): raise RuntimeError >>> run_test(raise_exception, args, kwargs, RuntimeError)
For warnings,
run_testcan accept atupleof two items where the first item is the expected result and the second item is the expected warning.def return_arg_and_warn(x): warn("", UserWarning) return x run_test(return_arg_and_warn, 1, {}, (1, UserWarning))
This function is also flexible enough that it can accept a
tupleorlistas its sole argument, with the arguments in the same order as in the function signature.>>> return_arg = lambda x: x >>> inputs = (return_arg, 42, {}, 42) >>> run_test(inputs)
If the
tupleorlisthas a length of3, thenrun_testassumes thatkwargsis missing.>>> inputs_without_kwargs = [return_arg, 42, 42] >>> run_test(inputs_without_kwargs)
import pytest def func(x, raise_exception=False, issue_warning=False): if raise_exception: raise ValueError("I'm sorry, Dave. I'm afraid I can't do that.") elif issue_warning: warn("Open the pod bay doors, HAL.", UserWarning) return x inputs_table = [ (func, 1, 1), (func, (2,), {}, 2), (func, 3, {'raise_exception': True}, ValueError), (func, 4, {'issue_warning': True}, UserWarning), (func, 5, {'issue_warning': True}, (5, UserWarning)), ] @pytest.mark.parametrize('inputs', inputs_table) def test_func(inputs): run_test(inputs)
- func (callable, list, or tuple) – The