Question

I've written a custom hamcrest matcher for checking that files in a list have been copied. The list could be long (1000 files+), so I don't want the matcher to print out the whole list if one file is missing.

I can make a custom description for the missing file, but is there a way to modify the Got: <list of files> part as well?

Complete code:

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

Usage:

assert_that(self.source_files, have_been_copied())
Was it helpful?

Solution

Override describe_mismatch to override the full actual value:

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

Your describe_to should describe the expected value--not the missing value. Or perhaps it should just report the number of files like "a list of 21 existing files".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top