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

Uploaded Python 3

File details

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

File metadata

  • Download URL: funalone-0.7.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2fd076cc1453f3cc220a1587bc4ecce2aae1dbefc54a58e32ecd3feb38d5e51b
MD5 414b2724211bf7db91e4a53f52e68368
BLAKE2b-256 d95247131859a843f731d261ee6513d2f6d2391922b62047eafd9b325302f420

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: funalone-0.7.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 edbf8b9f35943129b2073e5f38b5d92373f21c1b002387d2faed6b48db34c55a
MD5 6ad6b2b80072550f1c1bd56f3bd365f3
BLAKE2b-256 e790a13dfc9010abbb8c2d5cb352c378c364e9be5798b6a8176f22b0e58b75d7

See more details on using hashes here.

Provenance

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