Please bear with me. Suppose I want to assert that result is in a particular format, I might set the return value to something as (True, &#39;Success&#39;)<br><br>Sample code: <a href="http://pastebin.com/VraxaKjL">http://pastebin.com/VraxaKjL</a><br>
<br><div style="margin-left:40px">def batch_move(self, *args, **kwargs):<br>  &#39;&#39;&#39; <br>  Move a batch of files to its respective destinations.<br>  Return type: tuple (boolean, message)<br>                      T/F    , &#39;string&#39; / str(exception)<br>
  &#39;&#39;&#39;<br><br>  srcs= kwargs.get(&#39;srcs&#39;, None)<br>  dests = kwargs.get(&#39;dests&#39;, None)<br><br>  try:<br>    if srcs and dests:<br>       # map srcs and dests into dictionary (srcs --&gt; keys, dests --&gt; values)<br>
       src_dest= dict(zip(srcs, dests))     <br><br>       for src, dest in src_dest:<br>         if os.path.exists(src):<br>           if os.path.exists(dest):<br>              shutil.rmtree(dest)<br>           shutil.move(src, dest)   # either way we will proceed to move<br>
         else:<br>           return (False, &#39;%s does not exist!&#39; % src)<br>       return (True, &#39;Success!&#39;)<br><br>    else:<br>       return (False, &#39;Something gone wrong with those kwargs...&#39;)<br>
  except Exception as e:<br>    return (False, e)<br><br></div>In order to get to return (True, &#39;Success!&#39;), I have<br><br>1. patch os.path.exists with True as return value. But in one unittest I want to skip this, how do I patch that os.path.exists?<br>
<div style="margin-left:40px">        if os.path.exists(dest):  # I want to skip this<br>             shutil.rmtree(dest)<br></div><br><br><br>2. How do I patch shutil.move(src, dest)? Do I just give True so it doesn&#39;t generate error? What if I want the case it fails and caught an exception? How do I simulate that? (I wouldn&#39;t always know which exception to catch, the primarily reason to use Exception as e).<br>
<br>3. If I actually pass the function, does it really mean no exception caught and it went through every single line? Or is it because I set `mock_object.return_value = (True, &#39;Success!&#39;)?<br><br>4. I am only using two dependencies here, do I need to patch out all the external dependencies such as (os, sys, math, datetime) all in one? Or if my function is using other functions (which are refactored)<br>
<br><br><div style="margin-left:40px"> def f1(*args, **kwargs):<br>    f2(..)   # use math, plot, datetime<br>    f3(..)   # use math and datetime<br>    f4(..)   # use datetime<br></div><br><br>Thanks. Sorry for the long questions. I really want to be good at writing unittests.<br>
<br><br>--<br>Ted<br><br><br>