Every Test

Write and execute multiple combination of the same test with less code with every_test package.

Features

  • Write unit test with various parameter values

  • Write golden tests with various finders and parameters

Getting started

flutter pub add --dev every_test

Usage

Simple unit test

Start by calling everyTest method in any *_test.dart file

main() {
    everyTest(/***/);
}

Then give it a description

everyTest(
    'my first everyTest test',
    ...
);

Give it the test itself using parameter

...
'my first everyTest test',
of: (param) {
    final repository = SampleRepository();
    repository.values = param;
    return repository.average;
},
...    

Last but not least, give it the list of expected results depending on parameter using param(foo).gives(bar) syntax

...
of: (param) {
    ...
},
expects: [
    param([1, 2, 3]).gives(2),
],
...

Then add as many combination as wanted and voilĂ  !

everyTest(
    'Expected average values computation',
    of: (param) {
        final repository = SampleRepository();
        repository.values = param;
        return repository.average;
    },
    expects: [
        param([1, 2, 3]).gives(2),
        param([4, 5, 6]).gives(5),
        param([7, 8, 9]).gives(8),
        param([9, 9, 9]).gives(9),
    ],
);

Using several parameters a time

You may need to send multiple parameters for test execution. Just use Json syntax. That's it.

everyTest(
    'translation test',
    of: (params) {
      // GIVEN
      final locale = params['locale'];
      final key = params['key'];
      final repository = TranslationsRepository(currentLocale: locale);

      // WHEN
      return repository.translate(key);
    },
    expects: [
      // THEN
      param({'locale': 'en', 'key': 'app_name'}).gives('sample'), // en translation
      param({'locale': 'fr', 'key': 'app_name'}).gives('exemple'), // fr translation
      param({'locale': 'es', 'key': 'app_name'}).gives('sample'), // default translation
      param({'locale': 'en', 'key': 'hello'}).gives('!hello!'), // not found translation
    ],
  );

Golden tests

 everyTestGolden(
    'every test golden',
    of: (tester, color) async {
      await tester.pumpWidget(MyApp(primarySwatch: color));
    },
    expects: [
      finder(find.byType(MyApp)).matches(Colors.blue, 'goldens/blue.png'),
      finder(find.byType(MyApp)).matches(Colors.red, 'goldens/red.png'),
      finder(find.byType(MyApp)).matches(Colors.green, 'goldens/green.png'),
    ],
  );

Libraries

every_test
golden_test
unit_test