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
listoftuples of the form(RESULT, METHOD, PARAMETERS). objects- a
listof 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
tupleMETHODshould an uncalledcallable, for example:>>> stack = Stack() >>> uncalled_callable = stack.popNotes: - If
METHODrequires multiple parameters, thenPARAMETERScan be passed as atuple. - IfMETHODhas no required return, thenRESULTcan be omitted in favor of(METHOD, PARAMETERS). - IfMETHODhas no parameters, thenPARAMETERScan 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
timeoutseconds. 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.simplefilteraction. Default value ensures that warnings are only displayed once. Choose"ignore"to suppress warnings.
If
grading_fileis 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 passedSinglyLinkedListNodes are the same. For instances offloats,math.iscloseis used for comparison.Args
first- The first element to be tested.
second- The second element to be tested
Returns
Trueiffirst = secondotherwiseFalse. 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
BinaryTreeNodeorNaryTreeNodedisplays 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
expectedis 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. DefaultNonewhich means thatdalpy_equals()will be used. in_placeTrueifexpectedshould be compared againstparams. By default this isFalse.enforce_no_modboolor alistofboolindicating which args should not be modified. DefaultFalseallows modification of all args.params_to_string- Function for displaying the parameters. Must be a
callable. DefaultNonewhich means thatdalpy_to_string()will be used instead. expected_to_string- Function for displaying the expected output. Must be a
callable. DefaultNonewhich means thatdalpy_to_string()will be used instead. output_to_string- Function for displaying the actual output. Must be a
callable. DefaultNonewhich means thatdalpy_to_string()will be used instead.
Raises
AssertionError- If the test fails.
UnexpectedReturnWarning- If
in_placeis set toTruebutmethodstill 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_placeTrueifexpectedshould be compared againstparams.enforce_no_modboolor alistofboolindicating which args should not be modified. DefaultFalseallows 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_placeis set toTruebutmethodstill returns a value. DeprecationWarning- If used in version >= 1.1.0.
If
expectedis anException, the test will assert that the function tested on the given parameters throws the expectedException. If no customto_strings are specified, thedalpy_to_string()method will be used for displaying parameters, input and output.
Classes
class UnexpectedReturnWarning (*args, **kwargs)-
A
Warningsubclass for instances where functions are expected to modify their arguments but return values instead.Ancestors
- builtins.Warning
- builtins.Exception
- builtins.BaseException