Unexpected Behaviour using unittest

Discussion in 'Python' started by Bruce Coram, Feb 22, 2008.

  1. Bruce Coram

    Bruce Coram Guest

    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
    Bruce Coram, Feb 22, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    4
    Views:
    2,945
    scoude
    Jan 12, 2011
  2. Steven Van den Berghe

    unexpected map behaviour

    Steven Van den Berghe, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    417
    Christian Jan├čen
    Aug 28, 2003
  3. Collin Winter

    Questions on unittest behaviour

    Collin Winter, Aug 19, 2006, in forum: Python
    Replies:
    3
    Views:
    279
    John Roth
    Aug 19, 2006
  4. Paul  Moore
    Replies:
    1
    Views:
    384
    Paul Moore
    Oct 14, 2008
  5. AlienBaby
    Replies:
    1
    Views:
    172
    Peter Otten
    Jul 28, 2011
Loading...

Share This Page