<?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: mistermocha</title>
    <description>The latest articles on DEV Community by mistermocha (@mistermocha).</description>
    <link>https://dev.to/mistermocha</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%2F16103%2Fcbab1dd8-b8ba-4471-aa63-46c3b1bfddcc.jpeg</url>
      <title>DEV Community: mistermocha</title>
      <link>https://dev.to/mistermocha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mistermocha"/>
    <language>en</language>
    <item>
      <title>In defense of Cat5e</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Mon, 16 Oct 2017 18:01:41 +0000</pubDate>
      <link>https://dev.to/mistermocha/in-defense-of-cat5e-7l3</link>
      <guid>https://dev.to/mistermocha/in-defense-of-cat5e-7l3</guid>
      <description>&lt;p&gt;A few years ago, we bought our house. This was my first house that I owned instead of rented. I was super excited to run patch cables through the walls, set up a networks switch and patch panel, and have keystone jacks strategically placed throughout the house. It was a silly engineering fantasy to have a garden of cables growing in a closet somewhere.&lt;/p&gt;

&lt;p&gt;I dug in and did my research on what I should get and what I should buy for everything. I counted rooms and did a bulk order on Monoprice for RJ45 connectors. I did found an excellent crimper and stripper in a pack on Amazon. I invested in ENT tubing (a.k.a. "Smurf" tubing, because of how blue it is) and spent my first weekend in the house running it behind all the drywall. I then got to looking at cable. I chose Cat5e.&lt;/p&gt;

&lt;p&gt;Now mind you, Cat5e was not an arbitrary choice. There were several factors which ultimately settled me on this choice. I still get flack from friends and colleagues for this choice, but I'm still happy with it and here's why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Cost&lt;/em&gt;: I initially saw how much more Cat6/6a cost than Cat5e. Cat7 was barely tickling the market when I started on this journey so I left it off my list. Generally speaking, the best prices I found at the time showed Cat6 costing about 50% more than Cat5e when buying spools. Since this was the biggest purchase across everything, and I was trying to be a responsible homeowner, I decided to interrogate and justify the added expense of higher quality cable.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Speed&lt;/em&gt;: Cat5e is rated to cap out at 1GbE whereas Cat6 and "better" cables are rated for 10GbE. Even now, years after my installation, my ISP isn't capable of providing 1GbE to my house, so higher quality cables provide no benefit there. 
Source: &lt;a href="https://www.digitaltrends.com/computing/differences-between-ethernet-cables/"&gt;https://www.digitaltrends.com/computing/differences-between-ethernet-cables/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Signal Quality&lt;/em&gt;: 1GbE, or officially 1000BASE-T, sets the standard for signal degradation at 100m. The longest run in my house is under 15m. If I were running to a property across the street, I might consider something higher quality.  Source: &lt;a href="https://en.wikipedia.org/wiki/Gigabit_Ethernet#1000BASE-T"&gt;https://en.wikipedia.org/wiki/Gigabit_Ethernet#1000BASE-T&lt;/a&gt; ... Similarly, the next biggest expense in my install was the switch, and 10GbE switches were similarly more expensive than 1GbE switches as well.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Ease of Installation&lt;/em&gt;: Cat6 cables have a plastic divider down the length of the cable. Cat6a got rid of this divider, but increased the wire gauge. Both are far less pliable than Cat5e and the divider makes crimping a major pain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Considering all this, I chose to adopt Cat5e. I decided that for now I did not want to invest the extra coin into the higher standard. It ultimately wasn't worth it for me until they could run faster than 1GbE to my premises. If I were serving production traffic from my home and needed rapid synchronization of cache across racks, then sure, but I have a single server in my closet and a couple raspberry pi devices. &lt;/p&gt;

&lt;p&gt;If for some reason I have the motivation to replace my cables, then I'm all set with my smurf tubes. 3/4" was sufficient to support redundant cables, so each drop has two runs. I hardwired my wife's desktop, a few entertainment devices (e.g., the Roku), and the "docking station" (series of dongles) for my workstation. The bulk of network traffic in my house now happens over hard wires instead of wireless, and all the cables are buried behind the drywall.&lt;/p&gt;

&lt;p&gt;I decided to write this because of two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The recently discovered flaw in WPA2 (&lt;a href="https://www.wired.com/story/krack-wi-fi-wpa2-vulnerability/"&gt;https://www.wired.com/story/krack-wi-fi-wpa2-vulnerability/&lt;/a&gt;) is yet another example that wired is inherently more secure. Broadcasting everything you're doing is far less safe than sending signals down a wire.&lt;/li&gt;
&lt;li&gt;Every time I write down Cat5e, there's someone who jumps up and says "Why not Cat6/6a/7 or fiber?" So now I can copy-paste a link to this write-up and have a canned response.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>networking</category>
      <category>security</category>
    </item>
    <item>
      <title>A Very Brief Evolution of SRE</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Wed, 31 May 2017 23:12:14 +0000</pubDate>
      <link>https://dev.to/mistermocha/a-very-brief-evolution-of-sre</link>
      <guid>https://dev.to/mistermocha/a-very-brief-evolution-of-sre</guid>
      <description>

&lt;p&gt;&lt;em&gt;This is an opinion piece I'm writing for fun. Please don't take this seriously.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'm an SRE. I've been a SysAdmin. I've been in Operations. I've been in tech support. I've been in this industry a long enough time to see the relationships between these titles and the tasks therein.&lt;/p&gt;

&lt;p&gt;Here's my short list of the evolution of SRE, showing how technology evolves and titles mean things. For me, this has been a career evolution, as I've moved generally through these titles as I worked through my career, but titles like "DevOps" and "SRE" weren't as common until later in my career (earlier in history). Note that each title will likely include the tasks from any and all titles before/above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tech Support&lt;/strong&gt;: The person you call when the webserver is down&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt;: knows when to restart the webserver (not likely to have access though)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SysAdmin&lt;/strong&gt;: restarts the webserver&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: writes a fancy script to restart all the webservers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SRE&lt;/strong&gt;: turns the fancy script into a service that restarts the webservers for you, which requires a webserver that someone has to restart at some point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Wednesday people!&lt;/p&gt;


</description>
      <category>sredevopssysadmin</category>
    </item>
    <item>
      <title>Python unit testing with Mock - Part Two</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Wed, 10 May 2017 20:15:00 +0000</pubDate>
      <link>https://dev.to/mistermocha/python-unit-testing-with-mock---part-two</link>
      <guid>https://dev.to/mistermocha/python-unit-testing-with-mock---part-two</guid>
      <description>&lt;h1&gt;
  
  
  How do you write tests?
&lt;/h1&gt;

&lt;p&gt;Python has many frameworks for writing tests, but ultimately they come down to collecting functions whose names begin with &lt;code&gt;test&lt;/code&gt; from an organized namespace. This article, being the sequel to "Python unit testing with Mock - Part One", isn't here to help organize your namespace (for the record, &lt;a href="https://docs.pytest.org/en/latest/"&gt;that's best done with &lt;code&gt;py.test&lt;/code&gt;&lt;/a&gt; which deserves its own glorious article).&lt;/p&gt;

