使用IDLE运行Python单元测试
是否有一种方法可以在空闲时间内直接运行PyUnit(unittest模块)单元测试 我问这个问题是因为我有一个简短的测试模块,当我从Cygwin shell中使用python mymodule.py运行它时,所有测试都通过了,但是当我使用run->run module from IDLE时,测试通过了,但随后我得到了一个异常(SystemExit:False) 例如,下面是一个示例测试模块,用于重现:使用IDLE运行Python单元测试,python,unit-testing,python-idle,Python,Unit Testing,Python Idle,是否有一种方法可以在空闲时间内直接运行PyUnit(unittest模块)单元测试 我问这个问题是因为我有一个简短的测试模块,当我从Cygwin shell中使用python mymodule.py运行它时,所有测试都通过了,但是当我使用run->run module from IDLE时,测试通过了,但随后我得到了一个异常(SystemExit:False) 例如,下面是一个示例测试模块,用于重现: #!/usr/bin/python import unittest class fooTe
#!/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()
当我使用python fooTests.py从Cygwin shell运行它时,它会生成:
$ python fooTests.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
>>> ================================ 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中编辑fooTests.py并运行->运行模块时,IDLE生成的新Python Shell将生成:
$ python fooTests.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
>>> ================================ 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
>>>
重新启动================================
>>>
..
----------------------------------------------------------------------
在0.031s内运行了2次测试
好啊
回溯(最近一次呼叫最后一次):
文件“C:\Some\path\info\I\shorted\fooTests.py”,第20行,在
unittest.main()
文件“C:\Python26\lib\unittest.py”,第817行,在\uuu init中__
self.runTests()
runTests中第865行的文件“C:\Python26\lib\unittest.py”
sys.exit(不是result.wasSuccessful())
SystemExit:False
>>>
我做错了什么,以产生这种回溯,尤其是我如何修复它,以便我可以在空闲时间内运行->运行模块(或F5)以快速运行单元测试
(这肯定是一个简单的问题,但我的快速尝试证明是徒劳的。)没有人回答(我发现如果在最初几分钟内没有任何答案,得到答案的可能性会显著降低:),所以我自己一直在研究这个问题
不确定这是否是最优雅的解决方案,但正在改变:
if __name__ == '__main__':
unittest.main()
到
他似乎成功了
我猜问题在于(根据)unittest模块通过调用sys.exit来完成,这对于IDLE来说显然是个问题,因为它希望保持Python shell运行,而当您从命令行运行它时,这不是问题,因为命令行希望将您转储回已经运行的命令行
我不知道这个捕获SystemExit事件并忽略它的解决方案是否有问题,但它似乎适用于我检查的所有测试通过和一些测试失败的情况
另外:请参阅此StackOverFlow Post:,它提供了一个测试,以查看程序是否在空闲状态下运行。类似的操作也应该可以:
suite = unittest.TestLoader().loadTestsFromTestCase( fooTests )
unittest.TextTestRunner(verbosity=2).run( suite )
我发现了这一点,它对我很有效。你也可以这样做
if __name__ == '__main__':
unittest.main(exit=False)
如果您使用的是Python>=2.7,try/except可能会吃掉返回代码,因此如果在CI环境中测试代码,则可能会导致问题。但是,这些信息很方便:当交互式shell想要保持打开状态时(即IDle、Emacs-pythonshell等),unitest模块调用sys.exit,因此显示了此异常。