Skip to main content

Unittest functions isolated.

Project description

FunAlone

A small python library that makes it easy to test a function alone, that is, mocking all dependencies. True unittests, without having to manually look and add every called function to an increasing list of @mock.patch decorators.

Example

Say you have a function named do_a_lot_of_stuff and you want to test it with unittest, only testing the functions code, not the function that it calls within.

def do_a_lot_of_stuff(arg: int):
    if arg > 5:
        result = do_this(arg)
    elif arg < 0:
        result = do_that(arg)
    else:
        result = do_nothing(arg)
    
    return result

In this case you might want to know which external function has been called, depending on the input, without having the code actually call that function (which could have side effects and should have it's own unit tests).

class Test(unittest.TestCase):
    def test_do_a_lot_of_stuff_with_5(self):
        with IsolatedFunctionClone(
            do_a_lot_of_stuff
            ) as do_a_lot_of_stuff_clone:

            # This calls a function clone with an isolated context.
            # All external names are mocked.
            do_a_lot_of_stuff_clone(5)

            # You can access this context later to assert calls,
            # And you can use the original functions to do this,
            # so it's all refactor-friendly.
            do_a_lot_of_stuff_clone.context[do_this].assert_not_called()
            do_a_lot_of_stuff_clone.context[do_that].assert_not_called()
            do_a_lot_of_stuff_clone.context[do_nothing].assert_called_once()

There are more parameters and ways to use the library, this is only a basic example.

Installation

You can install the latest version with pip:

pip install funalone

Tests

There is a small collection of unittests in the test folder. You can run them using unittest.

Credits

This project is developed by Borja Martinena (borjamartinena[at]gmail.com).

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

funalone-0.7.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

funalone-0.7.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file funalone-0.7.3.tar.gz.

File metadata

  • Download URL: funalone-0.7.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for funalone-0.7.3.tar.gz
Algorithm Hash digest
SHA256 c4e5c7e9a6fba826fe63cd916014f21dc00729c50648d42f04a5e514bb511c65
MD5 1def6f4a2fc20e02ae8ebd169f45db6b
BLAKE2b-256 de2cb4f2a9b2ef0af8ebf3f5d461db58e36ecdc6e5568d2a568b2829c338ef76

See more details on using hashes here.

Provenance

The following attestation bundles were made for funalone-0.7.3.tar.gz:

Publisher: publish.yml on aafrecct/funalone

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

File details

Details for the file funalone-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: funalone-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for funalone-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 df42d0c21d8055f369c1e91fc85a842985367f368b8fcaa2bc31eed6d90d7fe0
MD5 b2328d6fe559ea0213fb5ff35ac8584a
BLAKE2b-256 cd786cc13c17087f1efd5b64508da8a6040c311a67f105369f66d8ac9b210d44

See more details on using hashes here.

Provenance

The following attestation bundles were made for funalone-0.7.3-py3-none-any.whl:

Publisher: publish.yml on aafrecct/funalone

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