&lt;p&gt;This article will show you how to write tests, and understand when and how to use the &lt;code&gt;mock&lt;/code&gt; module. It's one thing to just know "oh, that's how a mock works" but another entirely to know how to apply a mock to test your code properly. This article seeks to address that bit of knowledge.&lt;/p&gt;

&lt;h1&gt;
  
  
  How do I actually use mock now?
&lt;/h1&gt;

&lt;p&gt;Having all the tools doesn't necessarily mean knowing how to use them! When I first read the documentation for mock, I found it baffling. I really didn't understand how to put it all together. Here's some practical-ish examples of how to read your code and decide how to craft a test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example #1
&lt;/h2&gt;

&lt;p&gt;Let's touch upon an example similar to one we witnessed in the previous article. Here's a function that uses the &lt;a href="http://docs.python-requests.org/en/master/"&gt;&lt;code&gt;requests&lt;/code&gt; library&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_example&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'http://example.com/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, we have an instantiated request &lt;code&gt;r&lt;/code&gt; and view the &lt;code&gt;status_code&lt;/code&gt; property on the object. The &lt;code&gt;requests&lt;/code&gt; object is what talks to the outside world, but our code that implements &lt;code&gt;requests&lt;/code&gt; is what we want to test out.&lt;/p&gt;

&lt;p&gt;What we need to do is replace the return of &lt;code&gt;requests.get()&lt;/code&gt; with a mock. We're going to use a variety of the features of mock to replace it properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'requests.get'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autospec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_example_passing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mocked_get&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="n"&gt;mocked_req_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;mocked_req_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
   &lt;span class="n"&gt;mocked_get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mocked_req_obj&lt;/span&gt;
   &lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_example&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

   &lt;span class="n"&gt;mocked_get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;mocked_get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'http://example.com/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's walk through this to show what we're doing and why&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mock.patch&lt;/code&gt; patches &lt;code&gt;requests.get&lt;/code&gt; with a mock (meaning the &lt;code&gt;get&lt;/code&gt; function in the &lt;code&gt;requests&lt;/code&gt; library) using &lt;code&gt;autospec=True&lt;/code&gt; to match the signature of &lt;code&gt;requests.get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This patched object is made available as the first argument, which we receive in &lt;code&gt;mocked_get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We know we need to create a return to feed into &lt;code&gt;r&lt;/code&gt; in the code we're testing, so we create &lt;code&gt;mocked_request_object&lt;/code&gt;. In the code, we only examine the &lt;code&gt;status_code&lt;/code&gt;, so we declare that value - in this case, 200&lt;/li&gt;
&lt;li&gt;We then assign &lt;code&gt;mocked_request_object&lt;/code&gt; to the &lt;code&gt;return_value&lt;/code&gt; of &lt;code&gt;mocked_get&lt;/code&gt;, which is what will be returned when the patched function gets called&lt;/li&gt;
&lt;li&gt;Since we know the function effectively returns boolean, we can call a simple &lt;code&gt;assert&lt;/code&gt; on the function's return.&lt;/li&gt;
&lt;li&gt;To ensure the mock was called properly, we call the built-in assertion methods. &lt;code&gt;assert_called&lt;/code&gt; ensures the mocked &lt;code&gt;requests.get&lt;/code&gt; was actually called, and &lt;code&gt;assert_called_with&lt;/code&gt; checks the args. We can do similar introspection on the returned mock as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now this seems like a lot of test code (8 lines) for a small function (3 lines). It's often the case that a python unit test could have more LOC than the code it's testing. We don't even have complete test coverage, because 200 isn't the only valid response! Test code should write faster than product code, since it doesn't involve the same level of thought or design.&lt;/p&gt;

&lt;p&gt;This was only an example to show how to design a mock to replace an interface library. The principle here is to understand what part of your code talks to the public and do the minimum to replace it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example #2
&lt;/h2&gt;

&lt;p&gt;It's called a "mock" because it can pretend to be something real. We know our code, so now we can examine our code to determine how to use a mock. We have a good overview of the parts and usage, but implementation is everything.&lt;/p&gt;

&lt;p&gt;When looking at your code and deciding how to test, you want to run a test that ensure the desired outputs and side effects happen when executing your code. Ideally, you want to ensure at least one, if not multiple, tests happen for every line of code. So, tests should cover each function and mocks should prevent those functions from hitting the outside world.&lt;/p&gt;

