Unexpected Behaviour using unittest

Bruce Coram brucecoram at clara.co.uk
Fri Feb 22 02:23:29 EST 2008


The code fragment below shows what appears to be inconsistent 
behaviour.  The code is testing  that messages are created correctly and 
that an instances of a class is created correctly.  It checks that all 
of the expected addresses are used (contained in addresslist) and that 
all of the messages are used (contained in msglist).  If the data in 
msglist that appears in the list in the dictionary whose first key is  
'Sonar  Main' is a simple list of two items the test behaves as expected 
and passes.  However, if the data is enclosed within a tuple such that 
the list only contains the tuple, which contains the two values, the 
test fails.  The first of these two values is a class object and the 
code tests whether the instance of this class is created correctly 
locally and that test is passed in both cases.  The test fails because 
the msglist item for 'Sonar Main'  is not deleted.  I have included an 
example of the 'Sonar Main' data and also the test output.  I can not 
explain this behaviour and while it is not a showstopper I need to 
understand why.  The same behaviour is shown elsewhere if a list of four 
items is grouped into two tuples each of two items.  Grateful for any 
comments/explanations.

CODE FRAGMENT:

       msglist = [{'Helm': {'init': (80, 80, 0, 12, 140, 20)}},
            {'UAW Director': {'init': (80, 80, 0, 12, 140, 20), 
'sonar_a': ['std'], 'sub_attack': ['std']}},
        {'Gun Director': {'guns_s': ['std'], 'init': (80, 80, 0, 12, 
140, 20), 'guns_mr': ['std'], 'guns_aa': ['std']}},
        {'Sonar Main': {'sonar_s': [DummySonar, 0]}}, {'Surface Radar 
Operator': {'radar_s': ['std']}}]
        addresslist = ['127.10.2.2', '127.10.2.1', '127.10.2.5', 
'127.10.2.4', '127.10.2.3']
        for entry in msgs:
            if entry[1] in addresslist:
                addresslist.remove(entry[1])
            if entry[0]['set role'] in msglist:
                if entry[0]['set role'].keys() == ['Sonar Main']:
                    print 'keys = ', entry[0]['set role'].keys()
                    if not 
isinstance(self.newprocessor.vessel.uwsearch.sonar_search, 
shipserverclasses.DummySonar):
                        Pass = False
                        print '1', 
type(self.newprocessor.vessel.uwsearch.sonar_search)
                    print 'removing msg ', entry[0]['set role']
                    msglist.remove(entry[0]['set role'])
                else:
                    print 'removing msg ', entry[0]['set role']
                    msglist.remove(entry[0]['set role'])
        if addresslist != []:
            Pass = False
            print '2', addresslist
        if msglist != []:
            Pass = False
            print '3', msglist
        self.failUnless(Pass == True)

------------------------------------------------------------------------------------------------

Data fragment - passed:

{'Sonar Main': {'sonar_s': [DummySonar, 0]}}

Test Output:

testAllConnectedUWsearchChange2 
(__main__.TestMsgProcessesShipServerSetType) ...
 removing msg  {'Helm': {'init': (80, 80, 0, 12, 140, 20)}}
removing msg  {'UAW Director': {'init': (80, 80, 0, 12, 140, 20), 
'sonar_a': ['s
td'], 'sub_attack': ['std']}}
keys =  ['Sonar Main']
removing msg  {'Sonar Main': {'sonar_s': [<class 
'shipserverclasses.DummySonar'>
, 0]}}
removing msg  {'Gun Director': {'guns_s': ['std'], 'init': (80, 80, 0, 
12, 140,
20), 'guns_mr': ['std'], 'guns_aa': ['std']}}
removing msg  {'Surface Radar Operator': {'radar_s': ['std']}}
ok

------------------------------------------------------------------------------------------------------

Data fragment - failed:

{'Sonar Main': {'sonar_s': [(DummySonar, 0)]}}

Test Output:

testAllConnectedUWsearchChange2 
(__main__.TestMsgProcessesShipServerSetType) ...
 removing msg  {'Helm': {'init': (80, 80, 0, 12, 140, 20)}}
removing msg  {'UAW Director': {'init': (80, 80, 0, 12, 140, 20), 
'sonar_a': ['s
td'], 'sub_attack': ['std']}}
removing msg  {'Gun Director': {'guns_s': ['std'], 'init': (80, 80, 0, 
12, 140,
20), 'guns_mr': ['std'], 'guns_aa': ['std']}}
removing msg  {'Surface Radar Operator': {'radar_s': ['std']}}
3 [{'Sonar Main': {'sonar_s': [(<class 'shipserverclasses.DummySonar'>, 
0)]}}]
FAIL




More information about the Python-list mailing list