Skip to main content

A Python package that simplifies the creation, execution, and analysis of unit tests.

Project description

test-helper-by-delica README

Purpose

This package simplifies the creation, execution, and analysis of unit tests in Python, in order to help programmers more efficiently develop and validate their software.

Installation

This package is available through the Python Package Index (PyPI).
One can easily download the package with the following pip install statement:
pip install test_helper_by_delica

If you are a contributor who needs to test changes from the development branch,
you can install the test version of the library from TestPyPI with the following command:
pip install --index-url https://test.pypi.org/simple/test_helper_by_delica

If you are not a contributor to this project, please only use the production version
that is deployed on PyPI and can be downloaded with pip install test_helper_by_delica.
The production version is the most stable release of the test helper package.

Getting Started

After installation, all we need to start using the package is a function that we want to test.
For a simple example, we can define a boolean function that always returns True.

def always_true():    
    return True   

Now we can call the boolean testing function from the package to check whether always_true returns the expected result.

import test_helper_by_delica 
test_helper_by_delica.test_bool_func(always_true, true_inputs=[()], test_desc="always true function")

This function call generates the following output:

TESTING ALWAYS TRUE FUNCTION
Test #1 of 1
Testing always_true function for input ()
SUCCESS: input=() -> output=True
ALL 1 TESTS COMPLETED FOR ALWAYS TRUE FUNCTION

Similarly, we can test a function that always returns False.

def always_false():    
    return False
    
test_helper_by_delica.test_bool_func(always_false, false_inputs=[()], test_desc="always false function")

We then have the following output:

TESTING ALWAYS FALSE FUNCTION
Test #1 of 1
Testing always_false function for input ()
SUCCESS: input=() -> output=False
ALL 1 TESTS COMPLETED FOR ALWAYS FALSE FUNCTION

To see what happens if a test fails, we can try using the always_false function in our always_true test.

test_helper_by_delica.test_bool_func(test_obj, always_false, true_inputs=[()], test_desc="always true function")

This call prints the following stdout output to inform the user that the test has failed:

TESTING ALWAYS TRUE FUNCTION
Test #1 of 1
Testing always_false function for input ()
FAILURE: ALWAYS_FALSE FUNCTION FOR INPUT () FAILED WITH INPUT = () EXPECTED_OUTPUT = True, ACTUAL_OUTPUT = False
ALL 1 TESTS COMPLETED FOR ALWAYS TRUE FUNCTION

Now suppose we want to test a boolean function that takes an input parameter. For example, let's use the is_int
function below, which returns True if the input parameter is an integer and False if it is not.

def is_int(val):
    return isinstance(val, int)

For this function, we need to specify values that should yield True and values that should yield False. The test helper package expects the input parameters for each individual test (e.g, testing whether the number 2 yields True) to be provided in the form of a tuple. If your input or output tuple has only one element, you can pass the object on its own without adding parentheses (e.g., can pass 1 instead of (1,) for an input tuple), because the test helper package will take care of converting your single input or return parameter to a tuple format.

test_helper_by_delica.test_bool_func(is_int, true_inputs=[1,2], false_inputs=[1.0,"2"], test_desc="is_int function")

If we have a function with two parameters, such as the sum function below, we need to pass the inputs as tuples (e.g., (1, 2), (0, 0), (3, 2), etc.). If we do not include the parentheses, the test helper function cannot accurately determine the number of input parameters that the test function should receive.

def calc_sum(int1, int2):
    return int1 + int2
test_helper_by_delica.run_func_tests(calc_sum, [IOPair((1, 2), 3), IOPair((0, 0), 0), IOPair((3, 2), 5)], 
test_desc="sum calculator function")

In the above example, we use run_func_tests from the test helper package instead of test_bool_func because the sum function does not return a boolean value. The more general run_func_tests function can test whether any type of return value is correct based on user-provided criteria. If a user wants to verify whether their test function raises an exception, they simply need to set the expected test output to the correct exception type (e.g., TypeError, ValueError, etc.), as demonstrated below.

def is_int_error_if_false(val):
    is_int_val = isinstance(val, int)
    if not is_int_val:
        raise TypeError("val is not an integer")
    return is_int_val
    
test_helper_by_delica.run_func_tests(is_int_error_if_false, [IOPair(1, True), IOPair(1.0, TypeError), IOPair((3, 2), 5)], 
               test_desc="is integer error if false function")

