当在同一个文件中运行多个测试,并依次运行每个测试时,某些类型的测试会失败。但是,当单独运行这些测试时,它们通过了相同的测试。
目前这是我的测试文件,虽然有点长:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rec/Components/Inputs/text_fields/DniTextField.dart';
import 'package:rec/Helpers/Validators.dart';
import '../../test_utils.dart';
void main() {
testWidgets('DniTextField works with invalid DNI', (WidgetTester tester) async {
var key = GlobalKey<DniTextFieldState>();
var formKey = GlobalKey<FormState>();
var onChangedResult;
var widget = Form(
key: formKey,
child: DniTextField(
key: key,
onChange: (String value) {
onChangedResult = value;
},
validator: (s) => Validators.verifyIdentityDocument(s),
),
);
await tester.pumpWidget(
TestUtils.wrapPublicRoute(widget),
);
await tester.pumpAndSettle();
// Test that widget has at least rendered
TestUtils.widgetExists(widget);
// Enter text into field with
var widgetFinder = find.byWidget(widget);
await tester.tap(widgetFinder);
await tester.showKeyboard(widgetFinder);
await tester.enterText(widgetFinder, 'invaliddni');
await tester.pumpAndSettle();
expect(onChangedResult, 'invaliddni');
expect(formKey.currentState.validate(), false);
});
testWidgets('DniTextField works with valid DNI', (WidgetTester tester) async {
var key = GlobalKey<DniTextFieldState>();
var formKey = GlobalKey<FormState>();
var onChangedResult;
var widget = Form(
key: formKey,
child: DniTextField(
key: key,
onChange: (String value) {
onChangedResult = value;
},
validator: (s) => Validators.verifyIdentityDocument(s),
),
);
await tester.pumpWidget(
TestUtils.wrapPublicRoute(widget),
);
await tester.pumpAndSettle();
// Enter text into field
var widgetFinder = find.byType(DniTextField);
await tester.tap(widgetFinder);
await tester.showKeyboard(widgetFinder);
await tester.enterText(widgetFinder, '80008000k');
await tester.pumpAndSettle();
expect(onChangedResult, '80008000k');
expect(formKey.currentState.validate(), true);
});
testWidgets('DniTextField works with valid DNI with trailing space', (WidgetTester tester) async {
var key = GlobalKey<DniTextFieldState>();
var formKey = GlobalKey<FormState>();
var onChangedResult;
var widget = Form(
key: formKey,
child: DniTextField(
key: key,
onChange: (String value) {
onChangedResult = value;
},
validator: (s) => Validators.verifyIdentityDocument(s),
),
);
await tester.pumpWidget(TestUtils.wrapPublicRoute(widget));
await tester.pumpAndSettle();
// Enter text into field
var widgetFinder = find.byWidget(widget);
await tester.tap(widgetFinder);
await tester.showKeyboard(widgetFinder);
await tester.enterText(widgetFinder, '80008000k ');
await tester.pumpAndSettle();
// The value emitted by the field, should be free of trailing whitespace
expect(onChangedResult, '80008000k');
expect(formKey.currentState.validate(), true);
});
}
如果我这样运行:
$ flutter test test/Components/inputs/dni_text_field_test.dart
第一个测试通过了,但接下来的两个测试未通过,会输出以下错误信息:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
The finder "zero widgets with the given widget
(DniTextField-[LabeledGlobalKey<DniTextFieldState>#0fb86]) (ignoring offstage widgets)" (used in a
call to "tap()") could not find any matching widgets.
When the exception was thrown, this was the stack:
#0 WidgetController._getElementPoint (package:flutter_test/src/controller.dart:902:7)
#1 WidgetController.getCenter (package:flutter_test/src/controller.dart:841:12)
#2 WidgetController.tap (package:flutter_test/src/controller.dart:273:18)
#3 main.<anonymous closure> (file:///[masked]/test/Components/inputs/dni_text_field_test.dart:99:18)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
如果我注释掉第一个测试,那么第二个测试就会通过,但第三个测试不会。正如我所说的,如果我单独运行它们,这些测试都会通过。
我找不到任何相关信息,甚至没有一条。也许有人可以指导我正确的方向。我对Flutter小部件测试有些陌生,可能会忽略一些重要的事情。
目前我唯一找到的解决方案是将每个测试放在单独的文件中。虽然这并不理想,但将相关测试包含在同一文件中会更好。Flutter有示例显示允许在同一文件中包含多个测试。
Flutter版本:2.4.0-5.0.pre.87