&lt;p&gt;Here's an example of a module that writes to a database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DBWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DBLibrary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"INSERT INTO mytable SET mystring = '{}'"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"DELETE FROM mytable WHERE mystring = '{}'"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this module uses a made-up API module called &lt;code&gt;DBLibrary&lt;/code&gt;. Let's assume &lt;code&gt;DBLibrary&lt;/code&gt; is an external library that is already trusted to work. We know that such a library has a couple features to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is outside of this code&lt;/li&gt;
&lt;li&gt;It may already have its own tests - even if it doesn't, refer to previous point&lt;/li&gt;
&lt;li&gt;It can change state in a database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are all perfectly qualifying characteristics for something ripe for being mocked away. Now that said, we need to write tests for &lt;code&gt;DBWriter&lt;/code&gt;, which is OUR library. Let's look at the behaviors of this module. The effective &lt;code&gt;save&lt;/code&gt; and &lt;code&gt;drop&lt;/code&gt; actions do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare the sql statement&lt;/li&gt;
&lt;li&gt;Write the statement to the database&lt;/li&gt;
&lt;li&gt;Increment the counter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these behaviors are elements to test, but we still want to protect the database from any writes. How do we do it?&lt;/p&gt;

&lt;h3&gt;
  
  
  Model #1 - Patch &lt;code&gt;commit_to_db&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The actual writing to the database happens in the &lt;code&gt;commit_to_db&lt;/code&gt; function. We can replace this function with a mock and replicate the behavior. It does two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increments the counter&lt;/li&gt;
&lt;li&gt;Calls &lt;code&gt;self.db.commit&lt;/code&gt; to commit to the database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how you write that test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'dbwriter.DBWriter.commit_to_db'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autospec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_commit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DBWriter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fake_commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="n"&gt;mock_commit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;side_effect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fake_commit&lt;/span&gt;

    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;mock_commit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"INSERT INTO mytable SET mystring = 'Hello World'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the namespace assumes &lt;code&gt;DBWriter&lt;/code&gt; is in a filename &lt;code&gt;dbwriter.py&lt;/code&gt; or otherwise such organized module. You'll need to know your module's namespace for this patch to work.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@mock.patch&lt;/code&gt; decorator replaces the &lt;code&gt;commit_to_db&lt;/code&gt; function in place. We declare &lt;code&gt;autospec=True&lt;/code&gt; which matches the signature to ensure &lt;code&gt;commit_to_db&lt;/code&gt; is called correctly.&lt;/p&gt;

&lt;p&gt;We also add a &lt;em&gt;side effect&lt;/em&gt;, which is something that happens besides containment of a return value. &lt;code&gt;commit_to_db&lt;/code&gt; doesn't just write to a database, but it also increments a counter. Adding this behavior in as a side effect ensures this action doesn't omitted when the mock is called. Since we're replacing the function with a mock, the actual code of that function never gets called.&lt;/p&gt;

&lt;p&gt;Calling the mock in this way gives us the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introspection into &lt;code&gt;commit_to_db&lt;/code&gt;: we can call mock's introspection tools to ensure that the function is receiving the right arguments&lt;/li&gt;
&lt;li&gt;Insulation from write actions: We don't bring up an equivalent action to &lt;code&gt;self.db.commit&lt;/code&gt; in the test, which means we've removed the action that can potentially write to the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, this comes with some drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lost introspection into &lt;code&gt;self.db.commit&lt;/code&gt;: We may be insulated, but we don't know if we're calling the function right. &lt;/li&gt;
&lt;li&gt;No exercise of the actual &lt;code&gt;commit_to_db&lt;/code&gt;: We're re-writing the non-state-changing code in our unit test! If that line were something more complex, or if there was a ton of code in here, we'd have to reimplement it all!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's try another approach:&lt;/p&gt;

&lt;h3&gt;
  
  
  Model 2: Patch self.db.commit
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'dbwriter.DBLibrary'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autospec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_dblib&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DBWriter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mock_dblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"INSERT INTO mytable SET mystring = 'Hello World'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we're patching &lt;code&gt;DBLibrary&lt;/code&gt; directly in the &lt;code&gt;dbwriter&lt;/code&gt; namespace, which prevents patching that library elsewhere. This is direct surgery.&lt;/p&gt;

&lt;p&gt;Doing this replaces the &lt;code&gt;DBLibrary&lt;/code&gt; with a mock. Again, &lt;code&gt;autospec=True&lt;/code&gt; will ensure that calls to &lt;code&gt;commit&lt;/code&gt; and any other implemented methods will respect calls that match the given signatures of those methods. &lt;/p&gt;

&lt;p&gt;Here's what we get for testing this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insulation from the database: same as before, just in a different way&lt;/li&gt;
&lt;li&gt;Introspection into how we use the &lt;code&gt;DBWriter&lt;/code&gt; module: notice w're calling &lt;code&gt;assert_called_with&lt;/code&gt; and determining how we call the function that actually writes to the database, with the same protections&lt;/li&gt;
&lt;li&gt;Full exercise of every line of code in our test: only external code is replaced by the mock&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's what we lose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introspection into the parent function: We don't see how the function is called by it's neighbors inside the module&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Luckily, both tests are valid and can be done in parallel. If you're not sure which test to write, write both! You can never have too much test coverage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example #3
&lt;/h2&gt;

&lt;p&gt;I'm often asked about how to mock something that gets instantiated multiple times in an object. A class coming up will instantiate a module twice (e.g., two separate database connections). Just naÃ¯vely patching the base module may give you one instance that gets used twice, or just spit out default mocks that don't help you at all. It took me some research and experimentation to figure out how to do this.&lt;/p&gt;

&lt;p&gt;So, here's some example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;some.library&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AnotherThing&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AnotherThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AnotherThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_that&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_more&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;got_it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_it&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;that_too&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do_it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;got_it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;that_too&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;self.this&lt;/code&gt; and &lt;code&gt;self.that&lt;/code&gt; are both instances of &lt;code&gt;AnotherThing&lt;/code&gt; but initialized with &lt;code&gt;this&lt;/code&gt; and &lt;code&gt;that&lt;/code&gt; respectively. They're both used throughout the code. We want to patch out &lt;code&gt;AnotherThing&lt;/code&gt; so we can decidedly test our code out.&lt;/p&gt;

&lt;p&gt;We can take two approaches here, with some caveats. Ultimately, the options you choose have to do with how you implement the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach #1: Patch &lt;code&gt;this&lt;/code&gt; and &lt;code&gt;that&lt;/code&gt; directly
&lt;/h3&gt;

