为什么这个简单布尔逻辑的 pytest 失败了?
why is this pytest of simple boolean logic failing?
我不明白为什么我对以下函数的 pytest 没有通过:
solutions/_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:\Users\Joseph\projects\byoc
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
我不明白为什么我对以下函数的 pytest 没有通过:
solutions/_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:\Users\Joseph\projects\byoc
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