Μάθε πώς να δοκιμάζεις τις ασκήσεις σου σε Dart στο Exercism
Χρησιμοποίησε το Exercism CLI για να κατεβάσεις την άσκηση με την οποία θέλεις να ασχοληθείς.
exercism download --track=dart --exercise=hello-world
Έπειτα, πήγαινε στον κατάλογο της άσκησης.
cd /path/to/exercism/dart/hello-world
Πριν τρέξεις τα tests, κατέβασε τις εξαρτήσεις της άσκησης.
dart pub get
dart test
Το dart test χρησιμοποιεί από προεπιλογή τον compact reporter, εμφανίζοντας σε μία γραμμή ένα συνεχώς ενημερωμένο πλήθος από tests που πέρασαν, παραλείφθηκαν και απέτυχαν.
$ 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~9: παραλείφθηκαν 9 tests-1: απέτυχε 1 testΓια πιο λεπτομερή έξοδο ανά test, χρησιμοποίησε τον expanded reporter, ο οποίος εμφανίζει κάθε αποτέλεσμα test καθώς εκτελείται.
$ 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.
Οι περισσότερες ασκήσεις έχουν πολλά tests, και όλα εκτός από το πρώτο παραλείπονται από προεπιλογή.
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);
});
}
Για να δουλέψεις τα tests ένα-ένα, άλλαξε το skip: true σε skip: false σε κάθε test καθώς προχωράς. Εναλλακτικά, τρέξε όλα τα tests ταυτόχρονα χωρίς να επεξεργαστείς το αρχείο.
dart test --run-skipped
Όλα τα tests εκτελούνται από τον διαδικτυακό test runner κατά την υποβολή, συμπεριλαμβανομένων και των παραλειπόμενων.
Ας υποθέσουμε ότι έχεις μια άσκηση που την έχεις ήδη λύσει και υποβάλει, και επιστρέφεις σε αυτήν μετά από καιρό. Μπορεί να συναντήσεις αυτό το σφάλμα όταν την τεστάρεις:
$ dart test
...
Could not find a command named "/path/to/frontend_server.dart.snapshot".
Η λύση είναι να διαγράψεις το αρχείο pubspec.lock. Έπειτα, τρέξε ξανά την εντολή dart.