Does Dart support parameterized unit tests?

Dart’s test package is smart in that it is not trying to be too clever. The test function is just a function that you call, and you can call it anywhere, even inside a loop or another function call.
So, for your example, you can do something like:

group("formatDay should format dates correctly:", () {
  var inputsToExpected = {
    DateTime(2018, 11, 01): "Thu 1",
    ...
    DateTime(2018, 11, 07): "Wed 7",
    DateTime(2018, 11, 30): "Fri 30",
  };
  inputsToExpected.forEach((input, expected) {
    test("$input -> $expected", () {
      expect(formatDay(input), expected);
    });
  });
});

The only important thing to remember is that all the calls to test should happen synchronously when the main function is called, so no calling it inside asynchronous functions. If you need time to set something up before running the test, do so in a setUp instead.

You can also create a helper function, and drop the map entirely (this is what I usually do):

group("formatDay should format dates correctly:", () {
  void checkFormat(DateTime input, String expected) {
    test("$input -> $expected", () {
      expect(formatDay(input), expected);
    });
  }
  checkFormat(DateTime(2018, 11, 01), "Thu 1");
  ...
  checkFormat(DateTime(2018, 11, 07), "Wed 7");
  checkFormat(DateTime(2018, 11, 30), "Fri 30");
});

Here each call of checkFormat introduces a new test with its own name, and each of them can fail individually.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)