<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shane O'Neill</title>
    <description>The latest articles on DEV Community by Shane O'Neill (@zeevo).</description>
    <link>https://dev.to/zeevo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F120487%2Fc5cdfc5d-c753-45aa-9df3-321a249fb602.jpg</url>
      <title>DEV Community: Shane O'Neill</title>
      <link>https://dev.to/zeevo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zeevo"/>
    <language>en</language>
    <item>
      <title>Effective Python API testing using Django and Patch</title>
      <dc:creator>Shane O'Neill</dc:creator>
      <pubDate>Mon, 17 Dec 2018 02:11:22 +0000</pubDate>
      <link>https://dev.to/zeevo/effective-python-api-testing-using-django-and-patch-1amj</link>
      <guid>https://dev.to/zeevo/effective-python-api-testing-using-django-and-patch-1amj</guid>
      <description>&lt;p&gt;Today I would like to offer a brief showcase on how to effectively test a Django API. Essentially, what we want to accomplish is the ability to test and control how our applications handle specific responses from external web APIs. A program might make calls to several different APIs like Reddit’s or Google’s, and we want to capture these responses under test. More importantly, in a way that our tests can pass every time regardless of the status of the APIs we consume.&lt;/p&gt;

&lt;h2&gt;
  
  
  How?
&lt;/h2&gt;

&lt;p&gt;By using Patch.&lt;/p&gt;

&lt;p&gt;Patch is a decorator in the library &lt;code&gt;unittest.mock&lt;/code&gt;. It allows us to replace attributes in our program with &lt;em&gt;mock&lt;/em&gt; objects and make assertions on how our program with behave. Let’s take a look.&lt;/p&gt;

&lt;p&gt;Here we have a simple Django API generated using my &lt;a href="https://shaneoneill.io/starters/" rel="noopener noreferrer"&gt;project starter&lt;/a&gt;. It makes a query to Reddit’s API for information, and returns a simple JSON payload to the client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2Fview-1-1024x545.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2Fview-1-1024x545.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And of course, a corresponding test:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2Ftests_before-1024x391.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2Ftests_before-1024x391.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait- what if Reddit’s API is down? Our test will fail. Unit tests should never fail because of someone else’s application. They should always pass regardless of the status of other systems. We also cannot reliably capture both (1) Reddit is ok, and (2) Reddit is down. For this, we need to use Patch to usurp our call to to Reddit’s API with a mock function.&lt;/p&gt;

&lt;p&gt;Let’s rewrite our test using Patch:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2F2-test-no-lambda-2-1024x799.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2F2-test-no-lambda-2-1024x799.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now will never fail due to an external system not behaving because regardless of the real status, our view will always receive exactly what we want to test.&lt;/p&gt;

&lt;p&gt;We can improve the readability and organization of our mocks using the &lt;code&gt;lambda&lt;/code&gt; keyword. Its not often I find an appropriate use of &lt;code&gt;lambda&lt;/code&gt; so it’s worth highlighting. Here are the final tests rewritten:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2F2-test-lambda-1-1024x569.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshaneoneill.io%2Fwp-content%2Fuploads%2F2018%2F12%2F2-test-lambda-1-1024x569.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Summarize
&lt;/h2&gt;

&lt;p&gt;We want to capture our API responses under test. So we used unittest. However, sometimes our API is dependent on other systems, causing a dependence and a potential to fail intermittently. To solve this issue, we use patch. Patch allows us to usurp the underlying functions, and implement our own test functions. Then, we used the lambda keyword to define our mocks inline for better readability.&lt;/p&gt;

&lt;p&gt;Thanks for reading and Happy Hacking!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