&lt;p&gt;This approach works if you know for a fact that &lt;code&gt;__init__&lt;/code&gt; for MyClass and the initialization of &lt;code&gt;AnotherThing&lt;/code&gt; didn't actively change state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_my_class&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fake this"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"fake that"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec_set&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'some.library.AnotherThing'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec_set&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'some.library.AnotherThing'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do_this&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do_that&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;do&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this approach, we instantiate and then patch the result in place. It's a straightforward approach that only really works when you know that your code will fit the pattern. It's highly unlikely that state changes happen in &lt;code&gt;__init__&lt;/code&gt; code, but entirely possible. It's also more likely that instantiating &lt;code&gt;AnotherThing&lt;/code&gt; will be slow and expensive, even if it wasn't changing any external state. So this may be less confusing, it's not necessarily better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach #2: Mock Generator
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;AnotherThing&lt;/code&gt; returns an instance of &lt;code&gt;AnotherThing&lt;/code&gt;, so why not just change that action to spit out mocks?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'yourcode.AnotherThing'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autospec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_my_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_thing&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fake_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;spec_set&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'some.library.AnotherThing'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mock_thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;side_effect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fake_init&lt;/span&gt;

    &lt;span class="n"&gt;my_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fake this"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"fake that"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fake this"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;my_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;that&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fake that"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mock_thing&lt;/code&gt; is the in-namespace patch to replace &lt;code&gt;AnotherThing&lt;/code&gt;. We take advantage of using &lt;code&gt;side_effect&lt;/code&gt; to return a &lt;code&gt;spec_set&lt;/code&gt; mock. Now we've patched &lt;code&gt;AnotherThing&lt;/code&gt; using &lt;code&gt;mock.patch&lt;/code&gt; to spit out mocks that look like the objects it creates. All my patching happens before instantiation, so there's no modification of the code object being tested here.&lt;/p&gt;

&lt;p&gt;Additionally, the mocks permit introspection, so we get to test usage as well!&lt;/p&gt;

&lt;h1&gt;
  
  
  To mock or not to mock?
&lt;/h1&gt;

&lt;p&gt;There are times when it's necessary and appropriate to mock, and we've covered some examples of when it's absolutely necessary for the scope of a unit test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calling an API&lt;/li&gt;
&lt;li&gt;Connecting to a database&lt;/li&gt;
&lt;li&gt;Using the command line&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are some obvious examples to consider. Let's look at some less obvious ones, and a reason not to mock as well&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Don't mock the filesystem.&lt;/em&gt; A great explanation why can be found at &lt;a href="https://moshez.wordpress.com/2016/12/02/dont-mock-the-unix-filesystem/"&gt;Moshe Zadka's blog&lt;/a&gt;. The filesystem is huge. Really huge. Trying to mock it out would be a huge awful mock with a lot of edges to replace. If you need a filehandle, make a fake one using &lt;code&gt;StringIO&lt;/code&gt;, or just write to a temporary file using &lt;code&gt;tempfile&lt;/code&gt;. Both are standard library and easy to implement.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Acceptance tests&lt;/em&gt; are how we ensure our code works with the rest of enterprise imports and third-party libraries. Don't ignore that these libraries could change in unexpected ways. Write your unit tests, but write some acceptance tests for occasional runs&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Integration tests&lt;/em&gt; by definition are there to test how your code integrates with the rest of the environment. You will want to occasionally test the real API call at some point.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Mock slow libraries&lt;/em&gt; to speed up your unit tests. Some code is expensive to instantiate, but doesn't really need to come up fully for a test. Abstract this away.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;If you're writing tests, you're doing it right!&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>testing</category>
      <category>mock</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python unit testing with Mock - Part One</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Wed, 10 May 2017 20:14:02 +0000</pubDate>
      <link>https://dev.to/mistermocha/python-unit-testing-with-mock---part-one</link>
      <guid>https://dev.to/mistermocha/python-unit-testing-with-mock---part-one</guid>
      <description>&lt;h1&gt;
  
  
  What is this article?
&lt;/h1&gt;

&lt;p&gt;Months ago, I was asked to give a talk to the &lt;a href="https://www.meetup.com/PSPPython/"&gt;Puget Sound Python Meetup group&lt;/a&gt;. This is the article-rendering of that talk. This talk makes some assumptions that you know the basics about how to actually write a test for python code and need to know how to embed mocks to keep your code safe.&lt;/p&gt;

&lt;p&gt;When I was asked to give this talk, I was (and still am) responsible for a big script that talks to tons of external services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;kerberos&lt;/li&gt;
&lt;li&gt;git&lt;/li&gt;
&lt;li&gt;aurora&lt;/li&gt;
&lt;li&gt;package repo&lt;/li&gt;
&lt;li&gt;jira&lt;/li&gt;
&lt;li&gt;email&lt;/li&gt;
&lt;li&gt;shared libraries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This posed the question of how do I test my code without touching these services?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note this article is going to be exclusive to the mock test library. Structuring overall tests is another topic and deserves its own article, which I have yet to write at this point. When I get there, I'll let you know!&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is testing?
&lt;/h1&gt;

&lt;p&gt;Testing makes sure your code behaves as expected by &lt;strong&gt;running your code&lt;/strong&gt; and observing the results. By actually running chunks code, it's the most assured way to know that it runs, how it runs, and that it does what is expected.&lt;/p&gt;

&lt;p&gt;When writing tests, you can categorize into one of three approximate buckets.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit: Just this small part&lt;/li&gt;
&lt;li&gt;Integration: When all the parts talk to each other and included parts&lt;/li&gt;
&lt;li&gt;Acceptance: When the whole app talks to everything else&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article will focus on the best use of mocks, and that's for writing unit tests. Mocks could potentially have a place in integration tests, and the application wouldn't change.&lt;/p&gt;

&lt;p&gt;Some code is easy to run to make sure it does what is expected. Here's a quick function and unit test that you might see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;### code ###
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;double_up&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="c1"&gt;### test ###
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_double_up&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;double_up&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unit test simply runs the code and compares the received output to the expected output. Just like previously stated, &lt;strong&gt;testing means running your code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now for some fun...&lt;/p&gt;

