unit testing class hierarchies

Demian Brecht demianbrecht at gmail.com
Tue Oct 2 13:51:31 EDT 2012


Am I missing something? Is there something that wasn't answered by my reply
about using mixins?

from unittest import TestCase

class SharedTestMixin(object):
    def test_shared(self):
        self.assertNotEquals('foo', 'bar')

class TestA(TestCase, SharedTestMixin):
    def test_a(self):
        self.assertEquals('a', 'a')

class TestB(TestCase, SharedTestMixin):
    def test_b(self):
        self.assertEquals('b', 'b')

$ nosetests test.py -v
test_a (test.TestA) ... ok
test_shared (test.TestA) ... ok
test_b (test.TestB) ... ok
test_shared (test.TestB) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

This seems to be a clear answer to the problem that solves the original
requirements without introducing error-prone, non-obvious solutions.



> Sorry, there's a misunderstanding: I want test_base() to be run as part of
> both TestD1 and TestD2, because it tests basic functions provided by both
> classes D1 and D2. The instances of D1 and D2 are created in TestD1.setUp
> and TestD2.setUp and then used by all tests. There is no possible
> implementation creating such an instance for TestBase, since the baseclass
> is abstract.
>
> Last edit for today, I hope that makes my intentions clear...
>
> ;)
>
> Uli
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121002/7e203a31/attachment.html>


More information about the Python-list mailing list