【问题标题】:Flutter/Dart slow unit tests: each file taking >4s to start runningFlutter/Dart 慢速单元测试:每个文件需要 >4 秒才能开始运行
【发布时间】: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


    【解决方案1】:

    jensjoha 在 GitHub 上 helped me find a workaround 通过创建“嵌套聚合测试文件”来实现

    在更高级别的注释中,我可能应该补充一点,对于您有测试的每个文件,运行测试会变慢,因为它必须进行编译、启动新进程等。同样地——正如我们所看到的——到达它实际上开始对测试做一些事情需要相当长的时间,所以运行 flutter test specificfile.dart (至少对于许多特定文件)并不是很好。 作为一种解决方法 - 当您想要运行所有测试时 - 您可以尝试创建一个测试文件,就像在 #86722 (comment) 中所做的那样,然后只运行它。

    例子

    // testgroup1_test.dart
    
    import test_file_1_test.dart as testFile1;
    import test_file_2_test.dart as testFile2;
    import test_file_3_test.dart as testFile3;
    import test_file_4_test.dart as testFile4;
    
    void main() {
      group("test file 1", testFile1.main);
      group("test file 2", testFile2.main);
      group("test file 3", testFile3.main);
      group("test file 4", testFile4.main);
    }
    
    
    // everything_test.dart
    
    import testgroup1_test.dart as testGroup1
    import testgroup1_test.dart as testGroup2
    import testgroup1_test.dart as testGroup3
    
    void main() {
      group("test group 1", testGroup1.main);
      group("test group 2", testGroup2.main);
      group("test group 3", testGroup3.main);
    }
    
    

    使用这种结构,我可以在运行单个测试文件的同一时间内运行所有(或任何组)测试文件,这对我来说要快约 75%。

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2019-09-21
      • 2010-09-05
      • 1970-01-01
      相关资源
      最近更新 更多