【发布时间】:2022-11-10 04:18:58
【问题描述】:
我的一个包含 7 个空测试的测试文件正在占用5+ 秒开始运行。我在 VS Code 中使用了 flutter test 和 Dart 测试运行器,它们都花费了大约相同的时间。
我的大约 150 个测试的整个测试套件接管了30秒完成,因为每个测试文件都需要 3-7 秒才能开始运行。
launch.json(用于 Dart 测试运行器) {
"name": "Dart: Run all tests",
"type": "dart",
"request": "launch",
"program": "./test/"
},
定时试运行:
00:05 +1 -7: Some tests failed.
real 0m9.532s
user 0m0.075s
sys 0m0.138s
我正在使用嵌套组块,但我不认为这会导致如此巨大的延迟,从而使 TDD 极其缓慢
代码示例部分void main() {
group('AuthCubit:', () {
late AuthCubit authCubit;
late MockLogIn logIn;
late MockLogOut logOut;
late MockRestoreSession restoreSession;
setUp(() {
logIn = MockLogIn();
logOut = MockLogOut();
restoreSession= MockRestoreSession();
authCubit = AuthCubit(
logIn : logIn,
logOut : logOut,
restoreSession: restoreSession,
);
});
tearDown(() {
authCubit.close();
});
test('initial state is unauthenticated', () async {
expect(authCubit.state, equals(const AuthStateUnauthenticated()));
});
group('logIn():', () {
//? Create late variables that all stages use, if needed
setUp(() {});
//? Create subgroups for stages this function will step through
group('On failure:', () {
setUp(() {});
test('emits unauthenticated state', () async { throw UnimplementedError(); });
});
group('On success:', () {
setUp(() {});
test('emits authenticated state', () async { throw UnimplementedError(); });
});
});
});
// ... + similar empty tests for other functions ...
}
我什至尝试过单独和组合测试两个空示例测试文件(见下面的代码)两者都需要 4+ 秒才能单独运行.如果我将两个测试文件合并到一个文件中,执行时间几乎与一个测试相同。
问题似乎在于启动每个测试文件,而不是测试本身。
测试
test_test_1.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 1:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
test_test_2.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 2:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
test_test_1_2.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 1:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
group('Test File 2:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
结果
test_test_1.dart 个人结果00:04 +0 -5: Some tests failed.
real 0m8.743s
user 0m0.060s
sys 0m0.167s
`test_test_2.dart` 个人结果00:05 +0 -5: Some tests failed.
real 0m8.982s
user 0m0.137s
sys 0m0.106s
`test_test_1_2.dart` 个人结果00:04 +0 -10: Some tests failed.
real 0m8.602s << Note this is actually FASTER than the smaller test files
user 0m0.045s << same ^
sys 0m0.200s
所有 3 个测试文件结果(一次运行)00:08 +0 -20: Some tests failed.
real 0m12.696s
user 0m0.015s << Weirdly, this is the smallest value out of all test cases
sys 0m0.152s
问题
我的代码、设置(软件或硬件)、Dart/Flutter 或其他任何问题可能是什么问题?
假装我对 Flutter、Dart 或 VS Code 一无所知;哪些问题有助于找到潜在的原因和解决方案?
【问题讨论】:
标签: unit-testing optimization flutter-test execution-time dart-test