Dart ट्रैक पर टेस्टिंग

Exercism पर अपने Dart अभ्यासों को टेस्ट करना सीखिए


अभ्यास डाउनलोड कीजिए

जिस अभ्यास पर आप काम करना चाहते हैं, उसे डाउनलोड करने के लिए Exercism CLI का इस्तेमाल कीजिए।

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 कमांड फिर से चलाइए।