unit testing - Python unittest successfully asserts None is False -
why assertfalse
succeed on 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
it seems if behaviour invites errors function not return value. 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 pass though should fail. missing error in code should caught.
how should assert value literally false
, not merely false in boolean context? e.g.
self.assertequals(false, none) # assert fails. good!
none
falsy, 0
, ""
, []
, ...
assertfalse
not check whether given value false
identity. behavior consistent if
statement:
if not none: print('falsy value!')
similarly, asserttrue
not check whether value true
, , such values 1
, "abc"
, [1, 2, 3]
pass test. see truth value testing more information.
this behavior explicitly documented:
asserttrue(expr, msg=none) assertfalse(expr, msg=none)test expr true (or false).
note equivalent
bool(expr) true
, notexpr true
if want sure value true
or false
, use assertis
.
Comments
Post a Comment