&lt;h2&gt;
  
  
  How would you test this
&lt;/h2&gt;

&lt;p&gt;How would you test this block of code without actually deleting anything?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wipe_directory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'rm'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'-rf'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'We had a fail'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How would you test this block of code without actually deleting anything?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delete_everything&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'http://example.com/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'delete'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'everything'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'autocommit'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'All things have been deleted'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Got an error: {}'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How would you test this block of code without committing any change to the database?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DBWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DBLibrary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"INSERT INTO mytable SET mystring = '{}'"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"DELETE FROM mytable WHERE mystring = '{}'"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;commit_to_db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to actually execute these blocks of code will modify external state. These examples either execute a shell command, talk to an API, or connect to a database, all of which are actions that happen outside of the execution space of the written code. &lt;/p&gt;

&lt;h1&gt;
  
  
  What is mocking?
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;unittest.mock&lt;/code&gt; is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://docs.python.org/3/library/unittest.mock.html"&gt;https://docs.python.org/3/library/unittest.mock.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As stated earlier, mocks are primarily used for unit testing. There may be some place in integration testing, highly unlikely in acceptance testing.&lt;/p&gt;

&lt;p&gt;Why should you mock in your code?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit test safely: stop the state-changing parts so you can actually run tests&lt;/li&gt;
&lt;li&gt;Write better code: a lovely side-effect of writing thorough tests&lt;/li&gt;
&lt;li&gt;Isolation: create walls between your code and "not-your-code" for safer tests&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About the mock library
&lt;/h2&gt;

&lt;p&gt;Mock objects intend to replace another part of code so that it pretends to be that code. In this example, let's assume that &lt;code&gt;visible_method&lt;/code&gt; is a function inside of &lt;code&gt;MyClass&lt;/code&gt; that runs &lt;code&gt;sub_method&lt;/code&gt;. Now let's assume that &lt;code&gt;sub_method&lt;/code&gt; does something we don't want to run in the scope of this test. We simply replace &lt;code&gt;sub_method&lt;/code&gt; with a &lt;code&gt;Mock&lt;/code&gt; instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;unittest.mock&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;mycode&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_myclass&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
   &lt;span class="n"&gt;my_object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;my_object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sub_method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;my_object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visible_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;my_object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sub_method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"arg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"that"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This isolates one function from talking to another function within the same class. A mock becomes a synthetic code object that simply runs and returns another Mock object successfully every time (with some exceptions). Unless you're expecting something specific, a raw mock object can drop in almost anywhere.&lt;/p&gt;

&lt;p&gt;However, most functions expect something specific, and this is the main purpose of a mock. You can shape a mock up to look like anything. Prop it up to behave like a specific function, module, class, object, etc. and you'll avoid having to do dicey things like call a database API. We'll get more on this in some of the next examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using mock.patch
&lt;/h2&gt;

&lt;p&gt;Sometimes, the dropping-in of an object is not as easy as directly replacing a function in a namespace. Sometimes the object to replace is much deeper down. By adding the &lt;code&gt;@mock.patch&lt;/code&gt; decorator to a function, you can replace an object directly in the namespace with a mock.&lt;/p&gt;

&lt;p&gt;Here's a function that has &lt;code&gt;subprocess.Popen&lt;/code&gt; available in its namespace. We can't directly replace it as we did in the prior example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count_the_shells&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'ps'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'-a'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'We had a fail'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s"&gt;"-bash"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;subprocess.Popen&lt;/code&gt; is inside the function, but it's available in the namespace of the function. By &lt;code&gt;@mock.patch&lt;/code&gt;ing, we replace &lt;code&gt;subprocess.Popen&lt;/code&gt; with a mock. The &lt;code&gt;PIPE&lt;/code&gt;s no longer are used because of the replacement, so we don't need to patch them.&lt;/p&gt;

&lt;p&gt;By examining the action in this function, we see its surfaces that talk to the &lt;code&gt;subprocess&lt;/code&gt; object that gets returned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Popen&lt;/code&gt; runs a command line execution and returns a subprocess object. In this case, &lt;code&gt;p&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p.wait()&lt;/code&gt; blocks until it gets back the shell's exit code and returns it as an integer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p.stdout&lt;/code&gt; is a filelike object that captures STDOUT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our mock then needs to have these surfaces as well. We don't need to completely replace the behavior of a &lt;code&gt;Popen&lt;/code&gt; call, we just need to make the return value &lt;em&gt;look like&lt;/em&gt; the return value of a &lt;code&gt;Popen&lt;/code&gt; call and the methods that we use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'subprocess.Popen'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_count_the_shells&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mocked_popen&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;mocked_popen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'testps.out'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mocked_popen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;count_the_shells&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at what's going on in this test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@mock.patch&lt;/code&gt; decorator replaces &lt;code&gt;subprocess.Popen&lt;/code&gt; with a mock object. That gets passed in as the first argument in the test function. The test function receives it as &lt;code&gt;mocked_popen&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Popen&lt;/code&gt; call returns a subprocess object. We're now amending the &lt;code&gt;return_value&lt;/code&gt; of that object by applying behavior to &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;wait&lt;/code&gt;, which get used in the function&lt;/li&gt;
&lt;li&gt;Now when &lt;code&gt;count_the_shells&lt;/code&gt; is executed, it calls the mock instead of &lt;code&gt;Popen&lt;/code&gt; and gets back expected values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We've effectively replaced &lt;code&gt;subprocess.Popen&lt;/code&gt; with a mock that behaves like that object as far as the function is concerned. Now you know how to replace a method with a mock that actually mocks something!&lt;/p&gt;

&lt;p&gt;But wait, there's more!&lt;/p&gt;

&lt;h2&gt;
  
  
  Spec and Autospec
&lt;/h2&gt;

&lt;p&gt;What if your mock could just be pointed at a module and automatically look like that module? What if it could be prompted to just respond to functions already defined in a module with mocks? Seems like just the python-majick we have come to expect and love, right? Well, read on!!&lt;/p&gt;

