DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

1

python: Mock variations

This is my note on how to create various mocks. Please let me know if you have trouble creating a mock in your test.

import pytest
from unittest.mock import Mock

# Create a mock with one attribute.
def test_mock_with_one_attribute():
    mock = Mock(one_attribute='one')
    assert "one" == mock.one_attribute

# Create a mock with multiple attributes.
def test_mock_with_multiple_attributes():
    mock = Mock(**{"one_attribute":"one","two_attribute":"two"})
    assert "one" == mock.one_attribute
    assert "two" == mock.two_attribute

# Create a mock with name attribute. As name is an argument of Mock, 
# we need to speficy it after create mock object.
def test_mock_name_attribute():
    mock = Mock()
    mock.name = "ken"
    assert "ken" == mock.name

# Set return value when the mock itself is called as function.
def test_mock_as_function():
    mock = Mock(return_value='result')
    assert "result" == mock()

# Create a mock with one method.
def test_mock_with_one_method():
    mock = Mock(**{"my_method.return_value":"result"})
    assert "result" == mock.my_method()

# Create a mock with several methods and attributes at once.
def test_mock_with_methods_and_attributes():
    mock = Mock(**{"my_method.return_value":"result_my",
                   "another_method.return_value":"result_another",
                   "one_attribute":"one",
                   "two_attribute":"two"})
    assert "result_my" == mock.my_method()
    assert "result_another" == mock.another_method()
    assert "one" == mock.one_attribute
    assert "two" == mock.two_attribute

# Create a method that returns Exception.
def test_mock_with_side_effect_exception():
    mock = Mock(side_effect=Exception("dummy"))
    with pytest.raises(Exception) as e:
        mock()
    assert "dummy" == str(e.value)

# Create a mock and inner mock as it's attribute
def test_mock_with_innter_mock_multiple_attributes():
    mock = Mock(data = Mock(**{"one_attribute":"one","two_attribute":"two"}))
    assert "one" == mock.data.one_attribute
    assert "two" == mock.data.two_attribute
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay