Summary - Testing and CI/CD
- Flutter course
- Advanced
- Lesson 65/69
- 4 min
Combine testWidgets and CI to make a quality gate.
Combining flutter_test and CI
In a CI workflow, the flutter test step runs every testWidgets test in the test/ folder. If tests pass, CI goes green and the code can be merged; if they fail, CI goes red and the merge is blocked. This creates a quality gate.
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
- run: flutter testPractice Two button test
In main, write a testWidgets test named 'iki buton'. Render MaterialApp(home: Row(children: [ElevatedButton(onPressed: () {}, child: Text('A')), ElevatedButton(onPressed: () {}, child: Text('B'))])) with pumpWidget. Verify with find.byType(ElevatedButton) and findsNWidgets(2). Finally print 'ci: flutter test'.
Starter code
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
// 'iki buton' adlı testWidgets testi yaz
// pumpWidget ile MaterialApp içinde Row ve iki ElevatedButton render et
// find.byType ile ElevatedButton bul ve 2 adet bulunduğunu doğrula
// son olarak 'ci: flutter test' yazdir
}Sample solution
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
testWidgets('iki buton', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Row(
children: [
ElevatedButton(onPressed: () {}, child: const Text('A')),
ElevatedButton(onPressed: () {}, child: const Text('B')),
],
),
),
);
expect(find.byType(ElevatedButton), findsNWidgets(2));
});
print('ci: flutter test');
}Expected output
ci: flutter test
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the flutter test step do in CI?
- Loads to device
- Runs all tests Correct answer
- Compiles code
- Installs packages
Explanation: The flutter test command runs every _test.dart file in the test/ folder; CI uses this step as a quality gate.
2 flutter test command
- run: ____
- run: flutter test
Explanation: In a CI workflow, the line - run: flutter test runs all tests; flutter run opens the app on a device, flutter build produces a package.
3 Test and CI combination
| Term | Meaning |
|---|---|
flutter_test |
Test package |
testWidgets |
Test function |
GitHub Actions |
CI tool |
flutter test |
Test run command |