الاختبار في مسار Dart

تعرّف على كيفية اختبار تمارين Dart في Exercism


نزّل تمرينًا

استخدم واجهة سطر الأوامر الخاصة بـ Exercism لتنزيل التمرين الذي تريد العمل عليه.

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 مرة أخرى.