run_test_equivalent_calls¶
-
plasmapy.utils.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 == 1will not raise an exception).
Raises: UnexpectedResultError– If not all of the results are equivalent, or not all of the results are of the same type andrequire_same_typeevaluates toTrue.UnexpectedExceptionError– 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_callsto test that different combinations of functions (or othercallableobjects), positional arguments, and keyword arguments return equivalent results.To test a single function that takes a single positional argument, then
test_inputsmay 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
tupleorlistobjects that contain atupleorlistcontaining the positional arguments, and adictcontaining 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
tupleorlist.>>> 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
tupleorlistobjects that contain the function for each test, atupleorlistwith the positional arguments, and adictwith 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
tupleorlist.>>> run_test_equivalent_calls(f, -1, 1) >>> run_test_equivalent_calls([f, -1, 1])
If
require_same_typeisFalse, 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)