Flutter › Unit Testing in Flutter
How do you properly write parameterized tests that run the exact same test logic against several genuinely different sets of input data, avoiding needing to duplicate that exact same test method several genuinely separate times?
AdvancedAnswer
Parameterized testing in Dart typically involves creating a list of genuinely different input and expected output combinations, then properly looping through that particular list and calling the test function once for each individual data set, effectively running the exact same underlying test logic against every single one of those separate genuinely different scenarios, which considerably reduces genuinely duplicated test code and makes it noticeably easier to add coverage for a genuinely new additional edge case simply by adding just one single new entry to that particular existing shared data list.
final testCases = [(1, 1, 2), (2, 3, 5), (-1, 1, 0)];
for (final (a, b, expected) in testCases) {
test('add($a, $b) equals $expected', () {
expect(add(a, b), expected);
});
}Real-world example A team tests their currency conversion function against a dozen genuinely different specific currency pairs using a parameterized approach, avoiding writing a dozen nearly identical separate test methods that would have otherwise only genuinely differed in their specific particular input values.