Module dalpy.test_utils
This module provides utilities related to unit testing.
This module contains the build_and_run_watched_suite()
, assert_array_equals()
, behavior_test()
, generic_test()
,
dalpy_equals()
and the dalpy_to_string()
functions, as well as UnexpectedReturnWarning
.
Functions
def assert_array_equals(expected, actual, msg=None)
def behavior_test(behavior, objects)
-
Test the behavior of an object.
Args
behavior
- a
list
oftuple
s of the form(RESULT, METHOD, PARAMETERS)
. objects
- a
list
of objects who's parameters are being called.
Raises
AssertionError
- If
METHOD(PARAMETERS) != RESULT
.
For each tuple in behavior this test asserts that
METHOD(PARAMETERS) = RESULT
.In each
tuple
METHOD
should an uncalledcallable
, for example:>>> stack = Stack() >>> uncalled_callable = stack.pop
Notes: - If
METHOD
requires multiple parameters, thenPARAMETERS
can be passed as atuple
. - IfMETHOD
has no required return, thenRESULT
can be omitted in favor of(METHOD, PARAMETERS)
. - IfMETHOD
has no parameters, thenPARAMETERS
can be omitted in favor of(RESULT, METHOD)
.Example:
>>> stack = Stack() >>> behavior = [ (stack.push, 1), (1, stack.pop) ]
The objects parameter is the object who's behavior is being tested, which will be used for the test log. If multiple objects are being tested, pass a tuple of objects.
def build_and_run_watched_suite(cases, timeout=None, show_tb=False, grading_file=None, warning_filter='once')
-
Runs a set of test cases, ensuring that they do not run longer than
timeout
seconds. Optionally, writes comma-separated test results to a file.Args
cases
- A list of TestCases to be run.
timeout
- Number of seconds to allow each test case to run for.
show_tb
- Boolean toggle for stack trace.
grading_file
- Output file path to store comma-separated test results.
warning_filter
- A
warnings.simplefilter
action. Default value ensures that warnings are only displayed once. Choose"ignore"
to suppress warnings.
If
grading_file
is not specified, the test logs will be dumped to console. def dalpy_equals(first, second)
-
Tests equality between two objects. If the objects are from the DALPy, they are compared using their own custom comparator.
dalpy_equals()
supports equality for the following objects:Array
,Array2D
,Queue
,Stack
,Set
,SinglyLinkedListNode
. ForSinglyLinkedListNode
, checks that all nodes next of the passedSinglyLinkedListNode
s are the same. For instances offloat
s,math.isclose
is used for comparison.Args
first
- The first element to be tested.
second
- The second element to be tested
Returns
True
iffirst = second
otherwiseFalse
. def dalpy_to_string(obj)
-
Generates a string representation of a DALPy object if passed object is from DALPy, otherwise calls native str method.
dalpy_to_string supports the following objects:
Array
,Array2D
,Queue
,Stack
,Set
,SinglyLinkedListNode
,BinaryTreeNode
,NaryTreeNode
,Vertex
, andGraph
.Calling dalpy_to_string on
BinaryTreeNode
orNaryTreeNode
displays the entire tree rooted at that node prepended with"BinaryTree"
and"NaryTree"
respectively. This is done to clarify the nodes themselves are not holding the data listed after it.Returns
string representation of
obj
.Args
obj
- The object to convert to string
def generic_test(params, expected, method, custom_comparator=None, in_place=False, enforce_no_mod=False, params_to_string=None, expected_to_string=None, output_to_string=None)
-
Test the output of a function.
Args
params
- Parameters to be passed into the function being tested. This argument can either be a single parameter, or a list of parameters.
expected
- Expected return value of tested function with parameters specified by params. If
expected
is anException
, the test will assert that the function tested on the given parameters throws the expectedException
. method
- Function being tested. Must be a
callable
. custom_comparator
- Function for determining if method output equals expected. Must be a
callable
. DefaultNone
which means thatdalpy_equals()
will be used. in_place
True
ifexpected
should be compared againstparams
. By default this isFalse
.enforce_no_mod
bool
or alist
ofbool
indicating which args should not be modified. DefaultFalse
allows modification of all args.params_to_string
- Function for displaying the parameters. Must be a
callable
. DefaultNone
which means thatdalpy_to_string()
will be used instead. expected_to_string
- Function for displaying the expected output. Must be a
callable
. DefaultNone
which means thatdalpy_to_string()
will be used instead. output_to_string
- Function for displaying the actual output. Must be a
callable
. DefaultNone
which means thatdalpy_to_string()
will be used instead.
Raises
AssertionError
- If the test fails.
UnexpectedReturnWarning
- If
in_place
is set toTrue
butmethod
still returns a value.
def run_generic_test(params, expected, method, custom_comparator=None, in_place=False, enforce_no_mod=False, init_params=None, init_expected=None, params_to_string=None, expected_to_string=None, output_to_string=None)
-
Test the output of a function.
Warnings
Deprecated in 1.1.0, to be removed. Use the generic_test function instead.
Args
params
- Parameters to be passed into the function being tested. This argument can either be a single parameter, or a list of parameters.
expected
- Expected return value of tested function with parameters specified by params.
method
- Function being tested. Must be a
callable
. custom_comparator
- Function for determining if method output equals expected. Must be a
callable
. in_place
True
ifexpected
should be compared againstparams
.enforce_no_mod
bool
or alist
ofbool
indicating which args should not be modified. DefaultFalse
allows modification of all args.init_params
- Function for initializing parameters. Must be a
callable
. init_expected
- Function for initializing expected output. Must be a
callable
. params_to_string
- Function for displaying the parameters. Must be a
callable
. expected_to_string
- Function for displaying the expected output. Must be a
callable
. output_to_string
- Function for displaying the actual output. Must be a
callable
.
Raises
AssertionError
- If the test fails.
UnexpectedReturnWarning
- If
in_place
is set toTrue
butmethod
still returns a value. DeprecationWarning
- If used in version >= 1.1.0.
If
expected
is anException
, the test will assert that the function tested on the given parameters throws the expectedException
. If no customto_string
s are specified, thedalpy_to_string()
method will be used for displaying parameters, input and output.
Classes
class UnexpectedReturnWarning (*args, **kwargs)
-
A
Warning
subclass for instances where functions are expected to modify their arguments but return values instead.Ancestors
- builtins.Warning
- builtins.Exception
- builtins.BaseException