扩展numpy的allclose函数的pytest夹具

pytest-allclose的Python项目详细描述


pytest allclose

pytest-allclose提供~.allclosepytest fixture, 扩展了numpy.allclose的测试特性。

allclose夹具的核心功能 测试的公差可以在外部配置。 这允许不同的存储库共享相同的测试, 但使用不同的公差。 有关详细信息,请参阅下面的“配置”部分。

安装

要使用此夹具,请使用

pip install pytest-allclose

使用量

~.allclose设备的使用方式与numpy.allclose类似。

importnumpyasnpdeftest_close(allclose):x=np.linspace(-1,1)y=x+0.001assertallclose(y,x,atol=0.002)assertnotallclose(y,x,atol=0.0005)assertnotallclose(y,x,rtol=0.002)

附加参数

~.allclosefixture有许多参数 那不是numpy.allclose的一部分。 其中一个参数是xtol, 它允许沿着第一轴移动的阵列。 通过一定数量的步骤被认为是接近的。

importnumpyasnpdeftest_close(allclose):x=np.linspace(-1,1)assertallclose(x[1:],x[:-1],xtol=1)assertallclose(x[3:],x[:-3],xtol=3)assertnotallclose(x[3:],x[:-3],xtol=1)

有关所有其他参数,请参阅~.allcloseapi参考。

RMSE错误报告

夹具存储均方根误差值, 可以在pytest终端摘要中报告。 为此,请将以下内容放入conftest.py文件中。

frompytest_allcloseimportreport_rmsesdefpytest_terminal_summary(terminalreporter):report_rmses(terminalreporter)

有关详细信息,请参阅API参考。

配置

allclose_公差

allclose_tolerances接受测试名称模式列表, 后跟任何~.allclose参数的值。 这些值将覆盖测试函数本身提供的任何值, 允许多个存储库使用同一个测试套件, 但公差不同。

allclose_tolerances=
    test_file.py:test_function atol=0.3  # set atol for specific test
    test_file.py:test_func* rtol=0.2  # set rtol for tests matching wildcard
    test_file.py:* atol=0.1 rtol=0.3  # set both tols for all tests in file
    test_*tion rtol=0.2  # set rtol for all matching tests in any file
    test_function[True] atol=0.1  # set atol only for one parametrization

在这些模式中唯一被识别的特殊字符 是通配符*, 它匹配任何一组零个或多个字符。

如果测试是参数化的, 然后像^{tt6}这样的模式$ 将匹配特定的参数设置, 并且test_name*将匹配所有参数设置。 注意,后者将匹配以test_name开头的任何测试。

如果一个测试有多个~.allclose调用, 可以使用多条与同一测试匹配的公差线 为第一次、第二次、第三次等调用设置不同的值。 如果有比公差线更多的~.allclose调用, 最后一条公差线将用于所有剩余的~.allclose调用。

测试文件示例:

deftest_close(allclose):x=np.linspace(-1,1)y=x+0.001assertallclose(y,x)assertnotallclose(y,x)

示例配置文件(pytest.inisetup.cfg):

allclose_tolerances=
    test_close atol=0.002  # affects first allclose call
    test_close atol=0.0005  # affects second allclose call

注意

不同的公差线对应于 函数,而不是代码行。如果你有for 循环调用~.allclose3次, 每个调用都对应一个新的公差线。 如果您有第四个~.allclose呼叫, 三个电话你需要三条容忍线 在for循环中,然后是最后一个调用的第四行。

小心!

函数中多次调用allclose的模式 必须完全相同。 这意味着如果你有一个 其他参数化和一般值, 必须将特定值放在第一位 否则它们不会有任何效果。

好例子,具体优先:

allclose_tolerances=
    test_close[True-1] atol=0.002
    test_close[True-1] atol=0.0005
    test_close* atol=0.001
    test_close* atol=0.0001

错误的示例,一般优先:

allclose_tolerances=
    test_close* atol=0.001
    test_close* atol=0.0001
    test_close[True-1] atol=0.002
    test_close[True-1] atol=0.0005

看到全部 documentation 用于API引用。

发布历史记录

1.0.0(2019年7月30日)

初版pytest-allclose! 感谢所有的贡献者让这成为可能!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
ssl证书javax。网ssl。SSLHandshakeException:java。安全cert.CertificateException:没有与IP地址匹配的主题替代名称   基于组件的体系结构的java粒度   java在读取文件中的下一行和上一行时,我得到一个空指针异常   java是什么。StrutConfig。xml。strutsdia文件?   java中如何根据枚举字段对一组对象进行排序   java Firebase setPersistenceEnabled。导致大量内存使用   java奇数编译泛型类和列表错误   java类型org没有可用的源代码。石英克隆表达;您是否忘记继承所需的模块?   java如何使用map计算列表中整数列表的和,并获得一个新列表,其中每个条目对应于每个计算出的和?   java二进制搜索不会结束   java跳过Jackson中的错误JSON数据   在服务层中使用依赖项注入时引发java空指针异常。DAO类bean为空   访问者模式如何解释这两个Java程序运行时间的差异?   用Java扩展日历   java调用通用静态方法