在 Dart 学习路径中测试

了解如何在 Exercism 上测试你的 Dart 练习


下载练习

使用 Exercism CLI 下载你想做的练习。

exercism download --track=dart --exercise=hello-world

然后进入练习目录。

cd /path/to/exercism/dart/hello-world

安装依赖

运行测试之前,先下载练习的依赖。

dart pub get

运行测试

dart test

看懂测试结果

dart test 默认使用 compact 报告器,在一行里实时显示已通过、已跳过和失败的测试数量。

$ dart test
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
  Expected: <3>
    Actual: <0>

  package:matcher                    expect
  test/binary_search_test.dart 13:5  main.<fn>


To run this test again: dart test test/binary_search_test.dart -p vm --plain-name 'finds a value in the middle of an array'
00:00 +1 ~9 -1: Some tests failed.
  • +1:通过了 1 个测试
  • ~9:跳过了 9 个测试
  • -1:失败了 1 个测试

如果需要更详细的单个测试输出,可以使用 expanded 报告器,它会在每个测试运行时显示其结果。

$ dart test -r expanded
00:00 +0: loading test/binary_search_test.dart
00:00 +0: test/binary_search_test.dart: finds a value in an array with one element
00:00 +1: test/binary_search_test.dart: finds a value in the middle of an array
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
  Expected: <3>
    Actual: <0>
  
  package:matcher                    expect
  test/binary_search_test.dart 13:5  main.<fn>
  
00:00 +1 -1: test/binary_search_test.dart: finds a value at the beginning of an array
00:00 +1 ~1 -1: test/binary_search_test.dart: finds a value at the end of an array
00:00 +1 ~2 -1: test/binary_search_test.dart: finds a value in an array of odd length
00:00 +1 ~3 -1: test/binary_search_test.dart: finds a value in an array of even length
00:00 +1 ~4 -1: test/binary_search_test.dart: identifies that a value is not included in the array
00:00 +1 ~5 -1: test/binary_search_test.dart: a value smaller than the array's smallest value is not found
00:00 +1 ~6 -1: test/binary_search_test.dart: a value larger than the array's largest value is not found
00:00 +1 ~7 -1: test/binary_search_test.dart: nothing is found in an empty array
00:00 +1 ~8 -1: test/binary_search_test.dart: nothing is found when the left and right bounds cross
00:00 +1 ~9 -1: Some tests failed.

逐个完成测试

大多数练习都有多个测试,默认情况下除第一个之外全部跳过。

void main() {
  final twoFer = TwoFer();

  group('TwoFer', () {
    test('no name given', () {
      final result = twoFer.statement();
      expect(result, equals('One for you, one for me.'));
    }, skip: false);

    test('a name given', () {
      final result = twoFer.statement('Alice');
      expect(result, equals('One for Alice, one for me.'));
    }, skip: true);

    test('another name given', () {
      final result = twoFer.statement('Bob');
      expect(result, equals('One for Bob, one for me.'));
    }, skip: true);
  });
}

想一次只做一个测试,可以边做边把每个测试的skip: true改成skip: false。 或者,不修改文件,一次性运行所有测试。

dart test --run-skipped

提交时,在线测试运行器会运行所有测试,包括被跳过的那些。

处理错误 “Could not find a command named frontend_server.dart.snapshot”

假设你有一个已经解决并提交过的练习,过了一段时间又回来做。 测试时可能会遇到下面这个错误:

$ dart test
...
Could not find a command named "/path/to/frontend_server.dart.snapshot".

解决办法是删除 pubspec.lock 文件。 然后重新运行 dart 命令。