runTests method
Runs tests for a single test group file. Returns TestResult for the test group run.
Implementation
Future<TestResult> runTests({
required int shardIndex,
required int totalShardCount,
required String filePath,
required String? resultsFilePath,
bool? isCoverageEnabled,
String? coveragePath,
bool? keepGeneratedTestGroups,
}) async {
try {
String? resultsFile;
if (resultsFilePath != null) {
resultsFile = await _createResultsFile(
path: resultsFilePath,
testGroupName: 'results_$shardIndex',
);
}
final stopwatch = Stopwatch()..start();
final progressMap = <int, TestProgress>{};
_logInfo(
'Running test group: ${shardIndex + 1}/$totalShardCount',
);
final coverageArgs = <String>[];
if (isCoverageEnabled == true) {
coverageArgs.add('--coverage');
}
if (coveragePath != null) {
coverageArgs.addAll(['--coverage-path', coveragePath]);
}
final process = await Process.start(
'flutter',
['test', filePath, '--machine', ...coverageArgs],
);
await process.stdout.transform(utf8.decoder).forEach(
(line) => _handleLine(
line: line,
progressMap: progressMap,
resultsFilePath: resultsFile,
),
);
stopwatch.stop();
final duration = Duration(milliseconds: stopwatch.elapsedMilliseconds);
var successCount = 0;
var failureCount = 0;
var failed = await process.exitCode != 0;
final errorTests = <String>[];
final testFile = File(filePath);
final testFileLines =
testFile.existsSync() ? testFile.readAsLinesSync() : <String>[];
for (final progress in progressMap.values) {
if (progress.completed) {
if (progress.failed) {
failureCount += 1;
failed = true;
final rootLine = progress.test.rootLine;
if (rootLine != null &&
rootLine > 0 &&
rootLine <= testFileLines.length) {
errorTests
.add(testFileLines[rootLine - 1].trim().replaceAll(';', ''));
}
} else {
successCount += 1;
}
}
}
_logInfo(
'Completed test group ${shardIndex + 1}/$totalShardCount',
);
return TestResult(
index: shardIndex,
filePath: filePath,
isSuccess: !failed,
duration: duration,
totalTestsCount: progressMap.length,
successTestsCount: successCount,
errorTestsCount: failureCount,
errorTests: errorTests);
} catch (error, stackTrace) {
_logError(
'Running test failed',
error: error,
stackTrace: stackTrace,
);
return TestResult(
index: shardIndex,
filePath: filePath,
isSuccess: false,
duration: Duration.zero,
totalTestsCount: 0,
successTestsCount: 0,
errorTestsCount: 1,
errorTests: [filePath]);
}
}