为什么这个简单布尔逻辑的pytest失败了



我不明白为什么以下函数的pytest没有通过:

解决方案/and.py

def _and(a, b):
if a:
return b
return False

test_and.py

_and_path='./solutions/'
import itertools
import sys
sys.path.append(_and_path)
print(sys.path)
import _and

def test_and():
for i, j in itertools.product([True, False], [True, False]):
assert(_and._and(i, j) == i and j)

故障报告:

pytest
================================================= test session starts =================================================
platform win32 -- Python 3.6.4, pytest-5.2.4, py-1.8.0, pluggy-0.13.0
rootdir: C:UsersJosephprojectsbyoc
collected 2 items
test_and.py .F                                                                                                   [100%]
====================================================== FAILURES =======================================================
______________________________________________________ test_and _______________________________________________________
def test_and():
for i, j in itertools.product([True, False], [True, False]):
>           assert(_and._and(i, j) == i and j)
E           assert (False == True)
E            +  where False = <function _and at 0x000001F2A5ED7BF8>(True, False)
E            +    where <function _and at 0x000001F2A5ED7BF8> = _and._and
test_and.py:14: AssertionError
============================================= 1 failed, 1 passed in 0.08s =============================================

我是Pytest的新手,但在我看来,当(True, False)是它的参数时,我的and版本返回的是False,但测试中的i and j返回的是具有相同参数的True。我是不是错过了什么?

您需要使用括号:

True and False == True and False # means True and (False == True) and False
Out[3]: False
(True and False) == (True and False)
Out[4]: True

相关内容

  • 没有找到相关文章

最新更新