我在对我的 Python 模块进行单元测试时遇到了一个奇怪的错误:
⅔ 的构建正常通过,但其中一个无法从标准 math 库导入 isclose。
错误重现如下:
==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_yau.py ______________________
ImportError while importing test module '/home/travis/build/Benjamin-Lee/squiggle/tests/test_yau.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_yau.py:5: in <module>
from math import isclose
E ImportError: cannot import name 'isclose'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.29 seconds ============================
The command "pytest --cov=squiggle" exited with 2.
在同一目录(或我的包中根本没有)中没有名为 math.py 的文件。可能是什么原因造成的?
多次重新启动构建并没有修复这个错误,它只出现在 Python 3.4 中。
完整日志可访问here .
请您参考如下方法:
pytest 具有函数 approx用于测试两个数字的近似相等性,适用于任何 python 版本。断言
assert math.isclose(a, b, rel_tol=rt, abs_tol=at)
因此可以替换为
assert a == pytest.approx(b, rel=rt, abs=at)
