Test succeeds in Python interpreter, but unit test in command prompt fails
-
24-06-2022 - |
Question
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
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow