Problem understanding some unit tests in Python distribution

Peter Otten __peter__ at web.de
Sat Apr 4 09:38:51 CEST 2009


André wrote:

> Hi everyone,
> 
> In the hope of perhaps contributing some additional unit tests for
> Python (thus contributing back to the community), I dove in the code
> and found something that should be simple but that I can not wrap my
> head around.
> 
> In list_tests.py, one finds
> ===
> from test import test_support, seq_tests
> 
> class CommonTest(seq_tests.CommonTest):
> 
>     def test_init(self):
>         # Iterable arg is optional
>         self.assertEqual(self.type2test([]), self.type2test())
> # etc.
> ===
> 
> Wanting to figure out what the type2test() method does, I looked in
> seq_tests.py and found the following:
> 
> ===
> class CommonTest(unittest.TestCase):
>     # The type to be tested
>     type2test = None
> 
>     def test_constructors(self):
>         l0 = []
>         l1 = [0]
>         l2 = [0, 1]
> 
>         u = self.type2test()
>         u0 = self.type2test(l0)
>         u1 = self.type2test(l1)
>         u2 = self.type2test(l2)
> 
> # etc.
> ===
> No where do I find a definition for the type2test() method - other
> than seeing it as a class variable defined to be None by default.  I
> looked in unittest.TestCase and did not see it anywhere.
> 
> Am I missing something obvious?  I would appreciate if someone could
> point me in the right direction.

Use grep ;)

CommonTest is not run standalone, it is used as a(n abstract) base class for
several other tests that override type2test. 

E. g. in Lib/test/list_tests.py:

from test import test_support, seq_tests

class CommonTest(seq_tests.CommonTest):
...

And then in Lib/test/test_list.py:

from test import test_support, list_tests

class ListTest(list_tests.CommonTest):
    type2test = list
...

Peter



More information about the Python-list mailing list