等式检验的近似

roughl的Python项目详细描述


https://img.shields.io/travis/numberoverzero/roughly/master.svg?style=flat-squarehttps://img.shields.io/coveralls/numberoverzero/roughly/master.svg?style=flat-squarehttps://img.shields.io/pypi/v/roughly.svg?style=flat-squarehttps://img.shields.io/pypi/status/roughly.svg?style=flat-squarehttps://img.shields.io/github/issues-raw/numberoverzero/roughly.svg?style=flat-squarehttps://img.shields.io/pypi/l/roughly.svg?style=flat-square 简单的近似相等性测试的重载值

安装

pip install roughly

用法

测试返回arrow.now()的函数通常如下所示:

import arrow


def function_under_test():
    return arrow.now()


def test_function_returns_now():
    now = arrow.now()
    actual = function_under_test()
    assert now.replace(seconds=-2) <= actual <= now.replace(seconds=2)

如果要检查两个具有datetime字段的对象,情况会更糟 这应该是大致相等的,但是你仍然想要这个班级的 __eq__检查完全相等。有更好的方法:

import arrow
import roughly


def test_function_returns_now():
    now = roughly.near(arrow.now(), seconds=2)
    assert function_under_test() == now

动机

应严格处理近似平等。你可以引入微妙的 当两个日期接近系统的一部分,但不接近时出错 另一个。

在测试中最常见的,但是,我们真的希望使用现有的。 对具有datetime属性且没有 修补提供日期时间的系统。

考虑一个将对象存储在模拟数据库中、更新项的测试 以当前时间。我们真的很想使用 unittest.mock类似于assert_called_once_with,但这需要我们使用 精确值。我们需要看起来与另一个等价的东西 系统。

如果不粗略地说,这是确保约会在附近的解决方法。请注意 所有其他参数都可以通过mock的assert调用进行检查:

assert engine.save.call_count == 1
(item, *_), kwargs = engine.save.call_args
assert item is key
assert kwargs["atomic"] is True

# :( One of the the fields in this object is a datetime, so we can't
# do an exact match.  We could mock arrow.now() but that's really an
# implementation detail that we shouldn't need to know.
condition = Key.until >= arrow.now().replace(seconds=-10)
assert kwargs["condition"].column is condition.column
assert kwargs["condition"].value >= condition.value

这是完全相同的检查,但是对于 条件(正在测试的条件是bloop conditionalexpression):

condition = Key.until >= near(arrow.now(), seconds=5)
engine.save.assert_called_once_with(key, atomic=True, condition=condition)

对象的近似部分被注入到我们所期望的参数中, 当unittest.mock机器执行比较时 self._call_matcher((args, kwargs)) == self._call_matcher(self.call_args) 并遍历(args, kwargs)元组,它将比较 条件对象与我们的近似日期时间的实际条件 实时。在类的__eq__中,它将检查 self.value == other.value用于我们的ApproximateArrow比较 到实际的Arrow

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

推荐PyPI第三方库


热门话题
java StoredProcedureCall 1x Varchar输出1x游标输出   java StackOverflower运行时错误   算法Java基准测试:确保对象在超出范围后不被重用   java在SpringDataNeo4j中使用RelationshipEntity保存节点的正确方法是什么?   命令行参数设置登录java的属性   Java泛型AnyType,如何允许调用任何方法?   java如何检查Html字符串和字符串   sql如何插入一个日期,然后从java执行的存储过程中向oracle数据库中插入的日期添加小时、分钟和秒   java 安卓 3布局相同的活动   安卓应用程序中的java毕达哥拉斯   使用javaw在批处理文件中运行JAR的服务会在process explorer XYNTService中产生多个java进程   java需要在Derby中编写正确的查询   多线程Java在缓存中为多个线程保存变量   持久化java实体引用问题   java在SpringMVC应用程序中使用本地线程安全吗?   JavaSwing,100个文本字段都有类似的任务,所以我想写一个函数来完成这个任务   java我们在新字符串(“literal”)中放置的字符串文字发生了什么变化;   java注入需要在GUI中使用枚举的对象   在Spark SQL中加载JDBC表时java数据不正确