{"id":21816602,"url":"https://github.com/5monkeys/mock","last_synced_at":"2025-09-11T10:36:26.853Z","repository":{"id":4343776,"uuid":"5479694","full_name":"5monkeys/mock","owner":"5monkeys","description":"Yet another mirror of code.google.com/p/mock","archived":false,"fork":false,"pushed_at":"2012-08-20T10:11:19.000Z","size":8928,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T10:13:24.511Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.voidspace.org.uk/python/mock/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/5monkeys.png","metadata":{"files":{"readme":"README.txt","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-20T10:10:36.000Z","updated_at":"2014-01-16T14:05:37.000Z","dependencies_parsed_at":"2022-08-06T16:15:19.844Z","dependency_job_id":null,"html_url":"https://github.com/5monkeys/mock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/5monkeys/mock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/5monkeys","download_url":"https://codeload.github.com/5monkeys/mock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fmock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274616485,"owners_count":25318160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-27T15:35:29.538Z","updated_at":"2025-09-11T10:36:26.802Z","avatar_url":"https://github.com/5monkeys.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"mock is a library for testing in Python. It allows you to replace parts of\nyour system under test with mock objects and make assertions about how they\nhave been used.\n\nmock provides a core `MagicMock` class removing the need to create a host of\nstubs throughout your test suite. After performing an action, you can make\nassertions about which methods / attributes were used and arguments they were\ncalled with. You can also specify return values and set needed attributes in\nthe normal way.\n\nmock is tested on Python versions 2.4-2.7 and Python 3. mock is also tested\nwith the latest versions of Jython and pypy.\n\nThe mock module also provides utility functions / objects to assist with\ntesting, particularly monkey patching.\n\n* `PDF documentation for 1.0 beta 1\n  \u003chttp://www.voidspace.org.uk/downloads/mock-1.0b1.pdf\u003e`_\n* `mock on google code (repository and issue tracker)\n  \u003chttp://code.google.com/p/mock/\u003e`_\n* `mock documentation\n  \u003chttp://www.voidspace.org.uk/python/mock/\u003e`_\n* `mock on PyPI \u003chttp://pypi.python.org/pypi/mock/\u003e`_\n* `Mailing list (testing-in-python@lists.idyll.org)\n  \u003chttp://lists.idyll.org/listinfo/testing-in-python\u003e`_\n\nMock is very easy to use and is designed for use with\n`unittest \u003chttp://pypi.python.org/pypi/unittest2\u003e`_. Mock is based on\nthe 'action -\u003e assertion' pattern instead of 'record -\u003e replay' used by many\nmocking frameworks. See the `mock documentation`_ for full details.\n\nMock objects create all attributes and methods as you access them and store\ndetails of how they have been used. You can configure them, to specify return\nvalues or limit what attributes are available, and then make assertions about\nhow they have been used::\n\n    \u003e\u003e\u003e from mock import Mock\n    \u003e\u003e\u003e real = ProductionClass()\n    \u003e\u003e\u003e real.method = Mock(return_value=3)\n    \u003e\u003e\u003e real.method(3, 4, 5, key='value')\n    3\n    \u003e\u003e\u003e real.method.assert_called_with(3, 4, 5, key='value')\n\n`side_effect` allows you to perform side effects, return different values or\nraise an exception when a mock is called::\n\n   \u003e\u003e\u003e mock = Mock(side_effect=KeyError('foo'))\n   \u003e\u003e\u003e mock()\n   Traceback (most recent call last):\n    ...\n   KeyError: 'foo'\n   \u003e\u003e\u003e values = {'a': 1, 'b': 2, 'c': 3}\n   \u003e\u003e\u003e def side_effect(arg):\n   ...     return values[arg]\n   ...\n   \u003e\u003e\u003e mock.side_effect = side_effect\n   \u003e\u003e\u003e mock('a'), mock('b'), mock('c')\n   (3, 2, 1)\n   \u003e\u003e\u003e mock.side_effect = [5, 4, 3, 2, 1]\n   \u003e\u003e\u003e mock(), mock(), mock()\n   (5, 4, 3)\n\nMock has many other ways you can configure it and control its behaviour. For\nexample the `spec` argument configures the mock to take its specification from\nanother object. Attempting to access attributes or methods on the mock that\ndon't exist on the spec will fail with an `AttributeError`.\n\nThe `patch` decorator / context manager makes it easy to mock classes or\nobjects in a module under test. The object you specify will be replaced with a\nmock (or other object) during the test and restored when the test ends::\n\n    \u003e\u003e\u003e from mock import patch\n    \u003e\u003e\u003e @patch('test_module.ClassName1')\n    ... @patch('test_module.ClassName2')\n    ... def test(MockClass2, MockClass1):\n    ...     test_module.ClassName1()\n    ...     test_module.ClassName2()\n\n    ...     assert MockClass1.called\n    ...     assert MockClass2.called\n    ...\n    \u003e\u003e\u003e test()\n\n.. note::\n\n   When you nest patch decorators the mocks are passed in to the decorated\n   function in the same order they applied (the normal *python* order that\n   decorators are applied). This means from the bottom up, so in the example\n   above the mock for `test_module.ClassName2` is passed in first.\n\n   With `patch` it matters that you patch objects in the namespace where they\n   are looked up. This is normally straightforward, but for a quick guide\n   read `where to patch\n   \u003chttp://www.voidspace.org.uk/python/mock/patch.html#where-to-patch\u003e`_.\n\nAs well as a decorator `patch` can be used as a context manager in a with\nstatement::\n\n    \u003e\u003e\u003e with patch.object(ProductionClass, 'method') as mock_method:\n    ...     mock_method.return_value = None\n    ...     real = ProductionClass()\n    ...     real.method(1, 2, 3)\n    ...\n    \u003e\u003e\u003e mock_method.assert_called_once_with(1, 2, 3)\n\nThere is also `patch.dict` for setting values in a dictionary just during the\nscope of a test and restoring the dictionary to its original state when the\ntest ends::\n\n   \u003e\u003e\u003e foo = {'key': 'value'}\n   \u003e\u003e\u003e original = foo.copy()\n   \u003e\u003e\u003e with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):\n   ...     assert foo == {'newkey': 'newvalue'}\n   ...\n   \u003e\u003e\u003e assert foo == original\n\nMock supports the mocking of Python magic methods. The easiest way of\nusing magic methods is with the `MagicMock` class. It allows you to do\nthings like::\n\n    \u003e\u003e\u003e from mock import MagicMock\n    \u003e\u003e\u003e mock = MagicMock()\n    \u003e\u003e\u003e mock.__str__.return_value = 'foobarbaz'\n    \u003e\u003e\u003e str(mock)\n    'foobarbaz'\n    \u003e\u003e\u003e mock.__str__.assert_called_once_with()\n\nMock allows you to assign functions (or other Mock instances) to magic methods\nand they will be called appropriately. The MagicMock class is just a Mock\nvariant that has all of the magic methods pre-created for you (well - all the\nuseful ones anyway).\n\nThe following is an example of using magic methods with the ordinary Mock\nclass::\n\n    \u003e\u003e\u003e from mock import Mock\n    \u003e\u003e\u003e mock = Mock()\n    \u003e\u003e\u003e mock.__str__ = Mock(return_value = 'wheeeeee')\n    \u003e\u003e\u003e str(mock)\n    'wheeeeee'\n\nFor ensuring that the mock objects your tests use have the same api as the\nobjects they are replacing, you can use \"auto-speccing\". Auto-speccing can\nbe done through the `autospec` argument to patch, or the `create_autospec`\nfunction. Auto-speccing creates mock objects that have the same attributes\nand methods as the objects they are replacing, and any functions and methods\n(including constructors) have the same call signature as the real object.\n\nThis ensures that your mocks will fail in the same way as your production\ncode if they are used incorrectly::\n\n   \u003e\u003e\u003e from mock import create_autospec\n   \u003e\u003e\u003e def function(a, b, c):\n   ...     pass\n   ...\n   \u003e\u003e\u003e mock_function = create_autospec(function, return_value='fishy')\n   \u003e\u003e\u003e mock_function(1, 2, 3)\n   'fishy'\n   \u003e\u003e\u003e mock_function.assert_called_once_with(1, 2, 3)\n   \u003e\u003e\u003e mock_function('wrong arguments')\n   Traceback (most recent call last):\n    ...\n   TypeError: \u003clambda\u003e() takes exactly 3 arguments (1 given)\n\n`create_autospec` can also be used on classes, where it copies the signature of\nthe `__init__` method, and on callable objects where it copies the signature of\nthe `__call__` method.\n\nThe distribution contains tests and documentation. The tests require\n`unittest2 \u003chttp://pypi.python.org/pypi/unittest2\u003e`_ to run.\n\nDocs from the in-development version of `mock` can be found at\n`mock.readthedocs.org \u003chttp://mock.readthedocs.org\u003e`_.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F5monkeys%2Fmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fmock/lists"}