&lt;p&gt;A mock will have its own built-ins, but any other call received will simply return another mock. This plastic behavior looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this_is_never_assigned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'mock.this_is_never_assigned()'&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'4422797328'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents accidental calls from blowing up your code, but, leaves room for a lot of error. You can obtain safer instantiation by autospeccing - make the mock behave like more like the thing you're mocking. Passing the &lt;code&gt;spec&lt;/code&gt; argument tells the mock to closely behave like another. Mocks instantiated with &lt;code&gt;spec=RealObject&lt;/code&gt; will pass &lt;code&gt;isinstance(the_mock, RealObject)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OrderedDict&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;OrderedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OrderedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="s"&gt;'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;spec&lt;/code&gt; also affords protection, preventing calls to undeclared attributes. You can declare any additional attributes you wish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;OrderedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this_does_not_exist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"/opt/twitter/lib/python2.7/site-packages/mock.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;658&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;__getattr__&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mock object has no attribute %r"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt; &lt;span class="s"&gt;'this_does_not_exist'&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this_does_not_exist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"this exists now"&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this_does_not_exist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;exists&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instantiating with &lt;code&gt;spec_set&lt;/code&gt; is an even &lt;em&gt;stricter&lt;/em&gt; spec. This prevents amending missing attributes. Attempts to define undeclared attributes will fail on &lt;code&gt;AttributeError&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec_set&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;OrderedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;this_does_not_exist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"o no you didn't"&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"/opt/twitter/lib/python2.7/site-packages/mock.py"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;761&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;__setattr__&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mock object has no attribute '%s'"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt; &lt;span class="s"&gt;'this_does_not_exist'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;create_autospec&lt;/code&gt; function is even stricter. This will generate mock functions defined to spec that will enforce function signatures, meaning if the original function expects two positional and one keyword argument, then the mocked function will also expect two positional and one keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_autospec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MagicMock&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'mock()'&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'4493382480'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mymock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"just one"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;string&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;myfunc&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;takes&lt;/span&gt; &lt;span class="n"&gt;exactly&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;given&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Appropriate use of spec can help you write cleaner code and catch typos. Since a spec_set locks out unimplemented methods, your test code will fail if it has a typo too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Thing'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assret_called_once_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# typo of "assert" passes because mock objects are forgiving
&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;urllib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assret_called_with&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt; &lt;span class="s"&gt;'assret_called_with'&lt;/span&gt;
&lt;span class="c1"&gt;# since "assret_called_with" is a typo, it's not declared. Proper exception caught!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, in python 3, you can &lt;code&gt;name&lt;/code&gt; your mocks, which shows in the repr of the mock. This is useful for debugging! You can see exactly where in your code your mock blew up because you know which of your mocks was misused!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introspection
&lt;/h2&gt;

&lt;p&gt;Mocks come with a long list of built-in functions that provide a level of introspection into the life of the mock. There are storage attributes that automatically store every function call in the mock. These are built-in attributes and functions for inspecting how the mock was used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;called&lt;/code&gt; - boolean, true if ever called&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;call_count&lt;/code&gt; - integer, number of times called&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;call_args&lt;/code&gt; - mock.call() object with args from last call&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;call_args_list&lt;/code&gt; - list of mock.call() with all args ever used&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;method_calls&lt;/code&gt; - track calls to methods and attributes, and their descendents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mock_calls&lt;/code&gt; - list of &lt;em&gt;all&lt;/em&gt; calls to the mock object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have a mock object passing deep through your module, and you want to make sure it's actually referenced and not missed over, the &lt;code&gt;called&lt;/code&gt; method is your friend. Similarly, &lt;code&gt;call_count&lt;/code&gt; can confirm how many times, and &lt;code&gt;call_args&lt;/code&gt; confirms what args were used the last time.&lt;/p&gt;

&lt;p&gt;Now since this is for unit tests, assertions are the common way to write tests. To make this better, mock has built-in assertion functions that reference the aforementioned attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;assert_called&lt;/code&gt; - if ever called&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_called_once&lt;/code&gt; - if called exactly once&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_called_with&lt;/code&gt; - specific args used in the last call&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_called_once_with&lt;/code&gt; - specific args are used exactly once&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_any_call&lt;/code&gt; - specific args used in any call ever&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_has_calls&lt;/code&gt; - like "any_call" but with multiple calls&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_not_called&lt;/code&gt; - has never been called&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So you don't have to wrap up booleans in if statements, you can just use the built-ins. &lt;/p&gt;

&lt;h1&gt;
  
  
  Summing Up - Part One
&lt;/h1&gt;

&lt;p&gt;In this article, we covered the usage and features of the &lt;code&gt;mock&lt;/code&gt; module in python. We discussed how to apply a mock to an existing test and how to adjust its behavior.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/mistermocha/python-unit-testing-with-mock---part-two"&gt;Python unit testing with Mock - Part Two&lt;/a&gt;, we'll cover how to review your code to decide what parts of your code require mocks, how to write them with real code samples, as well as some cases where you might want to mock, but shouldn't.&lt;/p&gt;

</description>
      <category>python</category>
      <category>testing</category>
      <category>mock</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Yes! You can plan your projects!</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Fri, 28 Apr 2017 22:28:41 +0000</pubDate>
      <link>https://dev.to/mistermocha/yes-you-can-plan-your-projects</link>
      <guid>https://dev.to/mistermocha/yes-you-can-plan-your-projects</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my first article here. I'm welcoming of feedback so I can make this better.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project planning. It's what makes engineers productive. You should definitely do it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What's that? You already do? You already know how to plan a project? Okay, what do you do first? Start coding or write a project plan? &lt;/p&gt;

&lt;p&gt;I was the same way too, once. I admit to just jumping in and coding my way into a corner. I thought I was saving time, but instead I burned up a ton of time. I'm writing this article to distill down what I went to two different universities to learn about, in conjunction with over a decade of industry experience, so congratulations! You've read my article and saved tens of thousands on tuition and caught up to my current career!&lt;/p&gt;

