test_helper_funcs

This module provides functions to help users easily create and run unit tests for their Python software.

Module Contents

Functions

make_tuple_str

Creates a more readable string representation of a tuple.

compare_output_tuples

Checks whether two tuples agree with each other based on a specified comparison type (“==”, “<”, “>”, etc.).

run_single_test

Runs a single unit test for a given function.

run_func_tests

Runs a set of unit tests for a specified function.

clean_input_tuple_list

Turns a list of function inputs into a list of tuples (the format expected by many of the testing functions). Caution: This function will turn each element of the input list into its own tuple, which might not be the behaviour that you desire. In order to prevent this type of parsing error, please consider adding the appropriate tuple parentheses to your inputs before using this function.

make_io_pairs_from_input_list

Makes a list of IOPairs objects from a list of input tuples that should all yield the same expected output.

test_bool_func

Runs a sequence of tests for a function that returns a boolean value.

Data

ASSERT_EQUAL

str: Constant string label for the assertion type that checks whether test outputs are equal to the expected outputs.

ASSERT_LESS

str: Constant string label for the assertion type that checks whether test outputs are less than expected output values.

ASSERT_LESS_OR_EQUAL

str: Constant string label for the assertion type that checks whether test outputs are less than or equal to the expected outputs.

ASSERT_GREATER

str: Constant string label for the assertion type that checks whether test outputs are greater than the expected output values.

ASSERT_GREATER_OR_EQUAL

str: Constant string label for the assertion type that checks whether test outputs are greater than or equal to the expected outputs.

ASSERT_RAISES

str: Constant string label for the assertion type that checks whether the test function raises an Exception.

ASSERT_TYPE

str: Constant string label for the assertion type that checks whether test outputs have the expected data types.

ASSERT_TYPES

list: List of all the assertion types that are currently supported in this module.

API

test_helper_funcs.ASSERT_EQUAL = 'assert_equal'

str: Constant string label for the assertion type that checks whether test outputs are equal to the expected outputs.

test_helper_funcs.ASSERT_LESS = 'assert_less_than'

str: Constant string label for the assertion type that checks whether test outputs are less than expected output values.

test_helper_funcs.ASSERT_LESS_OR_EQUAL = 'assert_less_or_equal'

str: Constant string label for the assertion type that checks whether test outputs are less than or equal to the expected outputs.

test_helper_funcs.ASSERT_GREATER = 'assert_greater_than'

str: Constant string label for the assertion type that checks whether test outputs are greater than the expected output values.

test_helper_funcs.ASSERT_GREATER_OR_EQUAL = 'assert_greater_or_equal'

str: Constant string label for the assertion type that checks whether test outputs are greater than or equal to the expected outputs.

test_helper_funcs.ASSERT_RAISES = 'assert_raises'

str: Constant string label for the assertion type that checks whether the test function raises an Exception.

test_helper_funcs.ASSERT_TYPE = 'assert_output_is_type'

str: Constant string label for the assertion type that checks whether test outputs have the expected data types.

test_helper_funcs.ASSERT_TYPES = None

list: List of all the assertion types that are currently supported in this module.

test_helper_funcs.make_tuple_str(input_tuple)

Creates a more readable string representation of a tuple.

Parameters

input_tupletuple | any

The tuple that we must convert into a string. (If you do not pass a tuple for this parameter, this function will create the appropriate tuple for your input before generating the string output.)

Returns

str

The string representation of the tuple.

test_helper_funcs.compare_output_tuples(output_tuple1, output_tuple2, compare_type=ASSERT_EQUAL)

Checks whether two tuples agree with each other based on a specified comparison type (“==”, “<”, “>”, etc.).

Parameters

output_tuple1tuple | any

The first tuple in the comparison. If it is not passed as a tuple, this function will convert it to a tuple with a single element.

output_tuple2tuple | any

The second tuple in the comparison. If it is not passed as a tuple, this function will convert it to a tuple with a single element.

compare_typestr | list[str], optional, default ASSERT_EQUAL

The name of the comparison type that should be used (must be a value from the ASSERT_TYPES list at the start of this file (test_helper_funcs.py)). If you want your outputs to be checked with different assertion types, you can instead provide a list of comparison methods. For example, if you have two integer outputs and you want to check if one is greater than a certain value and if the other is less than a different value, then you can provide the list [ASSERT_GREATER, ASSERT_LESS] for this parameter. If your list is shorter than the number of outputs, the last comparison type in the list will be used for the remaining output values.

Returns

bool

Returns True if the tuples match based on the comparison type and False if they do not.

test_helper_funcs.run_single_test(test_func, test_input=(), expected_output=(), assert_type=ASSERT_EQUAL, test_desc='', raise_error_on_fail=True, add_new_line=True, include_input_in_error_msg=True)

Runs a single unit test for a given function.

Parameters

test_funcfunction

The function that should be tested.

test_inputtuple, optional, default=()

The input tuple that will be passed to the test function.

expected_outputtuple, optional, default=()

The expected output that should be returned by the test function.

assert_typestr | list[str], optional, default ASSERT_EQUAL

