在IDLE中有沒有辦法直接運行PyUnit(unittest module)單元測試?使用IDLE運行Python PyUnit單元測試
我問,因爲我有一個簡短的測試模塊,當我從Cygwin shell中運行python mymodule.py時,所有測試都通過了,但是當我使用Run-> Run Module from IDLE時,測試通過,得到一個異常(SystemExit:False)。
例如,這裏是一個樣品的測試模塊來重現此:
#!/usr/bin/python
import unittest
class fooTests(unittest.TestCase):
def setUp(self):
self.foo = "bar"
def testDummyTest(self):
self.assertTrue(True)
def testDummyTestTwo(self):
self.assertEquals("foo", "foo")
def tearDown(self):
self.foo = None
if __name__ == '__main__':
unittest.main()
當運行此從Cygwin的外殼,蟒fooTests.py它產生:
$ python fooTests.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
但是,當我在IDLE中編輯fooTests.py並且我運行 - >運行模塊,由IDLE產生的新的Python Shell生成:
>>> ================================ RESTART ================================
>>>
..
----------------------------------------------------------------------
Ran 2 tests in 0.031s
OK
Traceback (most recent call last):
File "C:\Some\path\info\I\shortened\fooTests.py", line 20, in <module>
unittest.main()
File "C:\Python26\lib\unittest.py", line 817, in __init__
self.runTests()
File "C:\Python26\lib\unittest.py", line 865, in runTests
sys.exit(not result.wasSuccessful())
SystemExit: False
>>>
我在做什麼錯誤,產生這個回溯,尤其是如何修復它,以便我可以在IDLE內運行 - >運行模塊(或F5)來快速運行單元測試?
(這想必一定是一個簡單的問題,但我很快試圖弄清楚已被證明無果而終。)
try/except可能會使用返回碼,因此如果代碼在CI環境中進行測試,可能會造成麻煩。但是,這些信息非常方便:unitest模塊調用sys.exit,當交互式shell要保持打開狀態時(例如IDle,Emacs python shell等),將顯示此異常。 – 2011-05-24 15:20:20