آزمایش کردن در مسیر Dart

یاد بگیرید چطور تمرین‌های Dart خود را در Exercism آزمایش کنید


دانلود یک تمرین

برای دانلود تمرینی که می‌خواهید روی آن کار کنید، از Exercism CLI استفاده کنید.

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

سپس به پوشه‌ی تمرین بروید.

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

نصب وابستگی‌ها

پیش از اجرای testها، وابستگی‌های تمرین را دانلود کنید.

dart pub get

اجرای testها

dart test

درک نتایج test

dart test به‌صورت پیش‌فرض از گزارشگر فشرده استفاده می‌کند و شمارش پیوسته‌ی 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: ۱ test پاس شد
  • ~9: ۹ test رد شد
  • -1: ۱ test ناموفق بود

برای خروجی جزئی‌تر به‌ازای هر test، از گزارشگر گسترده استفاده کنید که نتیجه‌ی هر 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.

پیش بردن testها

بیشتر تمرین‌ها چندین test دارند و به‌صورت پیش‌فرض همه به‌جز اولی رد شده‌اند.

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

برای پیش بردن testها یکی‌یکی، هر بار skip: true را در هر test به skip: false تغییر دهید. در عوض، می‌توانید همه‌ی testها را یک‌جا و بدون تغییر فایل اجرا کنید.

dart test --run-skipped

هنگام ارسال، همه‌ی testها، از جمله testهای ردشده، توسط اجراکننده‌ی test آنلاین اجرا می‌شوند.

برخورد با خطای «Could not find a command named frontend_server.dart.snapshot»

فرض کنید تمرینی را قبلاً حل کرده و ارسال کرده‌اید و بعد از مدتی دوباره سراغ آن آمده‌اید. ممکن است هنگام اجرای test آن با این خطا روبه‌رو شوید:

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

راه‌حل این است که فایل pubspec.lock را حذف کنید. سپس دستور dart را دوباره اجرا کنید.