&lt;h1&gt;
  
  
  Terminology - not really just making up words
&lt;/h1&gt;

&lt;p&gt;For sake of this article, I'm going to use some simple terms that may equate to your place of business (or not-business, you might work on your own project, a non-profit, volunteer group, etc.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals - the things you kinda sorta know you definitely want
&lt;/h2&gt;

&lt;p&gt;In a workplace environment, the people/business/organization/etc will need to accomplish things. We'll call them &lt;strong&gt;goals&lt;/strong&gt;. Goals can be more nebulous, like &lt;em&gt;get more users&lt;/em&gt; or &lt;em&gt;speed up deploy time&lt;/em&gt;. Companies may call these something different, like "objectives." &lt;/p&gt;

&lt;p&gt;A measurable goal is just that, measurable. One can take a metric and review before and after some time and effort to see a result. Such goals may look something like these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce number of alerts&lt;/li&gt;
&lt;li&gt;Speed up deploy time&lt;/li&gt;
&lt;li&gt;Reduce CapEx budget&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An immeasurable goal is not something where a simple metric can be applied, but it is clear to a human observer that there has ben a change. Some aspects may be measured (more on that later), but the overall goal doesn't have a metric. Such goals make look something like these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improve operational overhead&lt;/li&gt;
&lt;li&gt;Become more proficient in Java&lt;/li&gt;
&lt;li&gt;Improve team morale&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Projects - The pieces of that goal that must be done
&lt;/h2&gt;

&lt;p&gt;When a goal is in mind, a &lt;strong&gt;project&lt;/strong&gt; can be defined to work towards that goal. Goals can be observed, but don't have to be measurable. Projects should absolutely be measurable. Additionally, a project should be generally time-scoped. Consider what you can get done in the next quarter/half/etc. This can be helpful during review cycles.&lt;/p&gt;

&lt;p&gt;Let's define a few projects based on a few goals from before:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce number of alerts:

&lt;ul&gt;
&lt;li&gt;Configure 50% of stateless services for automatic instance remediation&lt;/li&gt;
&lt;li&gt;Upgrade MySQL hosts from 1G to 10G NICs&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Speed up deploy time:

&lt;ul&gt;
&lt;li&gt;Migrate main backend service to SSDs&lt;/li&gt;
&lt;li&gt;Examine and implement service warmup settings in FooBarBaz service&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Become more proficient in Java

&lt;ul&gt;
&lt;li&gt;Complete 30 HackerRank challenges&lt;/li&gt;
&lt;li&gt;Make at least one "major" production code change in a Java repository&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some projects are incremental and will have a ratio of completion. Assume that you have six stateless services on your team, but you feel you can only handle three of them this quarter. Then your project can theoretically progress 1/3 at a time.&lt;/p&gt;

&lt;p&gt;When planning projects out that have a metric attached, it should be known how to measure that metric. Adding a metric may be a goal in and of itself. For &lt;em&gt;speed up deploy time&lt;/em&gt; goal, you may need to start measuring your actual deploy time before you can actually start. Draw that up as a project and get to it first!&lt;/p&gt;

&lt;p&gt;At Twitter, we use &lt;a href="https://en.wikipedia.org/wiki/OKR"&gt;OKRs&lt;/a&gt; to plan our work. Objectives are the word we use for the unstructured goals. Key Results are what we use to define projects as a measurable chunk of work to be done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tasks - the things that actually get done
&lt;/h2&gt;

&lt;p&gt;Projects are specific, but not necessarily a discrete unit of work. We'll call them &lt;strong&gt;tasks&lt;/strong&gt;. Tasks are specific units of work. Let's define a few tasks for some of our projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure 50% of stateless services for automatic instance remediation

&lt;ul&gt;
&lt;li&gt;Define standard alert set for our stateless services&lt;/li&gt;
&lt;li&gt;Bind service X to standard alert set&lt;/li&gt;
&lt;li&gt;Hook up service X to remediation platform&lt;/li&gt;
&lt;li&gt;Bind service Y to standard alert set&lt;/li&gt;
&lt;li&gt;Hook up service Y to remediation platform&lt;/li&gt;
&lt;li&gt;Bind service Z to standard alert set&lt;/li&gt;
&lt;li&gt;Hook up service Z to remediation platform&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Upgrade MySQL hosts from 1G to 10G NICs

&lt;ul&gt;
&lt;li&gt;Acquire 2 10G hosts for testing&lt;/li&gt;
&lt;li&gt;Test production services against 10G hosts&lt;/li&gt;
&lt;li&gt;Fix cmalloc startup bug&lt;/li&gt;
&lt;li&gt;Acquire 8 10G hosts for production rollout&lt;/li&gt;
&lt;li&gt;Production cutover&lt;/li&gt;
&lt;li&gt;Set up deploy config changes&lt;/li&gt;
&lt;li&gt;Set up service config changes&lt;/li&gt;
&lt;li&gt;Run production deploy&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes, these specific units of work may involve even smaller units of work. Try to avoid task inception though (sub-tasks within sub-tasks). Hours can be wasted managing projects too deeply, but it's sometimes necessary. Our &lt;em&gt;Production cutover&lt;/em&gt; here has several steps, but they're all in the same goal. It's your call if you want to run those tasks hierarchically.&lt;/p&gt;

&lt;p&gt;You'll perhaps find that some of the tasks from your projects really belong to other colleagues or teams. For example, host acquisition may take several days or weeks depending on approvals and provisioning - specifically, work done by other people. Now you've discovered &lt;strong&gt;blocking tasks&lt;/strong&gt;, and those are good to identify and kick off right away. You can't test until you get those test hosts, so order them right away and switch over to another task while you wait.&lt;/p&gt;

&lt;h1&gt;
  
  
  Project management - be your own boss?
&lt;/h1&gt;

&lt;p&gt;Now, how do you manage these things going forward? Project planning leads into project management. Now if you're a reasonably-sized tech company, you might use Jira for managing your work items. Goals are something that you have stuck to the wall on a poster, where as projects and tasks equate to Jira epics and stories/tasks.&lt;/p&gt;

