run_test_equivalent_calls¶
-
plasmapy.utils.pytest_helpers.pytest_helpers.
run_test_equivalent_calls
(*test_inputs, require_same_type: bool = True)¶ Test that different functions/inputs return equivalent results.
Parameters: - test_inputs – The functions and inputs to the tests in an allowed format, as described below.
- require_same_type (bool) – If
True
(the default), then all of the results are required to be of the same type. IfFalse
, results do not need to be of the same type (e.g., cases like1.0 == 1
will not raise an exception).
Raises: UnexpectedResultFail
– If not all of the results are equivalent, or not all of the results are of the same type andrequire_same_type
evaluates toTrue
.UnexpectedExceptionFail
– If an exception is raised whilst attempting to run one of the test cases.InvalidTestError
– If there is an error associated with the inputs or the test is set up incorrectly.
Examples
There are several possible formats that can be accepted by this
run_test_equivalent_calls
to test that different combinations of functions (or othercallable
objects), positional arguments, and keyword arguments return equivalent results.To test a single function that takes a single positional argument, then
test_inputs
may be the function followed by an arbitrary number of positional arguments to be included into the function.>>> def f(x): return x ** 2 >>> run_test_equivalent_calls(f, -1, 1)
To test a single function with an arbitrary number of positional and keyword arguments, the first argument should be the function, followed by an arbitrary number of
tuple
orlist
objects that contain atuple
orlist
containing the positional arguments, and adict
containing the keyword arguments.>>> def g(x, y, z): return x + y + z >>> run_test_equivalent_calls(g, ((1, 2, 3), {}), ((3, 2), {'z': 1}))
If there is only one positional argument, then it is not necessary to include it in a
tuple
orlist
.>>> run_test_equivalent_calls(f, ([1], {}), ([1], {})) >>> run_test_equivalent_calls(f, (1, {}), (1, {}))
To test multiple functions with an arbitrary number of positional and keyword arguments, use a series of
tuple
orlist
objects that contain the function for each test, atuple
orlist
with the positional arguments, and adict
with the keyword arguments.>>> def p(x, y=None): return x + y if y else x >>> def q(x, y=None): return x + 1 if y else x
>>> run_test_equivalent_calls([p, (1,), {'y': 1}], [q, (2,), {'y': False}])
The inputs may also be passed in as a whole as a
tuple
orlist
.>>> run_test_equivalent_calls(f, -1, 1) >>> run_test_equivalent_calls([f, -1, 1])
If
require_same_type
isFalse
, then an exception will not be raised if the results are of different types.>>> run_test_equivalent_calls(f, -1, 1.0, require_same_type=False)