Golden Literal Testing in UTest
9 months ago
- #automation
- #scala
- #unit-testing
- uTest 0.9.0 introduces Golden testing, also known as Snapshot testing, which compares code output against pre-defined 'Golden' values.
- Golden tests are useful for large outputs that shouldn't be manually maintained, such as logs, ensuring they 'look right' without unexpected changes.
- The new `assertGoldenLiteral` method simplifies test setup and maintenance by automatically updating test assertions when the output changes.
- Users can update multiple golden tests at once by running tests with `UTEST_UPDATE_GOLDEN_TESTS=1`, which modifies the source code directly.
- `assertGoldenLiteral` works by capturing the location of the golden value in the source code, allowing it to update the value when discrepancies are found.
- The feature is inspired by the desire to reduce tedious manual updates in test suites, making test maintenance more efficient.
- While `assertGoldenLiteral` is powerful, it's limited to literals that can be pretty-printed, such as primitives, collections, and case classes.
- The implementation leverages the PPrint library for well-formatted output and uses source code manipulation to update test assertions automatically.