单元测试断言引发异常

发布于 2024-11-30 03:27:08 字数 1801 浏览 4 评论 0原文

当我运行单元测试来测试两个整数是否相等时,我得到一个断言错误异常,而不是单元测试报告失败,然后继续下一个测试。这是测试代码(为了证明这一点而大大简化)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-12-07 03:27:08

是的 - 感谢@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

哀由 2024-12-07 03:27:08

而不是 self.assertTrue(myVariable==0) 尝试使用 self.assertEqual(myVariable, 0)

Instead of self.assertTrue(myVariable==0) try using self.assertEqual(myVariable, 0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文