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 명령을 다시 실행해요.