<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <br>
    <div class="moz-cite-prefix">On 7/18/2012 2:46 PM, John Wong wrote:<br>
    </div>
    <blockquote
cite="mid:CACCLA55Bu2Q8QFMk+wcGkPgCt4PCYiYp4eVurrtidjMC075zSA@mail.gmail.com"
      type="cite">Suppose the sample code is:<br>
      def readfile(*args, **kwargs):<br>
      &nbsp;&nbsp;&nbsp; local = kwargs.get('local', os.getcwd())<br>
      &nbsp;&nbsp;&nbsp; filename = args[0]<br>
      &nbsp;&nbsp;&nbsp; f_path = os.path.join(local, filename)<br>
      &nbsp;&nbsp;&nbsp; with open(f_path, 'r') as f:<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return f.read()<br>
      <br>
      Let's have three tests<br>
      <br>
      @patch.object('os.path', 'join')<br>
      def test01_local_is_used(self, mk_join):<br>
      &nbsp;&nbsp;&nbsp; kwargs = {'local': 'LOCAL'}<br>
      &nbsp;&nbsp;&nbsp; mk_join.return_value = 'full path'<br>
      &nbsp;&nbsp;&nbsp; readfile('filename', **kwargs)<br>
      &nbsp;&nbsp;&nbsp; assert mk_join.call_args_list == [call('LOCAL', 'filename')]<br>
      <br>
      Another test will tests that `open` is called with f_path and `r`
      ?<br>
      Only patch the function that your test is actually depending on?
      In the first time, we want to know local is used, and the
      immediate use of this `local` variable is os.path.join. So
      patching out os.path.join seems reasonable?<br>
      <br>
    </blockquote>
    No, in my opinion, you never need to patch os.path.join to test this
    function.&nbsp; You don't care whether os.path.join was called.&nbsp; You only
    care that it opened the right file.&nbsp; So test that. <br>
    <br>
    --Ned.<br>
    <br>
    <blockquote
cite="mid:CACCLA55Bu2Q8QFMk+wcGkPgCt4PCYiYp4eVurrtidjMC075zSA@mail.gmail.com"
      type="cite">Thanks.<br>
      <br>
      <br>
      <br>
      def test01_local_is_used:<br>
      &nbsp;&nbsp;&nbsp;&nbsp; read<br>
      <br>
      <br>
      <div class="gmail_quote">On Wed, Jul 18, 2012 at 10:18 AM, Ned
        Batchelder <span dir="ltr">&lt;<a moz-do-not-send="true"
            href="mailto:ned@nedbatchelder.com" target="_blank">ned@nedbatchelder.com</a>&gt;</span>
        wrote:<br>
        <blockquote class="gmail_quote" style="margin:0 0 0
          .8ex;border-left:1px #ccc solid;padding-left:1ex">
          <div bgcolor="#FFFFFF" text="#000000">
            <div class="im"> On 7/18/2012 12:58 AM, John Wong wrote:<br>
              <blockquote type="cite">Hi guys,<br>
                <br>
                Here is a simple function.<br>
                <br>
                def readfile(*args, **kwargs):<br>
                &nbsp;&nbsp;&nbsp; local = kwargs.get('local', os.getcwd())<br>
                &nbsp;&nbsp;&nbsp; filename = args[0]<br>
                &nbsp;&nbsp;&nbsp; f_path = os.path.join(local, filename)<br>
                &nbsp;&nbsp;&nbsp; with open(f_path, 'r') as f:<br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return f.read()<br>
                <br>
                <br>
                Now here is just ONE case of the readfile test code. For
                readabilty, I will put in <a moz-do-not-send="true"
                  href="http://pastebin.com" target="_blank">pastebin.com</a><br>
                <a moz-do-not-send="true"
                  href="http://pastebin.com/P45uc2TV" target="_blank">http://pastebin.com/P45uc2TV</a><br>
                <br>
                I am verifying how many times X Y C dependencies are
                called and what parameters are passed to them. I think
                these are necessary to check (what if one of the
                dependency functions called with the wrong variable).<br>
                <br>
              </blockquote>
            </div>
            You definitely don't have to mock os.path.join.&nbsp;&nbsp; You don't
            care that it called that function, you care that it opened
            the right path, and you already have a check for that.&nbsp;
            You're over-testing the implementation of the function.&nbsp;
            Focus on the externally-observable behavior.<br>
            <br>
            If you are checking call_args_list, then .called or
            .call_count is redundant, so remove those lines from the
            test.<br>
            <br>
            BTW: the function itself is a bit odd.&nbsp; Why use **kwargs if
            you only use one keyword, and *args if you only use the
            first element?<span class="HOEnZb"><font color="#888888"><br>
                <br>
                --Ned.<br>
                <br>
              </font></span>
            <blockquote type="cite">
              <div class="im"> But is this really the nature of
                unittest? I've read many tests code and they all seen to
                be 5-20 lines..... I faint when I am reading my test
                code.............. SIGH<br>
                <br>
                I have some 10 lines function and it took 50 lines to
                test! Let alone for each of these functions, I need to
                write a a few tests for each branch case...<br>
                So if there are 10 functions, each 5 lines, I can end up
                with 1000 lines of test code....<br>
                <br>
                Last question, I find my patching is really really long
                and inconvince. How should I patch (say they all come
                from os package)?<br>
                <br>
                Thanks.<br>
                John<br>
                <br>
                <fieldset></fieldset>
                <br>
              </div>
              <div class="im">
                <pre>_______________________________________________
testing-in-python mailing list
<a moz-do-not-send="true" href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a moz-do-not-send="true" href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
              </div>
            </blockquote>
            <br>
            <br>
          </div>
        </blockquote>
      </div>
      <br>
    </blockquote>
    <br>
    <br>
  </body>
</html>