How to parameterize unittests

Antoon Pardon antoon.pardon at rece.vub.ac.be
Sat Apr 16 16:37:52 EDT 2016


Op 15-04-16 om 18:47 schreef Steven D'Aprano:
> On Fri, 15 Apr 2016 10:48 pm, Antoon Pardon wrote:
> 
>> Starting from this:
>>
>>     class Test_AVLTree(unittest.TestCase):
>>  
>>         def test_empty_tree_is_false(self):
>>             instance = avltree()
>>             self.assertFalse(instance)
>>
>> Changing it into this:
>>
>>     def MakeAVLTest(avltree):
>>         class Test_AVLTree(unittest.TestCase):
>>
>>             def test_empty_tree_is_false(self):
>>                 instance = avltree()
>>                 self.assertFalse(instance)
>>
>>         return Test_AVLTree
>>
>>     AVLTest = MakeAVLTest(avltree)
>>     MyTreeTest = MakeAVLTest(mytree)
>>
>> Seems to work
> 
> Right up to the moment that you realise that you need different tests for a
> subclass, and now you can't using subclassing because they aren't
> subclasses, they're completely independent classes that happen to duplicate
> the same methods.
> 
> If the tests for your AVL tree and it's subclasses are *identical*, then
> what's the point of the subclasses?

The subclass has a key-function as a static method similar to the key argument
for sort. This way I can have a tree with strings as keys, but with the
keys sorted according to the locale with the locale.strxfrm function.

This means the only tests that need changing are the tests that test for
iterators to deliver the items in sorted order. But that can be done
by letting the test check for the presence of the keyfunction and in
that case, use if to check if things are in order.

-- 
Antoon




More information about the Python-list mailing list