Search code examples
javaapproval-tests

How to use ApprovalTests for approving logs


I want to use Approval Tests to verify log output. What is the preferred way to deal with dynamic content of the log text, e.g. time stamps? The included FileApprover always compares the files for exact equality.


Solution

  • There are a couple solutions to this. Here they are in order of preference:

    1. Don't log dynamic content during the test.

    2. Fake dynamic content during the test so the output is consistent.

    3. Scrub the log file before comparing. This usually looks like:

      ScrubFile("log.txt", "log_scrubbed.txt", new DateScrubber(), new GuidScrubber())