Тестування на треку Dart

Як тестувати свої вправи з Dart на Exercism


Завантаження вправи

Скористайтеся Exercism CLI, щоб завантажити вправу, над якою хочете працювати.

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

Потім перейдіть до каталогу вправи.

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

Встановлення залежностей

Перш ніж запускати тести, завантажте залежності вправи.

dart pub get

Запуск тестів

dart test

Розуміння результатів тестів

dart test за замовчуванням використовує компактний репортер, показуючи в одному рядку поточну кількість тестів, які пройдено, пропущено й не пройдено.

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

Опрацювання тестів

У більшості вправ тестів кілька, і всі, крім першого, за замовчуванням пропущені.

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 знову.