<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Marco Richetta</title>
        <link>https://paragraph.com/@marco-richetta</link>
        <description>undefined</description>
        <lastBuildDate>Sat, 11 Jul 2026 11:34:13 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[Mocking de una función en tests de Python]]></title>
            <link>https://paragraph.com/@marco-richetta/mocking-de-una-funci-n-en-tests-de-python</link>
            <guid>LTSbkfi9ATZpikmqZjmN</guid>
            <pubDate>Tue, 02 Nov 2021 00:12:25 GMT</pubDate>
            <description><![CDATA[Recursoshttps://realpython.com/python-mock-library/https://gist.github.com/evansde77/45467f5a7af84d2a2d34f3fcb357449chttps://aaronlelevier.github.io/python-unit-testing-with-magicmock/Suponiendo que existe una función get_github_profile en services.pyimport requests def get_github_profile(user_id: int): """ Obtener datos de un usuario de Github """ headers = {"accept": "application/vnd.github.v3+json"} response = requests.get(f"https://api.github.com/users/{user_id}", headers=headers) user = ...]]></description>
            <content:encoded><![CDATA[<h3 id="h-recursos" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Recursos</h3><ul><li><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://realpython.com/python-mock-library/#conclusion">https://realpython.com/python-mock-library/</a></p></li><li><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://gist.github.com/evansde77/45467f5a7af84d2a2d34f3fcb357449c">https://gist.github.com/evansde77/45467f5a7af84d2a2d34f3fcb357449c</a></p></li><li><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://aaronlelevier.github.io/python-unit-testing-with-magicmock/">https://aaronlelevier.github.io/python-unit-testing-with-magicmock/</a></p></li></ul><p>Suponiendo que existe una función <code>get_github_profile</code> en <code>services.py</code></p><pre data-type="codeBlock" text="import requests

def get_github_profile(user_id: int):
    &quot;&quot;&quot; Obtener datos de un usuario de Github &quot;&quot;&quot;

    headers = {&quot;accept&quot;: &quot;application/vnd.github.v3+json&quot;}

    response = requests.get(f&quot;https://api.github.com/users/{user_id}&quot;, headers=headers)

    user = response.json()

    return user[&quot;name&quot;]
"><code><span class="hljs-keyword">import</span> requests

<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_github_profile</span>(<span class="hljs-params">user_id: <span class="hljs-built_in">int</span></span>):
    <span class="hljs-string">""" Obtener datos de un usuario de Github """</span>

    headers = {<span class="hljs-string">"accept"</span>: <span class="hljs-string">"application/vnd.github.v3+json"</span>}

    response = requests.get(<span class="hljs-string">f"https://api.github.com/users/<span class="hljs-subst">{user_id}</span>"</span>, headers=headers)

    user = response.json()

    <span class="hljs-keyword">return</span> user[<span class="hljs-string">"name"</span>]
</code></pre><p>Si hacemos un test que ejecute 100 veces esta función, encontraremos 2 problemas:</p><ul><li><p>Las requests HTTP a una API externa son <strong>lentas.</strong></p></li><li><p>Posiblemente alcancemos un límite de requests muy rápidamente.</p></li></ul><p>En estos casos podemos <em>mockear</em> esa request a la API externa.</p><pre data-type="codeBlock" text="def test_get_github_profile(self):

    with patch(&apos;requests.get&apos;) as mock_request:

        # A partir de acá `requests.get` es nuestro mock y podemos
        # modificar su comportamiento como queramos

        # Set del valor que va a devolver nuestra request &quot;fake&quot;
        mock_request.return_value = {&quot;user_id&quot;: 150, &quot;name&quot;: &quot;Katie&quot;}

        result = get_github_profile(user_id=150)

        assert result == &quot;Katie&quot;
"><code>def test_get_github_profile(<span class="hljs-built_in">self</span>):

    with patch(<span class="hljs-string">'requests.get'</span>) <span class="hljs-keyword">as</span> mock_request:

        # A partir de acá `requests.get` es nuestro mock y podemos
        # modificar su comportamiento como queramos

        # Set del valor que va a devolver nuestra request <span class="hljs-string">"fake"</span>
        mock_request.return_value <span class="hljs-operator">=</span> {<span class="hljs-string">"user_id"</span>: <span class="hljs-number">150</span>, <span class="hljs-string">"name"</span>: <span class="hljs-string">"Katie"</span>}

        result <span class="hljs-operator">=</span> get_github_profile(user_id<span class="hljs-operator">=</span><span class="hljs-number">150</span>)

        <span class="hljs-built_in">assert</span> result <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">"Katie"</span>
</code></pre>]]></content:encoded>
            <author>marco-richetta@newsletter.paragraph.com (Marco Richetta)</author>
        </item>
    </channel>
</rss>