Ben*_*Lee 3 python math pytest travis-ci python-3.4
在对我的 Python 模块进行单元测试时,我遇到了一个奇怪的错误:
\n\n\xe2\x85\x94 的构建正常通过,但其中之一无法isclose从标准math库导入。
该错误重现如下:
\n\n==================================== ERRORS ====================================\n______________________ ERROR collecting tests/test_yau.py ______________________\nImportError while importing test module \'/home/travis/build/Benjamin-Lee/squiggle/tests/test_yau.py\'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\ntests/test_yau.py:5: in <module>\n from math import isclose\nE ImportError: cannot import name \'isclose\'\n!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!\n=========================== 1 error in 0.29 seconds ============================\nThe command "pytest --cov=squiggle" exited with 2.\nRun Code Online (Sandbox Code Playgroud)\n\n在同一目录中没有指定的文件math.py(或者在我的包中根本没有)。可能是什么原因造成的?
多次重新启动构建并没有修复这个错误,它只出现在 Python 3.4 中。
\n\n完整日志可在此处访问。
\npytestapprox具有测试两个数字近似相等的功能,适用于任何 python 版本。断言
assert math.isclose(a, b, rel_tol=rt, abs_tol=at)
Run Code Online (Sandbox Code Playgroud)
因此可以替换为
assert a == pytest.approx(b, rel=rt, abs=at)
Run Code Online (Sandbox Code Playgroud)