Skip to main content

A module to easily mock code while testing.

Project description

auto_mock is a module for mocking during unit testing. It automatically replaces all variables and functions external to the unit with default values to help developers exclude external state in their unit tests.

Installation

pip install python-auto-mock

Usage

In the example we will be making use of myclass.py

myclass.py

X = 1
Y = 2
Z = 3

class MyClass():
    def addition_with_globals(self):
        print(X)
        print(Y)
        print(X + Y)
        return X + Y

To test this class we will write a test script.

import auto_mock

def test_my_class_addition():
    stubs = {
        'X' : 36,
        'Y' : 35,
    }

    with auto_mock.full_mock('myclass', 'MyClass.addition_with_globals', stubs) as addition_with_globals:
        if addition_with_globals() != 71:
            print("Addition is broken")

def main():
    test_my_class_addition()

main()

Done correctly this should print out

36
35
71

full_mock takes 3 non optional arguments.

  1. The module name where the code is located.

  2. The specific function or method that is being tested.

  3. The dictionary of stubs/mocks that you would like to override the values with.

The stubs variable contains the names of the variables that are being mocked. In this example we have X, Y, and Z as global variables. We only use X and Y in this example, so they are the only variables that are in the stubs dictionary.

full_mock returns the function or method that you are trying to test with the external variables mocked. If the variable is not within the stubs then it is mocked with a default object. Inside the with block you can test that the function does what it is supposed to do.

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

python-auto-mock-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

python_auto_mock-0.1.1-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file python-auto-mock-0.1.1.tar.gz.

File metadata

File hashes

Hashes for python-auto-mock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 48b0be039aef5ab5821d397e5b58f611e6658fb713b25dff2dfd22eff628ba41
MD5 88ccc245e360ea7d72171943b50b36de
BLAKE2b-256 45abceaf1f9c5dd096ba8a6384ccb03103f36ab3332a99f5331c46c7864ceb28

See more details on using hashes here.

File details

Details for the file python_auto_mock-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_auto_mock-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5a37acfe229af2e480f5efb626cba6fa43821a4c85cbf68b593b001e56f56e61
MD5 b52af7b577c47ec71f25e1565df219eb
BLAKE2b-256 f42941d09cafbfac354e37cbb43c8b90908d669cae02675c9824ffbe741530a8

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