integration_test
패키지와 flutter_test
패키지 2개를 설치해야 한다. (flutter_driver
패키지는 integration_test
패키지로 마이그레이션 됌)
dev_dependencies:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
실제로 애플리케이션을 구동시키는 것은 아니기 때문에, lib 폴더가 아니라 별도로 test_drvier 폴더를 만들어서 거기에서 통합 테스트를 진행하는 방법을 추천한다.
먼저, lib 폴더에 다음과 같은 카운터 앱이 있다고 하면
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Counter App',
home: MyHomePage(title: 'Counter App Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter', // Provide a Key to this specific Text widget. This allows
// identifying the widget from inside the test suite,
// and reading the text.
key: const Key('counter'), style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
// Provide a Key to this button. This allows finding this
// specific button inside the test suite, and tapping it.
key: const Key('increment'), onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add),
),
);
}
}
test\\test_driver
폴더에 임의로 다음과 같은 integration_test.dart
파일을 만들어 준다.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hello_world_flutter/counter_app.dart' as app; //2번 방식
import 'package:integration_test/integration_test.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end to end test', () {
testWidgets('tab on the floating action button', (widgetTester) async {
// 1. await widgetTester.pumpWidget(MaterialApp(home: Scaffold(body: app.MyHomePage(title: 'aa'))));
// 2. app.main();
await widgetTester.pumpAndSettle(); // pump() 도 가능
expect(find.text('0'), findsOneWidget);
final Finder fab = find.byTooltip('Increment');
//final Finder fab = find.byKey(const Key('increment')); 도 가능
await widgetTester.tap(fab);
await widgetTester.pumpAndSettle(); // pump() 도 가능
expect(find.text('1'), findsOneWidget);
});
});
}
웹 브라우저로 테스트를 진행하려면 해당 링크 참고