Skip to content

Instantly share code, notes, and snippets.

@bartekpacia
Last active September 19, 2023 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartekpacia/06b1712c2edb191aea6f1c93d44289ef to your computer and use it in GitHub Desktop.
Save bartekpacia/06b1712c2edb191aea6f1c93d44289ef to your computer and use it in GitHub Desktop.
Flutter Silesia #6 snippets
PatrolTester $;
$.native.grantPermissionWhenInUse()
$.native.grantPermissionOnlyThisTime();
$.native.denyPermission();
$.native.pressHome();
$.native.openNotifications();
$.native.tapOnNotificationBySelector(
Selector(textContains: 'Someone likes'),
);
void main() {
test('alpha', () {});
test('bravo', () {});
test('charlie', () {});
}
import 'package:test_api/src/backend/invoker.dart';
void main() {
test('test_explorer', () {
final implicitTopLevelGroup = Invoker.current!.liveTest.groups.first;
_printGroupEntry(implicitTopLevelGroup);
});
test('alpha', () {});
test('bravo', () {});
test('charlie', () {});
}
$ dart test test/example_test.dart
Group:
Test: alpha
Test: bravo
Test: charlie
00:00 +4: All tests passed!
String requestedTest = "bravo"; // assume this gets passed in
void main() {
test('alpha', () {});
test('bravo', () {});
test('charlie', () {});
}
String requestedTest = "bravo"; // assume this gets passed in
void main() {
patrolTest('alpha', () {});
patrolTest('bravo', () {});
patrolTest('charlie', () {});
}
void patrolTest(String testName, dynamic Function() body) {
test(testName, () async {
if (testName == requestedTest) {
await body();
print('executed $testName');
} else {
print('skipped $testName');
}
});
}
$ dart test test/example_test.dart
skipped alpha
executed bravo
skipped charlie
00:00 +3: All tests passed!
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(/* ... */);
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
test('login flow', () { /* ... */ });
}
void main() {
PatrolBinding.ensureInitialized();
patrolTest('login flow', ($) async { /* ... */ });
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
String? _currentTest;
Map<String, String> _testResults = {};
PatrolBinding() {
setUp(() {
_currentTest = Invoker.current!.liveTest.parentGroupName;
});
final oldReportTestException = reportTestException;
reportTestException = (details, testDescription) {
_testResults[_currentTest] = Failure(testDescription, '$details');
oldReportTestException(details, testDescription);
};
tearDown(() {
final result = _testResults[_currentTest];
await patrolAppService.markDartTestAsCompleted(result);
});
}
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
String? _currentTest;
Map<String, String> _testResults = {};
PatrolBinding() {
setUp(() {
_currentTest = Invoker.current!.liveTest.parentGroupName;
});
}
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
String? _currentTest;
Map<String, String> _testResults = {};
PatrolBinding() {
// remember current test
final oldReportTestException = reportTestException;
reportTestException = (details, testDescription) {
_testResults[_currentTest] = Failure(testDescription, '$details');
oldReportTestException(details, testDescription);
};
}
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
String? _currentTest;
Map<String, String> _testResults = {};
PatrolBinding() {
// remember current test
// listen for failed tests
tearDown(() {
final result = _testResults[_currentTest];
await patrolAppService.markDartTestAsCompleted(result);
});
}
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
String? _currentTest;
Map<String, String> _testResults = {};
PatrolBinding() {
// remember current test
// listen for failed tests
// send results to instrumentation
}
}
class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
@override
Future<void> runTest(
Future<void> Function() testBody,
VoidCallback invariantTester, {
String description = '',
Duration? timeout,
}) async {
await super.runTest(
testBody,
invariantTester,
description: description,
);
_testResults[description] ??= _success;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment