使用 PyDev 测试运行程序失败
我在 PyDev 中创建了一个默认的单元测试模块。该模块如下:
import unittest
class Test(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testName(self):
self.assertEqual(1, 2) #here I expect to see failure message
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
然后我使用“Run as -> Python unit-test”选项来执行单元测试。我在输出控制台中看到的只是以下两行:
查找文件...完成。
导入测试模块...
然后什么也没有。 Eclipse 的行为就像测试正在执行一样,但实际上什么也没发生。我可以单击“停止”按钮来终止测试,但如果不这样做,什么也不会发生。
注意,在搜索网络时,我发现上面的输出应该包括文件和模块名称(请参阅此讨论)
如果我使用“Run as -> python run”选项,测试按预期执行。我确信我错过了有关使用 PyDev 测试运行程序的一些事情,但无法找出是什么。
将 test* 函数名称从 testName
更改为 test_name
并没有解决问题
I have created a default unit test module in PyDev. The module is as follows:
import unittest
class Test(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testName(self):
self.assertEqual(1, 2) #here I expect to see failure message
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
I then use "Run as -> Python unit-test" option to perform the unit tests. All I see in the output console is the following two lines:
Finding files... done.
Importing test modules ...
And then nothing. Eclipse behaves as if the tests are being executed, but nothing really happens. I can click the "stop" button in order to terminate the test, but if I don't nothing happens.
NOTE that when searching the Net, I figured out that the output above should include file and module names (see this discussion for example)
If I use the "Run as -> python run" option, the tests are performed as expected. I am sure that I miss something about using PyDev test runner, but can't find out what.
Changing the test* function names from testName
to test_name
didn't solve the issue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为(从内存中操作)您应该在测试名称前加上 test_ (需要下划线)。
I think (operating from memory) that your should prefix the test name with test_ (underscore required).
好的,我已经找到问题了。
事实证明,我的模块的初始化代码的几个部分依赖于解析 sys.argv[0]。当使用“Run As -> Python Run”时效果很好,因为 sys.argv[0] 包含执行程序的预期路径。当使用“Run As -> Python单元测试”时,运行的程序是PyDev插件目录中的“runfiles.py”。这种意外的变化导致了无限循环,我将其解释为“什么也没发生”。我会将这种行为视为设计错误,并修改代码中有问题的部分。
OK, I've found the problem.
It turns out that several parts of inititalization code of my modules rely on parsing
sys.argv[0]
. This worked fine when using "Run As -> Python Run" assys.argv[0]
contained the expected path to the executed program. When using "Run As -> Python unit tests", the running program is "runfiles.py" in PyDev plugin directory. This unexpected change caused an infinite loop, which I interpretered as "nothing happens". I will treat this behaviour as a design fault and revise the problematic parts of my code.