views:

117

answers:

3

Hello,

I am wrting test's and I have heard some people saying to use self.assertFalse rather than assert False. Why is this and are there any advantages to be had?

Thanks

+9  A: 

assert False throws an exception without useful logging information. The test had an error.

self.assertFalse() throws a test failure exception with test failure information like a message and a test name.

There's a difference between an error -- test could not even run -- and a failure -- test code worked but produced the wrong answer.

Errors are a serious problem with your code.

Failures are just failures that need to be fixed.

S.Lott
@S.Lott -- Thanks, that makes useful to know, I'll need to remove the `assert False` lines. +1
chrissygormley
+5  A: 

If you run

import unittest

class Test_Unittest(unittest.TestCase):
    def test_assert(self):
        assert False
    def test_assertFalse(self):
        self.assertFalse(True)

if __name__ == '__main__':
    unittest.main(argv = unittest.sys.argv)

You get the same logging information, the same failure:

FF
======================================================================
FAIL: test_assert (__main__.Test_Unittest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 6, in test_assert
    assert False
AssertionError

======================================================================
FAIL: test_assertFalse (__main__.Test_Unittest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 8, in test_assertFalse
    self.assertFalse(True)
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)

The reason both are handled the same is because unittest.TestCase defines

failureException = AssertionError

When you say assert False an AssertionError is raised.

When you say self.assertFalse(True), a failureExeception is raised.

Since these exceptions are the same, there is no apparent difference.

assert and self.assertFalse do differ in conventional usage, however.

assert is used to declare that a certain condition should hold at a certain point in the code. It is used as a crutch during development, but is not meant to be used in production code. If you run python -O my_unittest.py, all assert statements are ignored. That would subvert your intended usage of assert, possibly making your unit tests pass even when there is a failure.

Even though (without the -O flag) the result is the same, assert is not meant to be used in unit test code. Use self.assertTrue or self.assertFalse when writing unit tests.

unutbu
@~unutbu -- +1, Found this very useful. This is slot clearer now, thanks.
chrissygormley
A: 

One point the answers so far have failed to mention is that there are several test frameworks (e.g. py.test and nose) that use python's introspection magic to allow you to write unit tests like so:

# test_this_and_that.py
def test_frobber():
    assert frobber('x') == 'y'
# EOF

without requiring any of the unittest boilerplate you saw above. So in some cases it can boil down to just a framwork/stylistic issue.

as
@chrissygormley: they have quotes. They're strings. What are you asking?
S.Lott
chrissygormley: they have quotes. They're not variables. Could you please delete your comment if it's no longer relevant?
S.Lott