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.0.tar.gz (6.8 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.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for funalone-0.7.0.tar.gz
Algorithm Hash digest
SHA256 63925c7c43b2d3f10c61d309360b20dc704093c376d0933bd288e6c7f41c928a
MD5 f943230dd89c9a5d03feebeb818d63c3
BLAKE2b-256 35e3bb3a99ccef291ed074a989cd3c3907244c040589bac86328a16981764726

See more details on using hashes here.

Provenance

The following attestation bundles were made for funalone-0.7.0.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.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for funalone-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56b325009bf9ad42b27bc7bb0d55dcb2d152ff5ae669b7bcb0a127a740b330e2
MD5 0ac244c1784224609ddfaa84ec547ec9
BLAKE2b-256 00779a8a61de28746e94900ad7db35a02bfb855b3c98747a3d7fdf45d2368da5

See more details on using hashes here.

Provenance

The following attestation bundles were made for funalone-0.7.0-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