The name of the assertion type that should be used for checking whether a test was successful (must be a value from the ASSERT_TYPES list at the start of this file (test_helper_funcs.py)). If you want your outputs to be checked with different assertion types, you can instead provide a list of comparison methods. For example, if you have two integer outputs and you want to check if one is greater than a certain value and if the other is less than a different value, then you can provide the list [ASSERT_GREATER, ASSERT_LESS] for this parameter. If your list is shorter than the number of outputs, the last comparison type in the list will be used for the remaining output values.

test_descstr, optional, default=””

A description of the test that should be printed to stdout.

raise_error_on_failbool, optional, default=True

A boolean flag indicating whether an AssertionError should be raised if the test fails.

add_new_linebool, optional, default=True

Boolean flag indicating whether we should add a blank line after we finish printing the test results to stdout (useful for readability).

include_input_in_error_msgbool, optional, default=True

Boolean flag indicating whether we should include the test’s input in the error message that is displayed if the test fails. Consider setting this parameter to False if your test description already includes the test input.

Returns

bool

A boolean flag indicating whether the test was successful.

Raises
AssertionError

Raised if the test fails and the raise_error_on_fail flag is set to True.

test_helper_funcs.run_func_tests(test_func, correct_io_pairs, assert_type=ASSERT_EQUAL, test_desc='', raise_error_on_fail=True)

Runs a set of unit tests for a specified function.

Parameters

test_funcfunction

The function that should be tested.

correct_io_pairslist[IOPair]

List of the input-output pairs that should occur if the test function is working properly. The number of input-output pairs determines the number of tests that will be run. For each input-output pair, this function will pass the input to the function that we are testing. It will then use the requested assertion function to check whether the actual output matches the expected output from the input-output pair.

assert_typestr | list[str], default ASSERT_EQUAL

The name of the assertion type that should be used for checking whether a test was successful (must be a value from the ASSERT_TYPES list at the start of this file (test_helper_funcs.py)). If you want your outputs to be checked with different assertion types, you can instead provide a list of comparison methods. For example, if you have two integer outputs and you want to check if one is greater than a certain value and if the other is less than another value, then you can provide the list [ASSERT_GREATER, ASSERT_LESS] for this parameter. If your list is shorter than the number of outputs, the last comparison type in the list will be used for the remaining output values.

test_descstr, optional, default=””,

A description of the tests that should be printed to stdout.

raise_error_on_failbool, optional, default=True

A boolean flag indicating whether an AssertionError should be raised if a test fails.

Returns

all_tests_succeededbool

A boolean flag indicating whether all of the tests were successful.

Raises
AssertionError

Raised if any of the tests fail and raise_error_on_fail is set to True.

test_helper_funcs.clean_input_tuple_list(input_list)

Turns a list of function inputs into a list of tuples (the format expected by many of the testing functions). Caution: This function will turn each element of the input list into its own tuple, which might not be the behaviour that you desire. In order to prevent this type of parsing error, please consider adding the appropriate tuple parentheses to your inputs before using this function.

Parameters

input_listlist

The input list

Returns

resultlist

The correctly formatted list of input tuples.

test_helper_funcs.make_io_pairs_from_input_list(input_list, expected_output)

Makes a list of IOPairs objects from a list of input tuples that should all yield the same expected output.

Parameters

input_listlist

The list of input tuples.

expected_outputtuple | any

The expected output tuple that should occur for all of the input tuples in input_list. If the user does not pass a tuple for this parameter, the function will convert the provided output value into a tuple with a single element.

Returns

io_pairslist[IOPair]

The list of IOPairs generated from the input tuple list and the expected output.

test_helper_funcs.test_bool_func(test_func, true_inputs=None, false_inputs=None, test_desc='', error_if_false=False, error_if_false_type=Exception, raise_error_on_fail=True, type_error_inputs=None, value_error_inputs=None, assert_error_inputs=None)

Runs a sequence of tests for a function that returns a boolean value.

Parameters

test_funcfunction

The boolean function that should be tested.

true_inputslist[tuple] | list[any], optional, default=None

List of input tuples that should cause the test function to return True.

false_inputslist[tuple] | list[any], optional, default=None

List of input tuples that should cause the test function to return False.

test_descstr, optional, default=””

A description of the tests that should be printed to stdout.

error_if_falsebool, optional, default=False

Boolean flag for whether the function should raise an error when the condition it evaluates is False.

error_if_false_typetype, optional, default=Exception

The type of exception that should be raised for False results (if error_if_false is True).

raise_error_on_failbool, optional, default=True

A boolean flag indicating whether an AssertionError should be raised if a test fails.

type_error_inputslist[tuple] | list[any], optional, default=None

List of input tuples that should cause the test function to raise a TypeError.

value_error_inputslist[tuple] | list[any], optional, default=None

List of input tuples that should cause the test function to raise a ValueError.

assert_error_inputslist[tuple] | list[any], optional, default=None

List of input tuples that should cause the test function to raise an AssertionError.

Returns

bool

Returns True if all tests were successful, False otherwise.

Raises
AssertionError

Raised if raise_error_on_fail is True and any of the tests fail (stdout messages allow the user to easily determine which test case failed).