這個事情再次佐證了一個莫名其妙的現象背後必定會有一個愚蠢到無以復加的錯誤的真理。python
寫python單元測試的時候發現一個莫名其妙的問題:dom
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
直到我很無聊的打算在裏頭引起一個異常:xml
def assertEqualXMLStruct(self,get,wanted): raise ValueError(); self.assertEqual( self.xmlStandander(get), self.xmlStandander(wanted) );
運行。。。咦,怎麼會依然OK?blog
D:\temp\py>xmltest.py ...... ---------------------------------------------------------------------- Ran 6 tests in 0.016s OK
幹掉這個ValueError,換個位置。。我在這裏引起:get
def testNoneExpect(self): raise ValueError(); self.assertEqualXMLStruct ( mapToXmlElement("item", {}), '<item></item>' );
這回成功的引起了異常。。it
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)
真是個詭異的問題。。。ast
而後我很無趣的決定在裏頭隨便亂來一下。。。
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)
祝福大家。。。。