Testare sul track Dart

Scopri come testare gli esercizi di Dart su Exercism


Scaricare un esercizio

Usa la CLI di Exercism per scaricare l'esercizio su cui vuoi lavorare.

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

Poi spostati nella directory dell'esercizio.

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

Installare le dipendenze

Prima di eseguire i test, scarica le dipendenze dell'esercizio.

dart pub get

Eseguire i test

dart test

Capire i risultati dei test

Per impostazione predefinita, dart test usa il reporter compatto, che mostra su una sola riga un conteggio progressivo dei test superati, saltati e falliti.

$ 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 test superato
  • ~9: 9 test saltati
  • -1: 1 test fallito

Per un output più dettagliato, test per test, usa il reporter esteso, che mostra il risultato di ogni test mentre viene eseguito.

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

Procedere con i test

La maggior parte degli esercizi ha diversi test, e per impostazione predefinita tutti tranne il primo sono saltati.

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

Per affrontare i test uno alla volta, cambia skip: true in skip: false su ciascun test man mano che procedi. In alternativa, esegui tutti i test in una volta sola senza modificare il file.

dart test --run-skipped

Al momento dell'invio, il test runner online esegue tutti i test, compresi quelli saltati.

Gestire l'errore «Could not find a command named frontend_server.dart.snapshot»

Supponi di avere un esercizio che hai già risolto e inviato, e di tornarci dopo un po' di tempo. Potresti incontrare questo errore quando lo testi:

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

La soluzione è eliminare il file pubspec.lock. Poi esegui di nuovo il comando dart.