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

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驱动程序连接时,mongodb服务器上的SSLhandshake失败   使用PlayFramework的Azure网站中的java Logback   java在另一个ArrayList中使用ArrayList处理复杂的JSON响应   java无法在另一台机器上运行eclipse tomcat中的war文件   java GZIPOutputStream有什么替代方案吗?   java Nashorn调试在Nashorn中运行的javascript   java文本短信未发送,即使toast显示已发送   java Hibernatesearch 5.0 spatial不确定是否在散列中存储lat/lon   java我想创建一个带有文本视图的计数器   java安卓:如何正确地同步资源   java使用mockito。当不知道方法调用的参数时   firebase Java使用HTTP v1发送错误字符的中文通知   java Hibernate无法映射到表?   java使用对象映射器解析复杂JSON   java Selenium Grid 2并行测试用例执行   java所有项目在列表视图中重复