单元测试断言引发异常
当我运行单元测试来测试两个整数是否相等时,我得到一个断言错误异常,而不是单元测试报告失败,然后继续下一个测试。这是测试代码(为了证明这一点而大大简化)。
import unittest
class TestClass(unittest.TestCase):
def test_method(self):
myVariable = 1
self.assertTrue(myVariable==0)
if __name__ == '__main__':
unittest.main()
回溯如下:
builtins.AssertionError: False is not true
File "C:\Dev\Volleyball\test.py", line 10, in <module>
unittest.main()
File "C:\Program Files\python32\Lib\unittest\main.py", line 124, in __init__
self.runTests()
File "C:\Program Files\python32\Lib\unittest\main.py", line 270, in runTests
self.result = testRunner.run(self.test)
File "C:\Program Files\python32\Lib\unittest\runner.py", line 168, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\case.py", line 498, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\case.py", line 446, in run
self._executeTestPart(testMethod, outcome, isTest=True)
File "C:\Program Files\python32\Lib\unittest\case.py", line 391, in _executeTestPart
function()
File "C:\Dev\Volleyball\test.py", line 7, in test_method
self.assertTrue(myVariable==0)
File "C:\Program Files\python32\Lib\unittest\case.py", line 527, in assertTrue
raise self.failureException(msg)
我是否做错了什么,或者我是否误解了测试断言的工作原理?
根据记录,我在 Windows 7 上使用 Python 3.2.1。TIA
,Alan
When I run a unit-test where I am testing for equality of two integers, I get an assertion error exception instead of unittest reporting a failure, then proceding to the next test. Here is the test code (greatly simplified to prove the point).
import unittest
class TestClass(unittest.TestCase):
def test_method(self):
myVariable = 1
self.assertTrue(myVariable==0)
if __name__ == '__main__':
unittest.main()
The traceback is as follows:
builtins.AssertionError: False is not true
File "C:\Dev\Volleyball\test.py", line 10, in <module>
unittest.main()
File "C:\Program Files\python32\Lib\unittest\main.py", line 124, in __init__
self.runTests()
File "C:\Program Files\python32\Lib\unittest\main.py", line 270, in runTests
self.result = testRunner.run(self.test)
File "C:\Program Files\python32\Lib\unittest\runner.py", line 168, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Program Files\python32\Lib\unittest\case.py", line 498, in __call__
return self.run(*args, **kwds)
File "C:\Program Files\python32\Lib\unittest\case.py", line 446, in run
self._executeTestPart(testMethod, outcome, isTest=True)
File "C:\Program Files\python32\Lib\unittest\case.py", line 391, in _executeTestPart
function()
File "C:\Dev\Volleyball\test.py", line 7, in test_method
self.assertTrue(myVariable==0)
File "C:\Program Files\python32\Lib\unittest\case.py", line 527, in assertTrue
raise self.failureException(msg)
Am I doing something wrong, or am I misunderstanding how test assertions work?
For the record, I am using Python 3.2.1 on Windows 7.
TIA, Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 - 感谢@agf,我已经达成了可接受的妥协。我已从“调试器:异常”的“始终报告”部分中删除了 AssertionError,现在调试器不再在断言上停止。缺点是,虽然在运行测试脚本时这很好(因为我将在失败列表中看到结果),但在运行“真实”程序时,我必须密切关注“调试 I/O”
输出以查看断言。
感谢所有回复的人。
艾伦
Right - thanks to @agf I have reached an acceptable compromise. I have removed AssertionError from the 'Always Report' section of the 'Debugger:Exceptions' and now the debugger no longer stops on the assertion. The downside is that while this is fine while running a test script (because I will see the results in the failure list), when running the 'real' program I will have to keep an eye on the 'Debug I/O'
output to view assertions.
Thanks to all who replied.
Alan
而不是 self.assertTrue(myVariable==0) 尝试使用 self.assertEqual(myVariable, 0)
Instead of self.assertTrue(myVariable==0) try using self.assertEqual(myVariable, 0)