一个图书馆,使编写比哈姆克雷斯特火柴更容易和更有趣。

pyhamcrest-toolbox的Python项目详细描述


https://badge.fury.io/py/pyhamcrest-toolbox.svgDocumentation StatusMaintainabilityPyPI Package monthly downloads

Pyhamcrest工具箱

pyhamcrest对于编写可重用的测试来说是一件很好的事情,但是有时候 写你自己的媒人可能有点麻烦。这个项目的目标是 使这项任务简单,快速,甚至有趣(为想要一个更好的词)。

要重申这一点,测试应该总是做所有必须做的检查, 即使其中一些失败了,其余的也应该继续运行。那样你就可以了 更好地了解你的代码出了什么问题,你就会浪费 较少的时间固定第一个检查,只发现第二个是 仍然失败,这意味着你应该修复 不同的方式。

所以,不要这样:

deftest_the_holy_grail():the_holy_grail=seek_the_holy_grail()assert_that(the_holy_grail.is_holy(),is_(True))assert_that(the_holy_grail.depth,greater_than(5))assert_that(the_holy_grail.width,greater_than(6))assert_that(the_holy_grail.height,greater_than(7))

这应该写成:

deftest_the_holy_grail():the_holy_grail=seek_the_holy_grail()assert_that(the_holy_grail,is_holy().with_depth(greater_than(5)).with_width(greater_than(6)).with_height(greater_than(7)))

或:

deftest_the_holy_grail():the_holy_grail=seek_the_holy_grail()assert_that(the_holy_grail,grail(holy=True,width=5))

然而,这两个例子都需要编写自己的匹配器。有了这个工具箱, 这很容易。

多组分匹配器

MulticomponentMatcher允许编写链式匹配器。

您只需编写从 MulticomponentMatcher作为主干。然后你写个人匹配 对于每个圣杯属性,使用 MatcherPlugin,然后用is_holy匹配器注册它们。

所以,这是您的is_holymatcher:

classIsHolyMatcher(MulticomponentMatcher):def__init__(self):super().__init__()defis_holy():returnIsHolyMatcher()

就这样。你不必重写通常的匹配方法。一切 将由父类完成。但是,它还没有做任何匹配,所以我们 需要编写插件。让我们从真正的神圣开始:

classHolinessMatcher(MatcherPlugin):def__init__(is_holy=True):super().__init__()self.is_holy=is_holydefcomponent_matches(self,item):# the item will be a grailreturnself.is_holy==item.is_holy()defdescribe_to(self,description):description.append_text("A grail which is {}holy".format(""ifself.is_holyelse"not "))defdescribe_component_mismatch(self,item,mismatch_description):mismatch_description.append_text("The grail was {}holy".format(""ifitem.is_holy()else"not "))

然后在主匹配器中注册:

classIsHolyMatcher(MulticomponentMatcher):def__init__(self,is_holy):super().__init__()self.register(HolynessMatcher(is_holy))defholy(is_holy):returnIsHolyMatcher(is_holy)

当然,您可以在 IsHolyMatcher,但是如果我们有插件的能力,为什么不使用它呢?

现在,我们只有这个位:assert_that(the_grail, is_holy()),并且 不是那种.with_width(...)的东西。所以我们来写吧。我不会经历 编写宽度插件的过程非常简单, 但这里是您如何向主匹配器注册的:

classIsHolyMatcher(MulticomponentMatcher):def__init__(self,is_holy):super().__init__()self.register(HolinessMatcher(is_holy))defwith_width(self,value):returnself.register(GrailWidthMatcher(value))defholy(is_holy):returnIsHolyMatcher(is_holy)

现在你可以做is_holy().with_width(greater_than(5))的事情了。 注意,必须从插件注册方法返回self.register(...), 因为(a)您可能希望将它们链接起来,并且(b)链接的结果仍然是 需要一个匹配者。

多组分匹配器

这个匹配器允许编写Kwarg样式的匹配器(如第二个示例所示 上面的),更像是Python,但如果你想看的话,看起来有点不自然。 与另一个匹配器而不是普通值匹配。我要表明我的意思 一分钟后。

一般方法与多组件匹配器相同:您编写 你的组件的匹配器插件,然后你注册它们到你的主 匹配器:

classGrailMatcher(KwargMulticomponentMatcher):def__init__(self,holy=None,width=None):self.register_for_kwarg(HolinessMatcher(holy),holy)self.register_for_kwarg(GrailWidthMatcher(width),width)

然后在您的测试中:

deftest_correct_width_wrong_holiness(self,my_grail):assert_that(my_grail,grail(holy=True,width=4))

正如我之前所说,这看起来更像是Python,但是,如果你想检查你的 对匹配项的值,而不仅仅是普通值(如这里的width=4),您的 代码看起来有点奇怪:

deftest_correct_width_wrong_holiness(self,my_grail):assert_that(my_grail,grail(holy=True,width=greater_than(4)))

我的建议是如果你知道 主匹配器可以这样使用。

演示

您可以在本报告的demo文件夹中找到这两种方法的演示。 克隆它,从demo/requirements.txt安装需求,然后运行 pytest演示/测试

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

推荐PyPI第三方库


热门话题
java如何将HashMap<String,Object>从一个活动传递到另一个活动   java如何手动加密socket连接的流量?   java正则表达式生成一个不正确的结果   Java方法引用具有泛型参数的方法   java app setBackground()错误:不兼容的类型:int无法转换为Drawable   java是启动Spring引导而不是SpringApplication的其他方法。跑   无法打开java类路径资源[org/quartz/impl/jdbcjobstore/tables_h2.sql],因为它不存在   spring使用Java,如何确定来自tomcat Web服务器的出站服务调用?   java获取多个同名的XML元素JAXB   java使用Ant从同一代码库构建Swing和Android应用程序   JComponent的java重绘方法不起作用   java目标不可访问,标识符“beanName”解析为null   smtp是否有支持esmtp管道的java api?   java如何在Spring中自动连接业务对象   java在Hibernate中没有其他保存实体的方法吗?   针对两个客户机的SpringJavaWeb应用程序项目开发   使用split的java标记化输入