python - 为什么这个简单 bool 逻辑的 pytest 失败了?

标签 python testing pytest boolean-logic

<分区>

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

解决方案/_and.py

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

测试和.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 的新手,但在我看来,我的 and 版本在 (True, False) 时返回 False是它的参数,但是测试中的 i 和 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

关于python - 为什么这个简单 bool 逻辑的 pytest 失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909645/

相关文章:

java - 模拟 FeignClient 响应

python - 如何在没有详细测试进度的情况下显示详细的 py.test 差异?

python - 覆盖范围未显示 virtualenv 中执行的行

python - 适用于 Python 的 Docker SDK : how to build an image with custom Dockerfile AND custom context

Python:如何在嵌套循环中用下一个替换 tqdm 进度条?

python - 关闭 TraitsUI 窗口而不单击确定

java - JMeter 消息吞吐量太低

testing - Erlang 测试套件

python - 如何在 py.test 中使用多核?

python - 无法从requirements.txt安装pytest