pyhamcrest修改“got”描述

2024-10-03 02:38:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我可以对丢失的文件进行自定义描述,但是有没有办法修改Got: <list of files>部分呢?你知道吗

完整代码:

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()

用法:

assert_that(self.source_files, have_been_copied())

Tags: 文件oftoselfsource列表returndef
1条回答
网友
1楼 · 发布于 2024-10-03 02:38:18

重写describe_mismatch以重写完整的实际值:

def describe_mismatch(self, actual, description):
    description.append(self.missing)

您的describe_to应该描述期望值,而不是缺少的值。或者它应该只报告文件的数量,比如“21个现有文件的列表”。你知道吗

相关问题 更多 >