&lt;p&gt;Atlassian, the company that makes Jira, &lt;a href="https://www.atlassian.com/agile/delivery-vehicles"&gt;published an article&lt;/a&gt; that gets into the details of how to manage projects using epics and stories/tasks. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting things done - look at what you wrote down!
&lt;/h2&gt;

&lt;p&gt;When projects are planned this way, as units of work that stack up towards a given goal, then the work you're doing is clearly trending towards that goal. Now I know you probably don't want to write out another task in Jira, or Asana, or whatever else you may have at hand, but it's definitely worth it to have and use these tools. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sprint planning - just look at your list and decide what's next
&lt;/h2&gt;

&lt;p&gt;Regardless of the tool you use, USE IT! Look at your lists of tasks. Jira lets you create a thing called a &lt;strong&gt;Kanban board&lt;/strong&gt;, that's straight out of any Agile textbook. You have columns for tasks that are not yet worked on, currently working, and done.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;backlog&lt;/th&gt;
&lt;th&gt;in progress&lt;/th&gt;
&lt;th&gt;complete&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Order Hardware&lt;/td&gt;
&lt;td&gt;Analyze existing setup&lt;/td&gt;
&lt;td&gt;Kickoff meeting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Upgrade Jenkins&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Better monitoring&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can IMMEDIATELY see the state of what's going on with your projects, which is a fine feeling. When the manager says "How's the Jenkins migration going?" She can look at your Kanban board and see your progress. Amazing! It's also a great feeling to see your backlog gradually grind down less and less.&lt;/p&gt;

&lt;h2&gt;
  
  
  Communication is your friend
&lt;/h2&gt;

&lt;p&gt;OK, so you've done all the tasks for your project, and it's definitely working towards your goal. Now what? How does anyone know what you did? &lt;em&gt;You tell them!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most of us in the tech industry have weekly one-on-one meetings with our managers. This is where we tell our manager what we have on our plate and where we're blocked. Got a task that bob-from-accounting has been camping on since you got it to him? Tell your manager that you're waiting on it, and she should be the one to go try to move it along. Not enough budget for those SSD's? Discuss with your manager what the budget is like.&lt;/p&gt;

&lt;p&gt;You should also let your manager know what you got done this week. This is how you build your reputation as a worthwhile engineer. You can say "hey, that goal that the CEO/VP/etc. set? Well, I got us two steps closer to that because of X!" She'll see how thoughtful you were with your planning and trust that you'll continue to work on the rest of your tasks.&lt;/p&gt;

&lt;p&gt;The continued reminder of your successes and your intent on blockers will reinforce that perception of your ability to get things done. Then people won't mind as much that you surf reddit while your project is compiling.&lt;/p&gt;

&lt;p&gt;Share your project work with your colleagues, but avoid bragging too much. Nobody likes a know-it-all, but nobody likes it when someone seems incompetent either. Moderation is key. A routine (bi)weekly email summarizing accomplishments is always a good thing. If nobody else on your team is doing it, start it and include team members' accomplishments. If someone else &lt;em&gt;is&lt;/em&gt; doing one, then make sure your completed tasks get mentioned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brand is all about perception.&lt;/strong&gt; It's about how others feel about you. You may not have direct control, but you do have an influence. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prototyping - buyer beware!
&lt;/h2&gt;

&lt;p&gt;Have you ever seen that one tool or service that nobody wants to touch because it never should've been shipped and nobody knows who owns it now? This is often the byproduct of somebody's hackathon project or a prototyped tool that was never really well planned. &lt;strong&gt;Prototyping&lt;/strong&gt; is just writing a just-barely-minimum-viable-product to show that the idea has merit.&lt;/p&gt;

&lt;p&gt;Drawing up a prototype &lt;em&gt;should&lt;/em&gt; be done in the context of something like a hackathon. That's the space to throw something at the wall and see if it sticks. But, consider the hierarchy. The thing being built should work towards a given goal. That goal may be &lt;em&gt;learn more about machine learning&lt;/em&gt; and the project of the hackathon may be &lt;em&gt;build a basic ML model to predict something&lt;/em&gt;. You can plan your ad-hoc projects too!&lt;/p&gt;

&lt;p&gt;Be careful when diving into writing code before planning a project. It's much easier to push things to production than it is to remove from production. Use a prototype to make decisions about what and how to build something, and spend as little time on it as possible until a proper project can be drafted.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Project planning for fun and profit! You can do it too! Plan and execute on your projects and you'll be climbing the ranks in no time! Follow these steps to success:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define your goals&lt;/li&gt;
&lt;li&gt;Set up projects that work towards those goals&lt;/li&gt;
&lt;li&gt;Break up your projects into tasks and assign those tasks to people&lt;/li&gt;
&lt;li&gt;When tasks are on other people or teams, raise awareness ASAP&lt;/li&gt;
&lt;li&gt;Watch your task lists every day so you know what to work on next.&lt;/li&gt;
&lt;li&gt;Tell your manager about what you've done&lt;/li&gt;
&lt;li&gt;Prototype with caution&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>projects</category>
      <category>planning</category>
      <category>tasks</category>
      <category>goals</category>
    </item>
    <item>
      <title>Hi, I'm mistermocha</title>
      <dc:creator>mistermocha</dc:creator>
      <pubDate>Tue, 11 Apr 2017 06:26:15 +0000</pubDate>
      <link>https://dev.to/mistermocha/hi-im-mistermocha</link>
      <guid>https://dev.to/mistermocha/hi-im-mistermocha</guid>
      <description>&lt;p&gt;I have been casually coding since 1999 when I started messing around with HTML and CSS. I've been professional at it to some degree since 2008 where I wrote Perl for an online advertising company.&lt;/p&gt;

&lt;p&gt;You can find me on GitHub as &lt;a href="https://github.com/mistermocha" rel="noopener noreferrer"&gt;mistermocha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I work for Twitter as an SRE. &lt;/p&gt;

&lt;p&gt;I mostly program in python and some bash. Occasionally I touch scala and JavaScript too.&lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
