np.testing.assert_array_less()
严格不等式检验:
In [1]: np.testing.assert_array_less(1., 1.)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-1-ea8ee0b762c3> in <module>()
----> 1 np.testing.assert_array_less(1., 1.)
AssertionError:
Arrays are not less-ordered
显然没有用于测试非严格不等式的参数。
如何通过仍然依赖 np.testing 来测试它的可解释错误消息? (我想避免 assert (a <= b).all()
)
最佳答案
正如您所注意到的,显然非严格不等式不在 numpy.testing
中定义的测试用例中。此外,没有记录的方法来使用更多测试用例扩展numpy.testing
。
查看源代码,很明显可以使用 assert_array_compare
来滚动您自己的测试用例:
import operator
def assert_array_less_equal(x, y, err_msg='', verbose=True):
from numpy.testing import assert_array_compare
__tracebackhide__ = True # Hide traceback for py.test
assert_array_compare(operator.__le__, x, y, err_msg=err_msg,
verbose=verbose,
header='Arrays are not equal or less-ordered')
>>> assert_array_less_equal(1., 1.)
>>> assert_array_less_equal(1.1, 1.)
.
.
.
AssertionError:
Arrays are not equal or less-ordered
(mismatch 100.0%)
x: array(1.1)
y: array(1.)
但是,如前所述,assert_array_compare
没有记录,而是 numpy.testing
中的辅助函数。因此,我猜它可能会在 numpy
更新时发生更改和更新,而无需任何通知。这可能会悄无声息地破坏您的代码。
关于python - 使用 np.testing 测试非严格不等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52381301/