سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top