Teste sur le parcours Dart

Apprends à tester tes exercices Dart sur Exercism


Télécharge un exercice

Utilise l'Exercism CLI pour télécharger l'exercice sur lequel tu veux travailler.

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

Ensuite, place-toi dans le répertoire de l'exercice.

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

Installe les dépendances

Avant de lancer les tests, télécharge les dépendances de l'exercice.

dart pub get

Lance les tests

dart test

Comprends les résultats des tests

Par défaut, dart test utilise le reporter compact, qui affiche sur une seule ligne le décompte des tests réussis, ignorés et échoués au fur et à mesure.

$ 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 réussi
  • ~9 : 9 tests ignorés
  • -1 : 1 test échoué

Pour une sortie plus détaillée, test par test, utilise le reporter étendu, qui affiche le résultat de chaque test au moment où il s'exécute.

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

Passe les tests un par un

La plupart des exercices comportent plusieurs tests, et tous sauf le premier sont ignorés par défaut.

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

Pour traiter les tests un par un, remplace skip: true par skip: false sur chaque test au fur et à mesure. Tu peux aussi lancer tous les tests d'un coup sans modifier le fichier.

dart test --run-skipped

Lors de la soumission, l'exécuteur de tests en ligne exécute tous les tests, y compris ceux qui sont ignorés.

Gère l'erreur « Could not find a command named frontend_server.dart.snapshot »

Imagine que tu as déjà résolu et soumis un exercice, et que tu y reviens après un moment. Tu peux tomber sur cette erreur au moment de le tester :

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

La solution consiste à supprimer le fichier pubspec.lock. Relance ensuite la commande dart.