Skip to content

Instantly share code, notes, and snippets.

@pszklarska
Created January 2, 2021 00:00
Show Gist options
  • Save pszklarska/4b5140cd485d78e1d9c8002f4ac84b26 to your computer and use it in GitHub Desktop.
Save pszklarska/4b5140cd485d78e1d9c8002f4ac84b26 to your computer and use it in GitHub Desktop.
Flutter Testing - Given-When-Then Example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Home(user: User()),
);
}
}
class Home extends StatelessWidget {
final User user;
const Home({Key key, this.user}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Test Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Tap me!'),
onPressed: () => _onButtonPressed(context),
),
],
),
),
);
}
void _onButtonPressed(BuildContext context) {
if (user.isLoggedIn) {
showDialog(
context: context,
builder: (context) => AlertDialog(title: Text('I\'m logged in!')),
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(title: Text('I\'m logged out...')),
);
}
}
}
class User {
bool get isLoggedIn => true;
}
name: flutter_testing
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.0
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.3
flutter:
uses-material-design: true
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_testing/main.dart';
import 'package:mockito/mockito.dart';
void main() {
testWidgets(
'GIVEN user is logged in '
'WHEN user taps on a button '
'THEN dialog is shown', (tester) async {
// given
final user = UserMock();
when(user.isLoggedIn).thenReturn(true);
// when
await tester.pumpWidget(MaterialApp(home: Home(user: user)));
await tester.tap(find.byType(FlatButton));
await tester.pump();
// then
expect(find.text('I\'m logged in!'), findsOneWidget);
});
testWidgets(
'GIVEN user is logged in '
'WHEN user taps on a button '
'THEN dialog is shown', (tester) async {
// given
final user = UserMock();
when(user.isLoggedIn).thenReturn(false);
// when
await tester.pumpWidget(MaterialApp(home: Home(user: user)));
await tester.tap(find.byType(FlatButton));
await tester.pump();
// then
expect(find.text('I\'m logged out...'), findsOneWidget);
});
}
class UserMock extends Mock implements User {}
@fzyzcjy
Copy link

fzyzcjy commented May 8, 2022

Hi, I see your interesting article about tests. I have just open-sourced a convenient_test package, which helps to write and debug tests easily, with action history, time travelling, screenshots, rapid re-execution, video recordings, interactive mode and more. Do you have interest in making an attempt :)

Link: https://github.com/fzyzcjy/flutter_convenient_test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment