Python unittest successfully claims None is False

Why is assertFalsedoing well None?

import unittest

class TestNoneIsFalse(unittest.TestCase):
    def test_none_is_false(self):
        self.assertFalse(None)

Results:

> python -m unittest temp
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

This behavior seems to cause errors when a function does not always return a value. For example:

def is_lower_than_5(x):
    if x < 5:
        return True
    elif x > 5:
        return False

....

def test_5_is_not_lower_than_5(self):
   self.assertFalse(is_lower_than_5(5))

The above test would pass, even if it should fail. There is no error in the code.

How can we say that a value is literal False, not just false in a boolean context? eg.

self.assertEquals(False, None)  # assert fails. good!
+4
source share
3 answers

NoneIt is false, and 0, "", [], ...

assertFalsedoes not check if the given Falseidentifier has a value . This behavior is consistent with the statement if:

if not None:
    print('falsy value!')

, assertTrue , True, , 1, "abc", [1, 2, 3] . . Truth Value Testing.

:

assertTrue(expr, msg=None)
assertFalse(expr, msg=None)

, expr ( false).

, bool(expr) is True, expr is True

, True False, assertIs.

+7

:

, bool (expr) - True, expr True ( assertIs (expr, True) ).

: https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertFalse

+1

Python - None.

Python Duck typing, ,

  • boolean False

, self.assertEquals(False, None), False. , python

if my_value:
    print 'truthy value'
else:
    print 'falsey value'

, , .

0

Source: https://habr.com/ru/post/1626179/


All Articles