The main difference between run_func_tests and test_bool_func, besides the types of functions that they can validate, is that run_func_tests requires a list of IOPair objects to specify the expected input-output pairs for the tests. For example, if our input numbers are 1 and 2 and we expect the sum value to be 3, we will pass this information to run_func_tests in the form IOPair((1, 2), 3). If we instead test a function that returns two numbers which equal an input parameter when added together, we could have a test with IOPair(3, (1, 2)). The test_bool_func function does not require IOPair objects to define expected outputs, because there are only two possible outcomes for boolean functions. The test helper package class includes the IOPair class to facilitate cases in which we need to test a wide range of possible output types.

The Usage section below provides additional examples of how you can test your own functions with the test helper package.

Usage

This package supports three different methods for testing Python code. If you only need to run one test, use the run_single_test function, as demonstrated below with a test function that returns the sum of two numbers.

def calc_sum(int1, int2):
    return int1 + int2

test_helper_by_delica.run_single_test(calc_sum, test_input=(1, 2), expected_output=3, test_desc="Sum Test 1")

If you need to run multiple tests, use run_func_tests to check how your function responds to multiple input scenarios. For each test case, provide an IOPair object that contains the input for the test and the expected output. The code segment below provides an example of how to format these IOPair objects for our sum calculator function.

test_helper_by_delica.run_func_tests(calc_sum, [IOPair((1, 2), 3), IOPair((2, 2), 4)], test_desc="Sum Test Suite")

Although the above example only uses integers, the IOPair class accepts any data types for test inputs and outputs. The package currently does not support file-based output checking (e.g., checking if a function creates a file or that the contents of an output file are correct). However, file IO will be included in a future version.

If your function returns a boolean output (True or False), you can skip the creation of IOPair objects by using the test_bool_func function in this package.

def is_pos(num):
    return num > 0
    
test_helper_by_delica.test_bool_func(is_pos, true_inputs=[1, 2, 0.1, 100], false_inputs=[-1, -0.1, -100, -1203], 
test_desc="Bool Function Tests")

Each of the three aforementioned functions (run_single_test, run_func_tests, and test_bool_func) can also verify that your functions throw errors in the correct circumstances. For example, suppose that we want our boolean function to throw a ValueError if the input is zero or negative.

def is_pos_with_error(num)
    result = num > 0
    if not result:
        raise ValueError()
    return result

test_helper_by_delica.test_bool_func(is_pos, true_inputs=[1, 2, 0.1, 100], false_inputs=[-1, -0.1, -100, -1203], 
test_desc="Bool Function Tests With Value Error", error_if_false=True, error_type=ValueError)

For additional information on how this package can accommodate your testing needs, please see the full documentation at https://delicalm.github.io/test_helper_by_delica/.

Citation

To reference this Python package, please use the following citation.

APA Format

Leboe-McGowan, D. S. (2026). Test helper by delica (Version 1.0.2) [Source code]. GitHub. https://github.com/DelicaLM/test_helper_by_delica

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

test_helper_by_delica-1.0.6.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

test_helper_by_delica-1.0.6-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file test_helper_by_delica-1.0.6.tar.gz.

File metadata

  • Download URL: test_helper_by_delica-1.0.6.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for test_helper_by_delica-1.0.6.tar.gz
Algorithm Hash digest
SHA256 7de2e40ee5ec06e463854966c6f9e62cba3fc7bef011cc92071ee9cef0cd8b53
MD5 7790bf1c086695c1a95be990cfb69704
BLAKE2b-256 abf78e791e835b4892375cd06e57d9c8773dd5393a0a9e17be73da409d3349cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for test_helper_by_delica-1.0.6.tar.gz:

Publisher: pypi_deploy.yml on DelicaLM/test_helper_by_delica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file test_helper_by_delica-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for test_helper_by_delica-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 97e34257341d6c581ebd2e400640cb2da10f7599a55479ecc87503fddbd9abed
MD5 a88a0683a37de7503560d1ec9a64719b
BLAKE2b-256 3f4ce6455d1e8392d17d13c54cf1a147499c9bdff70ca2329ed46a38c2f8a61b

See more details on using hashes here.

Provenance

The following attestation bundles were made for test_helper_by_delica-1.0.6-py3-none-any.whl:

Publisher: pypi_deploy.yml on DelicaLM/test_helper_by_delica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page