[Tutor] unittest problem

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Mar 2 15:49:02 EST 2004


> When launching my program with the => lines commented, no problem. When no
> lines are commented, I obtain the following error:
> $ python test.py
> Traceback (most recent call last):
>    File "test.py", line 5, in ?
>      import test_begohandler
>    File "/home/cfernand/bego/code/lib/test_begohandler.py", line 4, in ?
>      import test
>    File "/home/cfernand/bego/code/lib/test.py", line 6, in ?
>      import test_statemachine
>    File "/home/cfernand/bego/code/lib/test_statemachine.py", line 6, in ?
>      class statemachineTest(test.Base):
> AttributeError: 'module' object has no attribute 'Base'

Python is trying to say that, in the statement that starts off with:

    class statemachineTest(test.Base):

it has no idea what 'test.Base' means.  The same error should also apply
to begohandlerTest, since it too uses 'test.Base'.

... What is the 'test' module?  Ah!  There's a "circular dependency" issue
here!  Your test module contains the following:

###
import begohandler
import test_begohandler
import test_statemachine
import test_syslogparse

class Base(unittest.TestCase):
    ... [code cut]
###


Conceptually, this is saying that the 'test' module depends on those four
modules.  But test_begohandler and test_statemachine themselves depend on
'test'.  This is called a 'circular dependency', and is not usually a good
idea.


The easiest way to fix it is to break the circularity.  I see that
test_begohandler and test_statemachine only care about test.Base, so you
can split off test.Base off into a separate module --- call it 'base.py'.
Then test_begohandler and test_statemachine only need to import 'base',
not 'test', and so we avoid the circularity issue.


Does this make sense?  Good luck to you!




More information about the Tutor mailing list