Skip to main content

A package to test functions with given inputs and expected outputs

Project description

Vaibhav Function Tester

Vaibhav Function Tester is a comprehensive Python package designed to test functions with specified inputs and expected outputs. This package streamlines the process of validating functions, providing detailed test results, and offering a convenient command-line interface (CLI) for easy testing directly from the terminal.

Features

  • Automated Testing: Seamlessly run multiple test cases on a specified function.
  • Detailed Results: Receive thorough information on each test case, including input, expected output, actual output, execution time, and the test result.
  • Command-Line Interface: Effortlessly run tests from the terminal with the CLI.
  • Customizable: Easily integrate with any function and test case format.

Installation

You can install the Vaibhav Function Tester package using pip:

pip install vaibhav_function_tester



## Usage

### Importing and Using in a Script

To use the package, define the function you want to test and create a list of test cases. Each test case should be a dictionary with `input` and `expected_output` keys.

#### Sample Function

Suppose you have a function that finds the index of a query element in a list of cards:

```python
def sample_function(cards, query):
    return cards.index(query) if query in cards else -1

Test Cases

Create a list of test cases:

test_cases = [
    {
        "input": {"cards": [13, 11, 10, 7, 4, 3, 1, 0], "query": 7},
        "expected_output": 3
    },
    {
        "input": {"cards": [13, 11, 10, 7, 4, 3, 1, 0], "query": 5},
        "expected_output": -1
    }
]

Running the Tests

Use the FunctionTester class to run the tests and print the results:

from vaibhav_function_tester.tester import FunctionTester

tester = FunctionTester(sample_function, test_cases)
results = tester.run_tests()

for result in results:
    print(f"Input: {result['input']}")
    print(f"Expected Output: {result['expected_output']}")
    print(f"Actual Output: {result['actual_output']}")
    print(f"Execution Time: {result['execution_time']:.3f} ms")
    print(f"Test Result: {result['result']}\n")

Using the CLI

The Vaibhav Function Tester package includes a command-line interface (CLI) for running tests directly from the terminal.

  1. Create a JSON file with your test cases. Save it as test_cases.json:

    [
        {
            "input": {"cards": [13, 11, 10, 7, 4, 3, 1, 0], "query": 7},
            "expected_output": 3
        },
        {
            "input": {"cards": [13, 11, 10, 7, 4, 3, 1, 0], "query": 5},
            "expected_output": -1
        }
    ]
    
  2. Run the CLI with the function module and test cases file as arguments:

    function_tester sample_module.sample_function test_cases.json
    

    The CLI will output the detailed results for each test case.

Examples

Here are some example usages of the Vaibhav Function Tester:

Example 1: Testing a Simple Addition Function

Function to Test

def add(a, b):
    return a + b

Test Cases

test_cases = [
    {
        "input": {"a": 1, "b": 2},
        "expected_output": 3
    },
    {
        "input": {"a": -1, "b": 1},
        "expected_output": 0
    }
]

Running the Tests

from vaibhav_function_tester.tester import FunctionTester

tester = FunctionTester(add, test_cases)
results = tester.run_tests()

for result in results:
    print(f"Input: {result['input']}")
    print(f"Expected Output: {result['expected_output']}")
    print(f"Actual Output: {result['actual_output']}")
    print(f"Execution Time: {result['execution_time']:.3f} ms")
    print(f"Test Result: {result['result']}\n")

Example 2: Using the CLI to Test a Function

  1. Function Module (math_operations.py)

    def multiply(a, b):
        return a * b
    
  2. Test Cases JSON File (multiply_test_cases.json)

    [
        {
            "input": {"a": 3, "b": 4},
            "expected_output": 12
        },
        {
            "input": {"a": -1, "b": -1},
            "expected_output": 1
        }
    ]
    
  3. Running the CLI

    function_tester math_operations.multiply multiply_test_cases.json
    

    The CLI will output the detailed results for each test case.

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

vaibhav_function_tester-0.2.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

vaibhav_function_tester-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file vaibhav_function_tester-0.2.0.tar.gz.

File metadata

  • Download URL: vaibhav_function_tester-0.2.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for vaibhav_function_tester-0.2.0.tar.gz
Algorithm Hash digest
SHA256 99e2fc7a077b2cd53d24b1adcdf96a542b9a0bf84fb87543c6d488e0ca0ee5e6
MD5 e963c6083e80d353d1127e9fbde881bb
BLAKE2b-256 347b640fa93646ee1266a55e8fd6632a7d42abf92f2ef4b0cea5c10fb85ca685

See more details on using hashes here.

File details

Details for the file vaibhav_function_tester-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for vaibhav_function_tester-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8135a154c1d5a6c15973139edae495cb142016ad937733f01d57690a32b0cbbd
MD5 cbd5f967e81ff9b70305648335bd5bcc
BLAKE2b-256 cea3ea550f8b2b7ee62431337aa1e1d8faac33dee550f267efc81dc9782c4da9

See more details on using hashes here.

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