Unit Tests
As Unit testing is already very well documented, we will here not provide basic steps how to setup the tests. We will focus on more complex use cases.
Dependencies
Depending on the use case you may run in the situation where even a widget test or unit test require some dependencies. Therefor we have created a dedicated package so simplify the setup of a test.
dependencies:
flutter:
sdk: flutter
m8ty_test:
git:
url: git@gitlab.m8ty.eu:app-framework/m8ty_test.git
Write test
Here we are using an example app to just demonstrate how to setup the dependencies just by using TestConfig class following the builder pattern:
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. 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:example/config/config_refresh.dart';
import 'package:example/config/config_transactions.dart';
import 'package:example/model/m8ty_config.dart';
import 'package:example/model/serializers.dart';
import 'package:example/provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:m8ty_banking_accounts/config.dart';
import 'package:m8ty_banking_transactions/m8ty_banking_transactions.dart';
import 'package:m8ty_business_components/dashboard/dashboard_widget_config.dart';
import 'package:m8ty_test/m8ty_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return const Text("Hi There");
}
}
void main() async {
SharedPreferences.setMockInitialValues({});
Widget appWidget = await TestConfig()
.withDashboardWidgets(transactionsDashboardWidgets)
.withNavigation(navigationBankingTransactions)
.withOverrides(getProviderOverrides)
.withWidgetPlacement(WidgetPlacement.home)
.withSerializers(exampleSerializers)
.withBaseUrlProvider(_loadConfig)
.withMockConfig(_loadConfig)
.withModuleConfigs((ref) => [
BankingTransactionsModuleConfig(TransactionsConfigParam()),
])
.withInitializers([
BankingAccountsModuleConfig.init,
BankingTransactionsModuleConfig.init,
]).withConfigurators([
configureRefresh,
]).buildUnit(() => const MyWidget());
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(appWidget);
await tester.pumpAndSettle();
expect(find.text("Hi There"), findsOneWidget);
});
}
Future<M8tyConfigModel> _loadConfig() async {
return loadFromJson<M8tyConfigModel>('assets/config/configuration.json');
}
Last modified: 10 December 2023