我定义了以下内容:
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'])
]
(均为单元测试类)
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
(均在文件linSol2.py中)
当我导入linSol2.py并在Python解释器中运行linSol2.groupEs时,它完美地运行:
>>> x = linSol2.groupEs(['4.0x', '-', '2.0e', '-', '4'])
>>> print(x)
['4.0x', '-', '2.0e-4']
但是当我在命令提示符下运行测试时,它失败了。相反,它会产生以下结果:
======================================================================
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']
groupEs_tests中的其他测试,例如
(['12e', '-', '7', '-', '4e', '+', '6'], ['12e-7', '-', '4e+6']),
(['e', '-', '2', '-', '7e', '+', '10'], ['e-2', '-', '7e+10'])
从命令提示符运行时工作,所以我不知道为什么它失败了。任何人都可以解释这个问题吗?
注意:使用Python 3