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はデフォルトでコンパクトレポーターを使い、成功・スキップ・失敗したテストの数を1行に表示します。

$ 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つ失敗しました

テストごとのより詳しい出力が必要なときは、拡張レポーターを使います。実行される各テストの結果が表示されます。

$ 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.

テストを順番に進める

ほとんどの演習には複数のテストがあり、最初の1つ以外はデフォルトでスキップされています。

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);
  });
}

テストを1つずつ進めるには、それぞれのテストの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コマンドを実行します。