unit testing class hierarchies
Peter Otten
__peter__ at web.de
Tue Oct 2 13:38:23 EDT 2012
Ulrich Eckhardt wrote:
> Am 02.10.2012 16:06, schrieb Thomas Bach:
>> On Tue, Oct 02, 2012 at 02:27:11PM +0200, Ulrich Eckhardt wrote:
>>> As you see, the code for test_base() is redundant, so the idea is to
>>> move it to a baseclass:
>>>
>>> class TestBase(unittest.TestCase):
>>> def test_base(self):
>>> ...
>>>
>>> class TestD1(TestBase):
>>> def test_r(self):
>>> ...
>>> def test_s(self):
>>> ...
>>>
>>> class TestD2(TestBase):
>>> def test_x(self):
>>> ...
>>> def test_y(self):
>>> ...
>>
>> Could you provide more background? How do you avoid that test_base()
>> runs in TestD1 or TestD2?
>
> 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...
>
> ;)
Ceterum censeo baseclassinem esse delendam ;)
$ cat test_shared.py
import unittest
class Shared(unittest.TestCase):
def test_shared(self):
pass
class D1(Shared):
def test_d1_only(self):
pass
class D2(Shared):
def test_d2_only(self):
pass
del Shared
unittest.main()
$ python test_shared.py -v
test_d1_only (__main__.D1) ... ok
test_shared (__main__.D1) ... ok
test_d2_only (__main__.D2) ... ok
test_shared (__main__.D2) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
$
More information about the Python-list
mailing list