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.1.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.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: funalone-0.7.1.tar.gz
  • Upload date:
  • Size: 7.0 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.1.tar.gz
Algorithm Hash digest
SHA256 f4338d0bf574d27a6bc81a52de517e1320a238b25cf5d7b533ff6d49309a5b79
MD5 7b735e6ccaddf6400e0b253e59209fbf
BLAKE2b-256 1ff790558dc4da6f0bd8facf9bd6ef5a6650ed3da3951bd5ff5828f119c2d569

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: funalone-0.7.1-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.12.9

File hashes

Hashes for funalone-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62570d082e53a30324f840ad7d09e5533763cc74193cc8ee90460e93972ab391
MD5 66a94757a7aeac7cfda0b4d53baa24f2
BLAKE2b-256 cc4747e4df48a9c7e1d504c6e015724ad18573396e4f5ea54c24e2d1d8a20bc5

See more details on using hashes here.

Provenance

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