python - 使用 np.testing 测试非严格不等式

标签 python numpy testing

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/

相关文章:

python - 多处理管理器进程不释放内存

python - 使用 scikit-learn 进行支持向量机训练

testing - 外部库上的 Jasmine 测试

testing - Automake:将本地便利行为添加到 "make check"

python - 模块递归导入其子模块是一种好习惯吗?

python - 从麦克风生成频谱图

python - 为什么使用 numpy.random.seed 不是一个好习惯?

python - 使用 numpy einsum 将向量列表乘以一个矩阵

c - 使用 cython_gsl 进行集成(加上将 numpy.array 寻址到 cython)

java - Web 应用程序中的 Appium : Unable to tap Allow permission button in notification pop up window