Skip to main content

Generate python mocks and assertions quickly

Project description

Mock Generator

A tool to generate the basic mocks and asserts for faster unit testing.

Introduction

A typical unit test looks like this (AAA pattern):

  • Arrange – Setup and prepare the various objects and prerequisites.
  • Act – Invoke the tested code by calling the tested function.
  • Assert – Verify the outcome. This can be the return value of the tested function and/or some side effects.

When using mocks, much time is wasted on the wiring. The Arrange and Assert sections are notorious in that regard. Only a fraction of the time is spent on the actual logic of the test.

This tool is meant to save you time, by generating the Arrange and Assert sections for you. The generated code can then be used as is, or altered according to your needs.

Usage

Note: All examples assume usage of the pytest-mock which is a fixture for pytest.

Getting Started

Let's assume you have a module named tested_module.py which holds a function to process a string sent to it and then add it to a zip file:

import zipfile

def process_and_zip(zip_path, file_name, contents):
    processed_contents = "processed " + contents  # some complex logic
    with zipfile.ZipFile(zip_path, 'w') as zip_container:
        zip_container.writestr(file_name, processed_contents)

This is the unit under test, or UUT.

Although this is a very short function, writing the test code takes a lot of time. It's the fact that the function uses a context manager makes the testing more complex than it should be. If you don't believe me, try to manually write mocks and asserts which verify that zip_container.writestr was called with the right parameters.

In any case, you start with a test skeleton:

import mock_autogen
from tests.sample.code.tested_module import process_and_zip

def test_process_and_zip(mocker):
    # Arrange: todo  

    # Act: invoking the tested code
    process_and_zip('/path/to.zip', 'in_zip.txt', 'foo bar')

    # Assert: todo

Now it's time to use Mock Generator instead of manually writing the 'Arrange' and 'Assert' sections.

Generating the 'Arrange' section

To generate the 'Arrange' section, simply put this code at the beginning of your test function skeleton and run it:

mock_autogen.generate_uut_mocks(process_and_zip)

This will generate the 'Arrange' section for you:

# mocked functions
mock_ZipFile = mocker.MagicMock(name='ZipFile')
mocker.patch('tests.sample.code.tested_module.zipfile.ZipFile', new=mock_ZipFile)

The generated code is returned, printed to the console and also copied to the clipboard for your convenience. Just paste it (as simple as ctrl+V) at the start of your test function:

import mock_autogen
from tests.sample.code.tested_module import process_and_zip

def test_process_and_zip(mocker):
    # mocked functions
    mock_ZipFile = mocker.MagicMock(name='ZipFile')
    mocker.patch('tests.sample.code.tested_module.zipfile.ZipFile', new=mock_ZipFile)

    # Act: invoking the tested code
    process_and_zip('/path/to.zip', 'in_zip.txt', 'foo bar')

    # Assert: todo

Excellent! Arrange section is ready.

Generating the Assert section

Now it's time to add the asserts. Add the following code at the 'Assert' step:

mock_autogen.generate_asserts(mock_ZipFile)

The mock_ZipFile is the mock object you generated earlier. Now execute the test function to get the assert section:

assert 1 == mock_ZipFile.call_count
mock_ZipFile.assert_called_once_with('/path/to.zip', 'w')
mock_ZipFile.return_value.__enter__.assert_called_once_with()
mock_ZipFile.return_value.__enter__.return_value.writestr.assert_called_once_with('in_zip.txt', 'processed foo bar')
mock_ZipFile.return_value.__exit__.assert_called_once_with(None, None, None)

Wow, that's a handful of asserts! Some are very useful:

  • Checking that we opened the zip file with the right parameters.
  • Checking that we wrote the correct data to the proper file.
  • Finally, ensuring that __enter__ and __exit__ are called, so there are no open file handles which could cause problems.

You can remove any generated line which you find unnecessary.

Paste that code right after the act phase, and you're done!

The complete test function:

from tests.sample.code.tested_module import process_and_zip

def test_process_and_zip(mocker):
    # mocked functions
    mock_ZipFile = mocker.MagicMock(name='ZipFile')
    mocker.patch('tests.sample.code.tested_module.zipfile.ZipFile', new=mock_ZipFile)

    # Act: invoking the tested code
    process_and_zip('/path/to.zip', 'in_zip.txt', 'foo bar')

    assert 1 == mock_ZipFile.call_count
    mock_ZipFile.assert_called_once_with('/path/to.zip', 'w')
    mock_ZipFile.return_value.__enter__.assert_called_once_with()
    mock_ZipFile.return_value.__enter__.return_value.writestr.assert_called_once_with('in_zip.txt', 'processed foo bar')
    mock_ZipFile.return_value.__exit__.assert_called_once_with(None, None, None)

Can you imagine the time it would have taken you to code this on your own?

What's Next

Asserting Existing Mocks

At times, you may be editing a test code already containing mocks, or you choose to write the mocks yourself, to gain some extra control.

Mock Generator can generate the assert section for standard Python mocks, even if they were not created using the Mock Generator.

Put this after the 'Act' (replace mock_obj with your mock object name):

import mock_autogen
mock_autogen.generate_asserts(mock_obj)

Take the generated code and paste it at the 'Assert' section.

Generating the 'Arrange' and 'Assert' sections in one call

You can make the `` call create the generate_asserts code for you:

import mock_autogen
mock_autogen.generate_uut_mocks_with_asserts(function_under_test)

Mocking Everything

So far you have seen examples of mocking the dependencies of a single function. Mock Generator can generate mocks for objects, classes and entire modules!

A great way to learn about those capabilities is to see them in action:

from mock_autogen import PytestMocker
PytestMocker(a_module_or_a_class_you_test_and_want_to_mock_its_dependencies).mock_everything().generate() 

PytestMocker class has many options to produce different kind of mocks. See its documentation for further details.

Wrapping up

I hope that you were convinced that this tool can save you a lot of time.

If you have improvement suggestions, bug reports, or would like to contribute pull requests, let me know.

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

mock-generator-2.0.1.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

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

mock_generator-2.0.1-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file mock-generator-2.0.1.tar.gz.

File metadata

  • Download URL: mock-generator-2.0.1.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10

File hashes

Hashes for mock-generator-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9e79f7eb9496c9b6bc5e8ca8b0275c58ae81b3be7f89d969523ae4b830b2f3b6
MD5 4a149d57c79bcd990cf702f2ea83d464
BLAKE2b-256 ee43acc7e77dfb4b8dd58544f6cbfbf37c12737ee8a53a4a92c3825ca14d7179

See more details on using hashes here.

File details

Details for the file mock_generator-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: mock_generator-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10

File hashes

Hashes for mock_generator-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d98918882075bec279d1197352e1d3f33a80cfa549541323fd3cb42ad7ca0dd0
MD5 506ea356b2b1b4954b91f3f90594084f
BLAKE2b-256 b28d04f0353edc2797cc0cd0ba5e730fc177776702fd7987d7bf1fa6fc19ad5f

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