Source: https://developer.ibm.com/devpractices/software-development

TDD?

Why TDD?

Reduce Bugs

Our Code Guardian

Code Documentation

TDD Phases

Coverage

Implementation

First state
Second state
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:simpk/config/theme.dart';
Future _pumpTestableWidget(WidgetTester tester, Widget child) async {
ThemeData theme = appTheme();
await tester.pumpWidget(MaterialApp(home: child, theme: theme));
}
void main() {
testWidgets('Create money return report: initial state'
(tester)async {
await _pumpTestableWidget(tester, Scaffold(
body:CreateMoneyReturn())
);

expect(
find.text('Tambah Laporan Pengembalian Uang'),
findsOneWidget
);

expect(
find.text('Deskripsi Pengembalian Uang'), findsOneWidget);

expect(find.textContaining('Upload Foto'), findsOneWidget);

expect
(find.text('Batal'), findsOneWidget);
expect(find.text('Simpan'), findsOneWidget);
});
testWidgets('Create money return report: Tap Batal to go back'
(WidgetTester tester) async {
await _pumpTestableWidget(tester, Scaffold(
body:CreateMoneyReturn()));
await tester.pump();

await tester.tap(find.text("Batal"));
await tester.pumpAndSettle();
expect(find.byType(Scaffold), findsNothing);
});
testWidgets(
"Create money return report: Simpan enabled when description is filled",
(tester) async {
await _pumpTestableWidget(tester, CreateReport1Screen());

ElevatedButton btn = tester.firstWidget(find.byType(ElevatedButton).last);
expect(btn.enabled, false);
await tester.enterText(
find.byType(TextFormField).first,
"Suatu gambar pengembalian uang");
await tester.pump();
btn = tester.firstWidget(find.byType(ElevatedButton).last);
expect(btn.enabled, true);
});
group('$ImagePicker', () {
const MethodChannel channel =
MethodChannel('plugins.flutter.io/image_picker');
final List<MethodCall> log = <MethodCall>[]; setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
return '';
});
log.clear();
});
testWidgets(
'Add Attachment: Take image for choosing image works'
(WidgetTester tester) async {
await _pumpTestableWidget(tester,
Scaffold(body:CreateMoneyReturn()));
await tester.tap(find.textContaining("Upload Foto"));
await tester.pump();

expect(
log,
<Matcher>[
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
}),
],
);
});
});
}

Conclusion

Computer Science Student at Universitas Indonesia