断言 True 中的奇怪失败是 True

mattrea6

我正在为我拥有的某些功能编写一些单元测试。这个特定的返回两个图像之间差异的百分比值。在测试中,我使用该assert语句来评估一个布尔值,结果是错误的。

当图像在容差范围内匹配时,该函数返回 True,在本次运行中,我将 image_a.png 与自身进行比较,因此每次都返回 True。由于某些未知原因,“断言匹配为真”每次都会引发断言错误。

这是错误日志:

platform linux2 -- Python 2.7.5, pytest-4.6.5, py-1.8.0, pluggy-0.12.0
_____________________________ test_imagediff_match _____________________________

  tmpdir = local('/tmp/pytest-of-install2/pytest-5620/test_imagediff_match0')

  def test_imagediff_match(tmpdir):
  """
  Test normal usage of routine. Images are identical and should match with 0 
  error.
  """
  tmpdir.chdir()
  base_dir = os.path.dirname(__file__)
  = os.path.join(base_dir, "image_a.png")
  image_b = os.path.join(base_dir, "image_a.png")
  comparison = imagediff.ImageDiff(canon=image_a, export=image_b)
  match = comparison.images_match()
> assert match is True
E assert True is True

pythontools/fluids_testing/tools/tests/test_imagediff.py:20: AssertionError

当然,这不应该抛出错误。

这个错误最初是由 pytest-4.6.5 在 TFS 的自动构建环境中的 python 2.7.5 上抛出的,但我在 PowerShell 中运行的 Python 3.7.4 中重复了该错误。

有谁知道为什么逻辑完全失败?我最初认为 True 已被重新分配,但这在 Python 3 中是不可能的。

mattrea6

我在发布问题之前通过将语句更改为 修复了此错误assert match,我只是想知道为什么会发生这种情况。

我发现了错误!imagediff 正在返回 type numpy.bool_,由于某种原因,它不是True

>>> test.test_imagediff_closematch(pathlib.Path("."))
Type of 'match': <class 'numpy.bool_'>
repr(match) = True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\mrea\Documents\FluidsTestTools\pythontools\fluids_testing\tools\tests\test_imagediffssim.py", line 40, in test_imagediff_closematch
    assert match is True
AssertionError
>>>

我觉得这很奇怪,numpy.bool_无法与常规 比较bool,但我想我无论如何都将它们进行了错误的比较。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章