0

I have the following things defined:

def test_groupEs(self):
    for in_list, out_list in self.groupEs_tests:
        self.assertEqual(linSol2.groupEs(in_list), out_list)

groupEs_tests = [
(['4.0x', '-', '2.0e', '-', '4'], ['4.0x', '-' '2.0e-4'])
]

(both in a unittest class)

def groupEs(list):
    for m in range(0, len(list)):
        if re.search('[Ee]\Z', list[m]) != None:
            x = list[m] + list[m+1] + list[m+2]
            list[m+1], list[m+2] = '', ''
            list[m] = x
    removeBlanks(list)
    return list

def removeBlanks(list): # Take empty elements out of the list
    p = len(list) - 1
    while p > -1:
        if list[p] == '':
            list[p:p+1] = []
        p -= 1

(both in the file linSol2.py)

When I import linSol2.py and run linSol2.groupEs in the Python interpreter, it works perfectly:

>>> x = linSol2.groupEs(['4.0x', '-', '2.0e', '-', '4'])
>>> print(x)
['4.0x', '-', '2.0e-4']

But when I run the test in Command Prompt, it fails. Instead it produces the following result:

======================================================================
FAIL: test_groupEs (__main__.linSol2tests)
----------------------------------------------------------------------
Traceback (most recent call last):
    File "linSol2_test.py", line 135, in test_groupEs
        self.assertEqual(linSol2.groupEs(in_list), out_list)
AssertionError: Lists differ: ['4.0x', '-', '2.0e-4'] != ['4.0x', '-2.0e4']

First differing element 1:
-
-2.0e4

First list contains 1 additional elements.
First extra element 2:
2.0e-4

- ['4.0x', '-', '2.0e-4']
?            ----

+ ['4.0x', '-2.0e-4']

Other tests in groupEs_tests such as

(['12e', '-', '7', '-', '4e', '+', '6'], ['12e-7', '-', '4e+6']),
(['e', '-', '2', '-', '7e', '+', '10'], ['e-2', '-', '7e+10'])

work when run from Command Prompt, so I've no idea why it's failing. Can anyone shed any light on the issue?

Note: using Python 3

Martin Thetford
  • 133
  • 1
  • 1
  • 6
  • 1
    Can you show the exact result when you run the script from the command line? `unittest` is a bit more verbose than just outputting the result you give. –  Aug 13 '13 at 09:42
  • 1
    Easier than thought, but nasty: there is no comma in your comparison list: `['4.0x', '-' '2.0e-4']` instead of `['4.0x', '-', '2.0e-4']`. The unit test will actually tell you there is an *extra* element, because it sees only 2 elements in the list it needs to validate against. Hence my previous comment, because it doesn't produce the result you show, instead, it thinks that should have been the output. Your interpretation was off, but `unittest` told you the correct thing: `First list contains 1 additional elements.` –  Aug 13 '13 at 09:54

0 Answers0