[TIP] 5 lines of code equals 30+ lines of single test

Chris Jerdonek chris.jerdonek at gmail.com
Tue Jul 17 22:49:10 PDT 2012


On Tue, Jul 17, 2012 at 9:58 PM, John Wong <gokoproject at gmail.com> wrote:
> 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

It seems like you're way over-thinking it.  Why can't you just test
the function as-is?

For example, you can have a directory of test files.  Each test can
call readfile() on one of the test files, and then assert that the
return value matches your expected value.

To test the default for 'local', you can create a context manager that
changes the working directory before your test and then changes it
back afterwards.

Your tests shouldn't be so heavily coupled to the implementation of
the function under test.  For example, that's going to make your code
a lot harder to refactor.  However, knowing the implementation can
guide your choice of test cases for coverage of the right edge cases,
etc.

That is sufficient to check that the functions inside your
implementation are internally being passed the right variables, etc.

--Chris


>
> 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...
> So if there are 10 functions, each 5 lines, I can end up with 1000 lines of
> test code....
>
> Last question, I find my patching is really really long and inconvince. How
> should I patch (say they all come from os package)?
>
> Thanks.
> John
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>



More information about the testing-in-python mailing list