Skip to content

Make autogold tests more readable

Administrator requested to merge cc/simplify-autogold into main

Created by: camdencheek

Stacked on #34595

This is a rather opinionated change, so feel free to speak up if you prefer the old style.

Basically, I really struggle to read, use, and update autogold tests when written in the current style.

  1. The files are massive, meaning I have to do a lot of scrolling to see what cases are being tested. Additionally, the big multi-line strings make it tough to grok since it breaks my mental indentation model.
  2. The tests read backwards. The expected output comes before the input. This means I often read the test name, then scroll down to the test input, then scroll back up to the test output.
  3. When a test fails, it's difficult to tell which one is failing because the test names aren't useful.
  4. When tests change, I can't autoupdate all tests at the same time. I have to do something like while true ; do go test ./package -update ; done

This changes the tests in a couple of ways to address these.

  • All test cases have a name and an input string, and all test inputs are in a single table.
  • Each case is run as a separate sub test. This fixes both (3) and (4) because each subtest has a different name, and autogold can update all the subtests in one -update run.
  • For tests with large, multiline outputs (i.e. the updated test), the output contents are moved into separate test files, taking advantage of autogold.Equal.

The disadvantage is, for large test outputs like this test, the "expected" value is not in-file.

Writing this all up because I'd like to propose this as a better autogold pattern moving forward.

Test plan

I ran the tests.

Merge request reports

Loading