Thanks Ronny. But isn't the whole point of using Mock is to patch out dependencies code? Meaning we should only leave out the code within a single unit (all the functions called should be patched out, and assigned a return_value / side_effect if applicable)?<br>
<br>In another word, if we split as you said, then we should still patch out readfile_makepath, shouldn't we?<br><br>John<br><br><div class="gmail_quote">On Wed, Jul 18, 2012 at 1:22 AM, Ronny Pfannschmidt <span dir="ltr"><<a href="mailto:Ronny.Pfannschmidt@gmx.de" target="_blank">Ronny.Pfannschmidt@gmx.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">you might want to design for testability to keep test complexity low<br>
<br>
for example you can split it up as<br>
<br>
def readfile_makepath(name, local=None, get_default=os.getcwd):<br>
if local is None:<br>
local = get_default()<br>
return os.path.join(local, name)<br>
<br>
def readfile(name, local=None):<br>
f_path = readfile_makepath(name, local)<br>
with open(f_path) as fp:<br>
return fp.read()<br>
<br>
* all of path construction can be tested witout patching now<br>
* for readfile you only need to test that the args<br>
are passed to _makepath and open is called with the result<br>
* for paranoya you can add a test<br>
to assert that get_default is os.getcwd<br>
<br>
-- Ronny<div><div class="h5"><br>
<br>
On 07/18/2012 06:58 AM, John Wong wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
Hi guys,<br>
<br>
Here is a simple function.<br>
<br>
def readfile(*args, **kwargs):<br>
local = kwargs.get('local', os.getcwd())<br>
filename = args[0]<br>
f_path = os.path.join(local, filename)<br>
with open(f_path, 'r') as f:<br>
return f.read()<br>
<br>
<br>
Now here is just ONE case of the readfile test code. For readabilty, I<br>
will put in <a href="http://pastebin.com" target="_blank">pastebin.com</a> <<a href="http://pastebin.com" target="_blank">http://pastebin.com</a>><br>
<a 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<br>
parameters are passed to them. I think these are necessary to check<br>
(what if one of the dependency functions called with the wrong variable).<br>
<br>
But is this really the nature of unittest? I've read many tests code and<br>
they all seen to be 5-20 lines..... I faint when I am reading my test<br>
code.............. SIGH<br>
<br>
I have some 10 lines function and it took 50 lines to test! Let alone<br>
for each of these functions, I need to write a a few tests for each<br>
branch case...<br>
So if there are 10 functions, each 5 lines, I can end up with 1000 lines<br>
of test code....<br>
<br>
Last question, I find my patching is really really long and inconvince.<br>
How should I patch (say they all come from os package)?<br>
<br>
Thanks.<br>
John<br>
<br>
<br></div></div>
______________________________<u></u>_________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.<u></u>org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/<u></u>listinfo/testing-in-python</a><br>
</blockquote>
<br>
</blockquote></div><br>