Skip to content

Instantly share code, notes, and snippets.

@mbfakourii
Created July 23, 2022 12:58
Show Gist options
  • Save mbfakourii/f30348250bd19eff9d734aa5d3d62c87 to your computer and use it in GitHub Desktop.
Save mbfakourii/f30348250bd19eff9d734aa5d3d62c87 to your computer and use it in GitHub Desktop.
flutter test example
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:ahille/calculator_test.dart';
import 'package:ahille/home_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ahille/main.dart';
void main() {
testWidgets('home page contains hello world text', (WidgetTester tester) async {
final testWidget = MaterialApp(
home: HomePage(),
);
await tester.pumpWidget(testWidget);
await tester.pumpAndSettle();
final buttonMaterial = find.descendant(
of: find.byType(ElevatedButton),
matching: find.byType(Material),
);
final materialButton = tester.widget<Material>(buttonMaterial);
expect(materialButton.color, Colors.blue);
expect(find.text('Weather today'), findsOneWidget);
expect(find.byKey(const Key('icon_weather')), findsOneWidget);
});
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
group('tests for add component', () {
late Calculator calculator;
setUp(() {
calculator = Calculator();
});
test('should return a + b when a and b are two positives.', () {
// arrange
int a = 10;
int b = 20;
int expectedResult = a + b;
// act
int actualResult = calculator.add(a, b);
// assert
expect(actualResult, expectedResult);
});
test('should return a + b when a and b are two negatives.', () {
// arrange
int a = -10;
int b = -20;
int expectedResult = a + b;
// act
int actualResult = calculator.add(a, b);
//assert
expect(actualResult, expectedResult);
});
test('should return a + b when a is negative and b is positive', () {
// arrange
int a = -10;
int b = 20;
int expectedResult = a + b;
// act
int actualResult = calculator.add(a, b);
//assert
expect(actualResult, expectedResult);
});
test('should return a + b when a is positive and b is negative.', () {
// arrange
int a = -10;
int b = -20;
int expectedResult = a + b;
// act
int actualResult = calculator.add(a, b);
//assert
expect(actualResult, expectedResult);
}); tearDown(() {
// Nothing to do here.
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment