首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyhamcrest修改"got“描述

pyhamcrest修改"got“描述
EN

Stack Overflow用户
提问于 2014-02-17 16:27:21
回答 1查看 133关注 0票数 1

我已经编写了一个自定义的hamcrest匹配器,用于检查列表中的文件是否已被复制。列表可能很长(1000 files+),所以如果缺少一个文件,我不希望matcher打印出整个列表。

我可以对缺少的文件进行自定义描述,但是是否也有方法修改Got: <list of files>部件?

完整代码:

代码语言:javascript
复制
class FilesHaveBeenCopied(BaseMatcher):
    def __init__(self):
        self.missing = None

    def _matches(self, source_files):
        try:            
            self.missing = next(f for f in source_files if not os.path.exists(target_of(f)))
        except StopIteration:
            return True
        return False

    def describe_to(self, description):            
        description.append_text("file to be copied '{0}'".format(self.missing)) 

def have_been_copied():
    return FilesHaveBeenCopied()

用法:

代码语言:javascript
复制
assert_that(self.source_files, have_been_copied())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 03:10:04

重写describe_mismatch以覆盖全部实际值:

代码语言:javascript
复制
def describe_mismatch(self, actual, description):
    description.append(self.missing)

您的describe_to应该描述预期值--而不是缺少的值。或者,它应该报告像“21个现有文件的列表”这样的文件数量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21834281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档