{"id":2686,"date":"2022-02-06T17:31:30","date_gmt":"2022-02-06T12:01:30","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2686"},"modified":"2022-02-06T17:31:30","modified_gmt":"2022-02-06T12:01:30","slug":"support-for-unit-testing-in-d-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/","title":{"rendered":"Support for Unit Testing in D Language"},"content":{"rendered":"<p>While I admire <em><strong>D<\/strong><\/em> language for many of its elegant and powerful features, two features stand out in my opinion. These are support for <em><strong>Design by Contract<\/strong><\/em> (DBC) and <em><strong>Unit Testing<\/strong><\/em>. I am a great fan of <em><strong>DBC<\/strong><\/em> and will cover that feature in a future article. Today I let me briefly touch upon <em><strong>D<\/strong><\/em>&#8216;s support for <em><strong>Unit Testing<\/strong><\/em>.<\/p>\n<p>Unit tests in <em><strong>D<\/strong><\/em> language are defined in blocks of code like this:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">unittest {<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #0000ff;\">\/* code to test *\/<\/span><\/p>\n<p><span style=\"color: #0000ff;\">}<\/span><\/p><\/blockquote>\n<p>Where do we define such unit tests? Well, we can define them at many convenient points:<\/p>\n<ul>\n<li>Within struct, union, class<\/li>\n<li>Outside of any code block<\/li>\n<li>In a separate file, independent of any other code<\/li>\n<\/ul>\n<p>The important point to note is that they are executed <em><strong>after<\/strong><\/em> all static initialization takes place, but before <em><strong>&#8220;main&#8221;<\/strong><\/em> gets control. If there are many unit test blocks in the same file, they are executed in <em><strong>lexical<\/strong><\/em> order.<\/p>\n<p>What can we have inside a <em><strong>unittest<\/strong><\/em> block? Any valid <em><strong>D<\/strong><\/em> code is acceptable. In a majority of cases, though, we use different forms of <em><strong>&#8220;assert&#8221;<\/strong><\/em>:<\/p>\n<ul>\n<li>assert(&lt;condition&gt;)<\/li>\n<li>assertThrown(&lt;expression&gt;)<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<li>assertNotThrown(&lt;expression&gt;)<\/li>\n<\/ul>\n<p>Both <em><strong>assertThrown<\/strong><\/em> and <em><strong>assertNotThrown<\/strong><\/em> can optionally be used with specific template parameters, depending on the need.<\/p>\n<p>It is possible to associate attributes such as <em><strong>@safe<\/strong><\/em>, <em><strong>@nogc<\/strong><\/em>, etc. It is even allowed to tag them as <em><strong>&#8220;private&#8221;<\/strong><\/em> (useful in the context of automatic documentation).<\/p>\n<p>For the complete specification of unit tests in <em><strong>D<\/strong> <\/em>language, go through this <a href=\"https:\/\/dlang.org\/spec\/unittest.html\" target=\"_blank\" rel=\"noopener\"><em><strong>documentation.<\/strong><\/em><\/a><\/p>\n<p>Next, how do we <em><strong>enable<\/strong><\/em> the unit tests? The commandline flag <em><strong>&#8220;-unittest&#8221;<\/strong><\/em> needs to be passed to the <em><strong>DMD<\/strong><\/em> compiler to compile and run unit tests.<\/p>\n<p>I use <em><strong>&#8220;VisualD&#8221;<\/strong><\/em>\u00a0(on top of <em><strong>Visual Studio 2017<\/strong><\/em>) for running <em><strong>D<\/strong><\/em> programs. Here is how to set the corresponding flag in the <em><strong>IDE<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2687\" aria-describedby=\"caption-attachment-2687\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2687\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/unittest-settting\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg\" data-orig-size=\"1058,650\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644051170&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Enabling &amp;#8220;unittest&amp;#8221;\" data-image-description=\"&lt;p&gt;Enabling &amp;#8220;unittest&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Enabling &amp;#8220;unittest&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting-1024x629.jpg\" class=\"wp-image-2687\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?resize=550%2C338&#038;ssl=1\" alt=\"Enabling &quot;unittest&quot;\" width=\"550\" height=\"338\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?resize=300%2C184&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?resize=1024%2C629&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?resize=768%2C472&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Unittest-settting.jpg?w=1058&amp;ssl=1 1058w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2687\" class=\"wp-caption-text\"><strong>Enabling &#8220;unittest&#8221;<\/strong><\/figcaption><\/figure>\n<p>Let us look at a simple example to understand this feature.<\/p>\n<figure id=\"attachment_2688\" aria-describedby=\"caption-attachment-2688\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2688\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/code-18\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg\" data-orig-size=\"623,1019\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644155428&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example D Program\" data-image-description=\"&lt;p&gt;Example D Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example D Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg\" class=\"wp-image-2688\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg?resize=500%2C818&#038;ssl=1\" alt=\"Example D Program\" width=\"500\" height=\"818\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg?resize=183%2C300&amp;ssl=1 183w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code.jpg?w=623&amp;ssl=1 623w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2688\" class=\"wp-caption-text\"><strong>Example D Program<\/strong><\/figcaption><\/figure>\n<p>In my <em><strong>VisualD<\/strong><\/em> environment, when I enable the <em><strong>&#8220;unittest&#8221;<\/strong><\/em> flag, the above program compiles and when I run it, this is the output I get:<\/p>\n<figure id=\"attachment_2689\" aria-describedby=\"caption-attachment-2689\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2689\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/output-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg\" data-orig-size=\"779,124\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644155933&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Unittest Output\" data-image-description=\"&lt;p&gt;Unittest Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Unittest Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg\" class=\"wp-image-2689\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg?resize=550%2C88&#038;ssl=1\" alt=\"Unittest Output\" width=\"550\" height=\"88\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg?resize=300%2C48&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg?resize=768%2C122&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output.jpg?w=779&amp;ssl=1 779w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2689\" class=\"wp-caption-text\"><strong>Unittest Output<\/strong><\/figcaption><\/figure>\n<p>What is interesting is that the actual <em><strong>&#8220;main&#8221;<\/strong><\/em> does not run, but only the unit tests run (even though all the tests pass). Let me now deliberately introduce a bug in the <em><strong>&#8220;factorial&#8221;<\/strong><\/em> function and then run the tests again.<\/p>\n<figure id=\"attachment_2690\" aria-describedby=\"caption-attachment-2690\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2690\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/buggy-code\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg\" data-orig-size=\"512,204\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644157702&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Buggy Function\" data-image-description=\"&lt;p&gt;Buggy Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Buggy Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg\" class=\"wp-image-2690\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg?resize=500%2C199&#038;ssl=1\" alt=\"Buggy Function\" width=\"500\" height=\"199\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg?resize=300%2C120&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg?resize=400%2C160&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Buggy-code.jpg?w=512&amp;ssl=1 512w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2690\" class=\"wp-caption-text\"><strong>Buggy Function<\/strong><\/figcaption><\/figure>\n<p>Here is the program output:<\/p>\n<figure id=\"attachment_2691\" aria-describedby=\"caption-attachment-2691\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2691\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/test-failure\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg\" data-orig-size=\"791,138\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644157601&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Failed Tests\" data-image-description=\"&lt;p&gt;Failed Tests&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Failed Tests&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg\" class=\"wp-image-2691\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg?resize=550%2C96&#038;ssl=1\" alt=\"Failed Tests\" width=\"550\" height=\"96\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg?resize=300%2C52&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg?resize=768%2C134&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Test-failure.jpg?w=791&amp;ssl=1 791w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2691\" class=\"wp-caption-text\"><strong>Failed Test<\/strong><\/figcaption><\/figure>\n<p>You can see that the test fails at <em><strong>line 20<\/strong><\/em>. Here is the failed case:<\/p>\n<figure id=\"attachment_2692\" aria-describedby=\"caption-attachment-2692\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2692\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/failed-test-case\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg\" data-orig-size=\"717,182\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644157732&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Failed Test Case\" data-image-description=\"&lt;p&gt;Failed Test Case&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Failed Test Case&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg\" class=\"wp-image-2692\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg?resize=550%2C140&#038;ssl=1\" alt=\"Failed Test Case\" width=\"550\" height=\"140\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg?resize=300%2C76&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Failed-test-case.jpg?w=717&amp;ssl=1 717w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2692\" class=\"wp-caption-text\"><strong>Failed Test Case<\/strong><\/figcaption><\/figure>\n<p>When the program is <em><strong>&#8220;correct&#8221;<\/strong> <\/em>(as per the unit tests), in order to get the actual <em><strong>&#8220;main&#8221;<\/strong><\/em> to run, I have to remove <em><strong>&#8220;unittest&#8221;<\/strong> <\/em>from the commandline argument. When I do that, here is the program output:<\/p>\n<figure id=\"attachment_2693\" aria-describedby=\"caption-attachment-2693\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2693\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/06\/support-for-unit-testing-in-d-language\/output-from-main\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg\" data-orig-size=\"798,162\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1644158113&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg\" class=\"wp-image-2693\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg?resize=550%2C112&#038;ssl=1\" alt=\"Program Output\" width=\"550\" height=\"112\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg?resize=300%2C61&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg?resize=768%2C156&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Output-from-main.jpg?w=798&amp;ssl=1 798w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2693\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>I hope this gives you an idea of the power of <em><strong>&#8220;unittest&#8221;<\/strong><\/em> blocks in <em><strong>D<\/strong><\/em> language. If you are into <em><strong>Test Driven Development<\/strong><\/em>, I am sure you will find this feature immensely useful. Do not forget to go through the official <a href=\"https:\/\/dlang.org\/spec\/unittest.html\" target=\"_blank\" rel=\"noopener\"><em><strong>documentation<\/strong><\/em><\/a>\u00a0to learn more about this feature.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I admire D language for many of its elegant and powerful features, two features stand out in my opinion. These are support for Design by Contract (DBC) and Unit Testing. I am a great fan of DBC and will cover that feature in a future article. Today I let me briefly touch upon D&#8216;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[302,17],"tags":[304,309],"class_list":["post-2686","post","type-post","status-publish","format-standard","hentry","category-d-language","category-programming","tag-d-language","tag-unit-testing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Hk","jetpack-related-posts":[{"id":2701,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/02\/23\/selective-unit-testing-in-d-language\/","url_meta":{"origin":2686,"position":0},"title":"Selective Unit Testing in D Language","author":"admin","date":"February 23, 2022","format":false,"excerpt":"In the last article, I briefly explained how D language has built-in support for performing unit tests. I feel this is an advantage over many current languages.\u00a0 What if we want to run only a subset of unit tests? Although it is a good idea to run the unit tests\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Code with Unit Tests","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/02\/Code1-163x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2668,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/","url_meta":{"origin":2686,"position":1},"title":"Template Mixins in D Programming Language","author":"admin","date":"January 23, 2022","format":false,"excerpt":"When I heard the name Mixin\u00a0for the first time in the context of Dlang, I imagined it would be something similar to the mixins\u00a0of Common Lisp, but I was completely wrong!\u00a0 Mixins are a very interesting feature of D language. They allow code to be \"injected\" into the source at\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Simple Template Mixin","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn-300x98.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2516,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/09\/04\/ring-language-dynamic-behavior-of-classes-and-objects\/","url_meta":{"origin":2686,"position":2},"title":"Ring Language: Dynamic Behavior of Classes and Objects","author":"admin","date":"September 4, 2021","format":false,"excerpt":"We have been exploring the Ring Programming Language\u00a0in the last two articles.\u00a0Ring is a dynamic programming language with many interesting features. In today's article, let us try to understand how this dynamic behavior is reflected in Classes and Objects. In Ring, we can define classes at runtime. One way to\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Dynamic Class Creation","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/09\/image1-300x146.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2935,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","url_meta":{"origin":2686,"position":3},"title":"Why Learn C++","author":"admin","date":"November 7, 2022","format":false,"excerpt":"This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3312,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/","url_meta":{"origin":2686,"position":4},"title":"The Hy Programming Language","author":"admin","date":"January 28, 2024","format":false,"excerpt":"In an earlier article\u00a0I had explained how to execute Python code from within Common Lisp using \u201cCLPython\u201d package. In contrast to that approach, \u201cHy\u201d\u00a0is a Lisp-style language (not compatible with Common Lisp) that is embedded in Python and hence provides seamless interoperability with Python code. Installation is straightforward (it is\u2026","rel":"","context":"In &quot;Hy Language&quot;","block_context":{"text":"Hy Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/hy-language\/"},"img":{"alt_text":"Hy REPL","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console-300x148.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":928,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/05\/27\/calling-go-functions-from-lisp\/","url_meta":{"origin":2686,"position":5},"title":"Calling Go Functions from Lisp","author":"admin","date":"May 27, 2018","format":false,"excerpt":"For the past few weeks I was travelling in the USA and had the pleasure of meeting several friends, some of whom are senior developers and architects. It was interesting to learn that many of them were either using Go language in their daily work, or were passionately endorsing the\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Part of Header File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2686"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2686\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}