{"id":13772024,"url":"https://github.com/alex/pretend","last_synced_at":"2025-05-16T07:06:39.499Z","repository":{"id":5400817,"uuid":"6590477","full_name":"alex/pretend","owner":"alex","description":"A library for stubbing in python","archived":false,"fork":false,"pushed_at":"2024-06-22T05:11:32.000Z","size":49,"stargazers_count":313,"open_issues_count":3,"forks_count":20,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-13T23:15:02.647Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alex.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.rst","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-11-08T03:15:14.000Z","updated_at":"2025-05-03T00:44:55.000Z","dependencies_parsed_at":"2024-01-13T00:37:42.847Z","dependency_job_id":"edcda458-4241-472b-be1c-9011e5632ff1","html_url":"https://github.com/alex/pretend","commit_stats":{"total_commits":81,"total_committers":9,"mean_commits":9.0,"dds":0.5432098765432098,"last_synced_commit":"a910eb32d183b06db6aa3ee2983dddeb17a56ae3"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alex%2Fpretend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alex%2Fpretend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alex%2Fpretend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alex%2Fpretend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alex","download_url":"https://codeload.github.com/alex/pretend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254485063,"owners_count":22078767,"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","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-08-03T17:00:58.973Z","updated_at":"2025-05-16T07:06:34.484Z","avatar_url":"https://github.com/alex.png","language":"Python","readme":"pretend\n=======\n\nPretend is a library to make stubbing with Python easier.\n\nWhat is stubbing?\n-----------------\n\nStubbing is a technique for writing tests. You may hear the term mixed up with\nmocks, fakes, or doubles. Basically a stub is an object that returns pre-canned\nresponses, rather than doing any computation.\n\nMartin Fowler does a good job explaining the terms in his `Mocks Aren't Stubs`_\narticle.\n\n.. _`Mocks Aren't Stubs`: http://martinfowler.com/articles/mocksArentStubs.html\n\nHow do I install ``pretend``?\n-----------------------------\n\nIt's easy with ``pip``!\n\n.. code:: bash\n\n    $ pip install pretend\n\nHow do I use ``pretend``?\n-------------------------\n\nIt's easy, the ``stub`` function makes it easy to create a stub:\n\n.. code:: pycon\n\n    \u003e\u003e\u003e from pretend import stub\n    \u003e\u003e\u003e x = stub(country_code=\"US\")\n    \u003e\u003e\u003e some_function(x)\n\nHere ``x`` will be an object with a single attribute ``country_code`` which has\nthe value ``\"US\"``. Unlike mocks, ``x`` will not respond to any other attribute\nor methods, nor does it have any methods for making assertions about what you\naccessed.\n\nIf you want to add a method to the stub, simply provide a function to it:\n\n.. code:: pycon\n\n    \u003e\u003e\u003e from pretend import stub\n    \u003e\u003e\u003e x = stub(country_code=lambda: \"US\")\n    \u003e\u003e\u003e x.country_code()\n    'US'\n\nIt's important to note that functions on stubs *do not* take a ``self``\nargument, this is because stubs should be returning pre-canned values, not\ndoing computations.\n\nExceptions with ``pretend``\n---------------------------\n\nSometimes a method you want to stub doesn't return a value, but instead raises\nan exception. To make this easy, ``pretend`` provides a helper function,\n``raiser``, it can be used like so:\n\n.. code:: pycon\n\n    \u003e\u003e\u003e from pretend import stub, raiser\n    \u003e\u003e\u003e x = stub(func=raiser(ValueError))\n    \u003e\u003e\u003e x.func()\n    Traceback (most recent call last):\n      File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n      File \"pretend.py\", line 74, in inner\n        raise exc\n    ValueError\n\nWhy is stubbing better?\n-----------------------\n\nIdeally stubbing tests how your system responds to a particular input, rather\nthan which API is used. Stubbing still requires you to write tests that check\nthe results of a computation, rather than looking for side effects. This\ndoesn't always work though, so you do sometimes still need mocking (e.g.\nsometimes you really want to check for a side effect.)\n\nHow do I get my stub into place?\n--------------------------------\n\nIf you come from other mocking libraries you're probably used to a ``patch``\nmethod to put a mock in place. ``pretend`` doesn't include anything like this,\na) we believe it's better, where possible, to pass stubs as arguments rather\nthan monkey patch them into place, b) we believe that when you do need to\nmonkey patch something into place you should use something provided by your\ntesting tool. ``py.test`` includes `such a tool`_.\n\n.. _`such a tool`: https://docs.pytest.org/en/latest/monkeypatch.html\n\nWhat if I really need to record the calls?\n------------------------------------------\n\nIf you really really need to, ``pretend`` includes a ``call_recorder`` utility:\n\n.. code:: pycon\n\n    \u003e\u003e\u003e from pretend import call_recorder, call\n    \u003e\u003e\u003e f = call_recorder(lambda a: a + 2)\n    \u003e\u003e\u003e f(3)\n    5\n    \u003e\u003e\u003e assert f.calls == [call(3)]\n\nWho wrote this?\n---------------\n\n``pretend`` is by Alex Gaynor, who was just tired of not having a good stubbing\ntool for Python. The name is from Idan Gazit.\n","funding_links":[],"categories":["Mock and Stub","Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falex%2Fpretend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falex%2Fpretend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falex%2Fpretend/lists"}