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

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


हर अभ्यास में यूनिट टेस्ट दिए होते हैं। इम्प्लीमेंटेशन आप लिखते हैं। हर फाइल एक कंसोल एक्ज़ीक्यूटेबल बनाती है, जो टेस्ट चलाता है। टेस्ट एक्ज़ीक्यूटेबल चलाने पर हर फेल होने वाले टेस्ट के लिए संदेश छपते हैं, और टेस्ट फेल होने पर यह शून्य से भिन्न एक्ज़िट स्टेटस बताता है।

ध्यान दें जब भी आप अपना प्रोजेक्ट बनाते हैं, आपका कोड टेस्ट सूट के सामने परखा जाता है। अगर आपका कोड एक या उससे अधिक टेस्ट पास नहीं करता, लेकिन मान्य D कोड है, तो भी वह कंपाइल हो जाएगा।

हर अभ्यास को पूरा करने की प्रक्रिया यह है:

  • dub test से टेस्ट चलाना
    • अगर आपने DUB इंस्टॉल न करने का फैसला किया है, तो इसके बजाय dmd source/*.d -de -w -main -unittest इस्तेमाल कीजिए, और फिर उससे बनी बाइनरी चलाइए।
  • हर यूनिट टेस्ट के लिए:
    • कंपाइल एरर दूर कीजिए ताकि टेस्ट फेल हो।
    • बस इतना लिखिए कि टेस्ट पास हो जाए।
    • अपने इम्प्लीमेंटेशन को बेहतर बनाइए ताकि वह पढ़ने में आसान हो, दोहराव कम हो, वगैरह।
    • अगला टेस्ट चालू कीजिए

ध्यान दें: D में सीधे भाषा के भीतर ही यूनिट टेस्टिंग का समर्थन है, इसलिए आम तौर पर यूनिट टेस्ट उसी फाइल में रहते हैं जिसमें इम्प्लीमेंटेशन होता है। यूनिट टेस्ट main के बॉडी से पहले चलते हैं, और अंतिम एक्ज़ीक्यूटेबल में इन्हें -unittest कंपाइलर स्विच से चालू किया जाता है।

यूनिट टेस्ट का आउटपुट समझना

अगर सारे टेस्ट पास हो जाएँ, तो टेस्ट का आउटपुट कुछ ऐसा दिख सकता है

$ dub test
             Generating test runner configuration 'dnd-character-test-library' for 'library' (library).
    Starting Performing "unittest" build using dmd for x86_64.
    Building dnd-character ~master: building configuration [dnd-character-test-library]
     Linking dnd-character-test-library
     Running dnd-character-test-library
1 modules passed unittests

जब टेस्ट फेल हों, तो टेस्ट का आउटपुट पढ़ना मुश्किल हो सकता है।

$ dub test
             Generating test runner configuration 'dnd-character-test-library' for 'library' (library).
    Starting Performing "unittest" build using dmd for x86_64.
    Building dnd-character ~master: building configuration [dnd-character-test-library]
     Linking dnd-character-test-library
     Running dnd-character-test-library
core.exception.AssertError@source/dnd_character.d(102): unittest failure
----------------
??:? _d_unittestp [0x5568ff91bf05]
source/dnd_character.d:102 void dnd_character.__unittest_L30_C1() [0x5568ff911fee]
??:? void dnd_character.__modtest() [0x5568ff913760]
??:? int core.runtime.runModuleUnitTests().__foreachbody6(object.ModuleInfo*) [0x5568ff928f82]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x5568ff9148f7]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_elf_shared.DSO) [0x5568ff92003f]
??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref rt.sections_elf_shared.DSO)) [0x5568ff9200d1]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))) [0x5568ff91ffcd]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)) [0x5568ff9148c9]
??:? runModuleUnitTests [0x5568ff928db7]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll() [0x5568ff91d248]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x5568ff91d1d5]
??:? _d_run_main2 [0x5568ff91d13e]
??:? _d_run_main [0x5568ff91cf27]
/usr/include/dmd/druntime/import/core/internal/entrypoint.d:29 main [0x5568ff90f8f1]
??:? [0x7f473154a249]
??:? __libc_start_main [0x7f473154a304]
??:? _start [0x5568ff90f7e0]
1/1 modules FAILED unittests
Error Program exited with code 1

वहाँ बहुत विस्तृत स्टैकट्रेस है, लेकिन ज़रूरी लाइन वह है जो ---------------- के ठीक ऊपर है।

core.exception.AssertError@source/dnd_character.d(102): unittest failure

वह लाइन बताती है कि फेल होने वाला असेर्शन फाइल dnd_character.d की लाइन 102 पर है। इससे आप उस टेस्ट को ठीक-ठीक पहचान सकते हैं जो अपेक्षाओं पर खरा नहीं उतर रहा है।