How to Pass Arguments to Test App?
Whilst command line arguments are not supported in the traditional console command sense, you can use --dart-define
to pass variables to the main test class. (Although if you are testing a desktop platform you can use the -a
argument to Flutter Driver)
Under the covers it uses environment variables to pass arguments in.
See How do you pass arguments from command line to main in Flutter/Dart? for more details.
How to Pass Arguments to Each Test?
From within the test, the environment variables are not available, so we can use the DataHandler of the Flutter Driver Extension to get the data.
In your main test app, the one that Flutter Driver uses to start the test, you can set up a DataHandler, like this:
void main() async {
const testsString = String.fromEnvironment("tests");
enableFlutterDriverExtension(handler: (request) async {
String returnString;
switch (request) {
case "get_tests":
{
returnString = testsString;
break;
}
}
return returnString;
});
// Call main app
app.e2e(configName: 'server-1');
}
Now, from your individual tests, you can call the DataHandler and request the 'command arguments' as data:
// A test file
void main() async {
// Connect to app
FlutterDriver driver = await FlutterDriver.connect();
String testsString = await config.driver.requestData("get_tests");
print("Args: $testsString");
}
Kick off the tests like this:
flutter drive --profile --target ./test_driver/app.dart --dart-define="tests=home_page" --dart-define="sample_data=ABC;XYZ"