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)

Asserts that two Arrays are equal, displaying a custom message if specified.

Args

expected
The expected Array.
actual
The actual Array.
msg
The message to display on AssertionError, if not specified, then a default message is displayed.

Raises

AssertionError
If expected != actual.
def behavior_test(behavior, objects)

Test the behavior of an object.

Args

behavior
a list of tuples 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 uncalled callable, for example:

>>> stack = Stack()
>>> uncalled_callable = stack.pop

Notes: - If METHOD requires multiple parameters, then PARAMETERS can be passed as a tuple. - If METHOD has no required return, then RESULT can be omitted in favor of (METHOD, PARAMETERS). - If METHOD has no parameters, then PARAMETERS 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. For SinglyLinkedListNode, checks that all nodes next of the passed SinglyLinkedListNodes are the same. For instances of floats, math.isclose is used for comparison.

Args

first
The first element to be tested.
second
The second element to be tested

Returns

True if first = second otherwise False.

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, and Graph.

Calling dalpy_to_string on BinaryTreeNode or NaryTreeNode 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 an Exception, the test will assert that the function tested on the given parameters throws the expected Exception.
method
Function being tested. Must be a callable.
custom_comparator
Function for determining if method output equals expected. Must be a callable. Default None which means that dalpy_equals() will be used.
in_place
True if expected should be compared against params. By default this is False.
enforce_no_mod
bool or a list of bool indicating which args should not be modified. Default False allows modification of all args.
params_to_string
Function for displaying the parameters. Must be a callable. Default None which means that dalpy_to_string() will be used instead.
expected_to_string
Function for displaying the expected output. Must be a callable. Default None which means that dalpy_to_string() will be used instead.
output_to_string
Function for displaying the actual output. Must be a callable. Default None which means that dalpy_to_string() will be used instead.

Raises

AssertionError
If the test fails.
UnexpectedReturnWarning
If in_place is set to True but method 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 if expected should be compared against params.
enforce_no_mod
bool or a list of bool indicating which args should not be modified. Default False 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 to True but method still returns a value.
DeprecationWarning
If used in version >= 1.1.0.

If expected is an Exception, the test will assert that the function tested on the given parameters throws the expected Exception. If no custom to_strings are specified, the dalpy_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