这个事情再次佐证了一个莫名其妙的现象背后一定会有一个愚蠢到无以复加的错误的真理。
写python单元测试的时候发现一个莫名其妙的问题:
def xmlStandander(self,s):
return xml.dom.minidom.parseString(s).toxml(); def assertEqualXMLStruct(self,get,wanted):
self.assertEqual(
self.xmlStandander(get),
self.xmlStandander(wanted)
);
这两个函数是用于测试的辅助函数。
测试函数被这样调用:
def testNoneExpect(self):
self.assertEqualXMLStruct
(
mapToXmlElement("item", {}),
'<item></item>'
);
测试通过。我很无知的认为什么问题都没有了。
D:\temp\py>xmltest.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.016s OK
直到我很无聊的打算在里头引发一个异常:
def assertEqualXMLStruct(self,get,wanted):
raise ValueError();
self.assertEqual(
self.xmlStandander(get),
self.xmlStandander(wanted)
);
运行。。。咦,怎么会依然OK?
D:\temp\py>xmltest.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.016s OK
干掉这个ValueError,换个位置。。我在这里引发:
def testNoneExpect(self):
raise ValueError();
self.assertEqualXMLStruct
(
mapToXmlElement("item", {}),
'<item></item>'
);
这回成功的引发了异常。。
D:\temp\py>xmltest.py
....E.
======================================================================
ERROR: testNoneExpect (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\temp\py\xmltest.py", line 82, in testNoneExpect
raise ValueError();
ValueError ----------------------------------------------------------------------
Ran 6 tests in 0.000s FAILED (errors=1)
真是个诡异的问题。。。
然后我很无趣的决定在里头随便乱来一下。。。
def assertEqualXMLStruct(self,get,wanted):
asdfasasasdfasgadgfads
self.assertEqual(
self.xmlStandander(get),
self.xmlStandander(wanted)
);
结果如下:
D:\temp\py>xmltest.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.000s OK
这极大的颠覆了我的人生观和世界观。。。然后我试了试这个。。
def testNoneExpect(self):
asdfasasasdfasgadgfads
self.assertEqualXMLStruct
(
mapToXmlElement("item", {}),
'<item></item>'
);
结果如下:
D:\temp\py>xmltest.py
....E.
======================================================================
ERROR: testNoneExpect (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\temp\py\xmltest.py", line 84, in testNoneExpect
asdfasasasdfasgadgfads
NameError: global name 'asdfasasasdfasgadgfads' is not defined ----------------------------------------------------------------------
Ran 6 tests in 0.016s FAILED (errors=1)
很明显,我敲入的这一堆乱七八糟的东西不是魔力字符,而是那个函数没有运行。
那么为什么没有运行呢?
揭晓正确答案。。。。。。
~~~~~~~悲催的分割线~~~~~~~
正确的调用方法如下:
def testNoneExpect(self):
self.assertEqualXMLStruct(
mapToXmlElement("item", {}),
'<item></item>'
);
我的世界终于被久违的纠正了:
D:\temp\py>xmltest.py
....E.
======================================================================
ERROR: testNoneExpect (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\temp\py\xmltest.py", line 84, in testNoneExpect
asdfasasasdfasgadgfads
NameError: global name 'asdfasasasdfasgadgfads' is not defined ----------------------------------------------------------------------
Ran 6 tests in 0.000s FAILED (errors=1)
祝福你们。。。。