How to create parameterized test run in Flutter test group from dynamically returned data

Issue

I want to create parameterized tests with Flutter. I have forEach loop that I want to use to generate parameterized tests based on what application responds to me. Any ideas how to do it? I thought of:

    group('Group for tests', () {
      test("Get necessary list from app_handler", () {
        expect(app_handler.async_function1(driver).then((value) => myListOfItems), completes;
        expect(myListOfItems, isNotEmpty);
      });

      myListOfItems.forEach((myItem) {
        test("Test for $myItem", () {
          expect(app_handler.async_function2(driver, myItem), completes);
        });
      });

but this does not work. myListOfItems fails empty check in the first test and it should be a populated list.
So I somehow need to prepare test cases dynamically based on what app responds to me during test.

Solution

So thanks to @Alessio discussion and @jamesdlin comment I ended up with solution that involves creating a file with data after one test and then using this file during another test.

     group('Smoke test', () {
       test("Get my vals", () async {
         final myVals= await app_handler.getMyVals(driver);
         expect(myVals, isNotEmpty);
         final file = File('myVals.json');
         file.writeAsString(jsonEncode({'myVals':myVals}));
       });
     }, tags: "Test_1");

    group('Main test', () {
      final file = File('myVals.json');
      final myVals = (json.decode(file.readAsStringSync())['exceptions'] as List).cast<String>();
      myVals.forEach((myVal) {
      test("Test $myVal", () {
        expect(app_handler.async_func(driver, myVal), completes);
      });
    }, tags: "Test_2"););

So test executor will first call test with first tag then with second and during second testrun tests will be generated dynamically.

Answered By – Artur Korobeynyk

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *