dart-lang / test

A library for writing unit tests in Dart.
https://pub.dev/packages/test
495 stars 213 forks source link

Bring --update-goldens from Flutter to pure Dart #2057

Open alexeyinkin opened 1 year ago

alexeyinkin commented 1 year ago

In Flutter, there is a concept of golden tests and the following support for it:

All of that is useful in pure Dart as well.

One case I came across was testing code generators. The standard way for that is source_gen_test package. It currently requires to inline the expected generated code, and I have prepared an update that allows that to be extracted to golden files. Unfortunately, in pure Dart there is no way to inform the test that it needs to overwrite goldens, so I am considering an ugly workaround with an environment variable:

SOURCE_GEN_TEST_UPDATE_GOLDENS=1 dart test my_test.dart

It would be much cleaner to write:

dart test --update-goldens my_test.dart

Other uses of this feature include testing all kinds of serializers, format converters, etc.

jakemac53 commented 1 year ago

My general thoughts:

natebosch commented 1 year ago
  • We could possibly explore implementing all the goldens stuff here, but I would rather see it in a separate package (package:goldens_test or similar?).

Yes, this is what I would prefer.

  • The environment variable is an OK workaround for now. It can only be read from VM tests, but only VM tests can touch the file system so that is kind of a good thing. So we don't have to rush into a solution, given a workaround is available.

+1 - I agree that a better API for passing in arguments to main or something along those lines would be cleaner, and would be nice to have in general. Even with that feature I think I'd expect the implementation to live in a separate package, and I don't think that feature is going to be high enough priority to get above the line anytime soon, so an environment variable seems like good tradeoff.