Unit testing - one test class/method, or test class/class

Edvard Majakari edvard+news at majakari.net
Fri Feb 25 14:27:29 EST 2005


aurora <aurora00 at gmail.com> writes:

> What I really want to bring up is your might want to look at refactoring
> your module in the first place. 348 test cases for one module sounds like a
> large number. That reflects you have a fairly complex module to be  tested
> to start with. Often the biggest benefit of doing automated unit testing is
> it forces the developers to modularize and decouple their code  in order to
> make it testable. This action alone improve that code quality  a lot. If
> breaking up the module make sense in your case, the test  structure will
> follows.

Here I have to emphasize a little: of those 348 test cases, only ~30 or so
are real, hand-coded methods. Some of the tests are generated on the fly by
py.test. It is not as fancy as it sounds, though. All it does is

test_some_feature(self):

     for foo, bar, expected in known_values:
         yield self.foo_bar_equals, foo, bar, expected

def foo_bar_equals(self, foo, bar, expected):
         
     assert some_feature(foo, bar) == expected

There are two methods. However, if known_values contains 100 tuples, 
py.test generates 100 "test methods" on the fly. Of course you could just do

test_some_feature(self):

     for foo, bar, expected in known_values:
         assert some_feature(foo, bar) == expected

but then you wouldn't see so easily which values are tested when you use
verbose mode, that is. That's one of the (many) nice things in py.test I
like :)

However, being practical in testing is probably more worth than being
completely orthodox, on that I agree. That's why I seldom stick to strict
rules in doing tests, though being systematic helps alot, especially
regarding orthogonality. It doesn't help to test same features over and
over again. Eg. if I wrote a test for a dot in 2D space, I'd write tests for
dot on origo, on positive x-axis with y < 0 and y > 0, ditto for x and y
reversed, then same tests for negative x and y, and last for positive and
negative x and y with other being exactly zero. There's no point testing
other values; all other combinations fall to some of the categories
mentioned.

-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!

$_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";



More information about the Python-list mailing list