{"id":153,"date":"2024-05-15T18:07:16","date_gmt":"2024-05-15T21:07:16","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=153"},"modified":"2024-05-15T18:07:16","modified_gmt":"2024-05-15T21:07:16","slug":"specification-based-testing-understand-the-requirements","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=153","title":{"rendered":"Specification-based Testing: Understand the requirements"},"content":{"rendered":"\n<p>Understanding the requirements is an&nbsp;important&nbsp;part of testing your code.&nbsp;If you&nbsp;already&nbsp;know the business&nbsp;rules&nbsp;you can create&nbsp;tests to validate it.<\/p>\n\n\n\n<p>Also, you can create some that will prevent unwanted behavior too.<\/p>\n\n\n\n<p>When writing tests, consider the inputs, how they affect the code, and if the results meet your business rules.&nbsp;Do&nbsp;you have&nbsp;some&nbsp;doubts&nbsp;about it?&nbsp;What should be the result in some specific situation?&nbsp;Or does the code have some requirements to run? Or something like that?<\/p>\n\n\n\n<p>If some questions don&#8217;t have clear answers, it might be a good idea to ask stakeholders before testing and releasing the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h2>\n\n\n\n<p>I was creating a CHIP-8 emulator.&nbsp;To implement an emulator you have to follow the system specifications.<\/p>\n\n\n\n<p>To exemplify let\u2019s take a look at the ADD with carry instruction present in the Chip-8 CPU. Here\u2019s&nbsp;the code that I wrote:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/ 8XY4\n\/\/ ADD\nint registerxIndex = ((instruction &amp; 0x0F00) &gt;&gt; 8);\nint registeryIndex = ((instruction &amp; 0x00F0) &gt;&gt; 4);\n\nint firstToSum = registers&#x5B;registerxIndex];\nint secondRegisterToSum = registers&#x5B;registeryIndex];\n\nint registersSum = (firstToSum + secondRegisterToSum);\nregisters&#x5B;registerxIndex] = registersSum &amp; 0xFF;\nif (registersSum &gt; 255) registers&#x5B;registers.length - 1] = 1;\nelse registers&#x5B;registers.length - 1] = 0;\npc += 2;\n\n<\/pre><\/div>\n\n\n<p>This code is a result of following the specifications described below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The instruction will be a combination of 2 bytes fetched from RAM.<\/li>\n\n\n\n<li>The CPU has 16\u00a0registers,\u00a0that can be indexed from 0 to 15.<\/li>\n\n\n\n<li>From the 16 bits,\u00a08\u00a0will\u00a0be used\u00a0to identify the instruction (in this\u00a0case\u00a0the 8XY4 ADD instruction), the other ones will be 4 to\u00a0identify\u00a0the index of the register X in an array of registers\u00a0and\u00a0the other 4 to\u00a0identify\u00a0the index of register Y.<\/li>\n\n\n\n<li>After identifying the registers:\n<ul class=\"wp-block-list\">\n<li>Get the 8-bit value present in the register X (being X a number between 0 and 14, from the 4-bit value that you extracted from the instruction, the 15th is to store the carry flag)<\/li>\n\n\n\n<li>Get the 8-bit value present in the register Y (being X a number between 0 and 14)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Then add the values, if\u00a0the result is greater than 8 bits, then we should set the\u00a0carry flag to 1\u00a0otherwise\u00a0we set it to 0 (register 15 or F in hexadecimal).<\/li>\n\n\n\n<li>We pick only the resultant 8 bits from the result and store them in the X register.<\/li>\n\n\n\n<li>Update the program counter by 2.<\/li>\n<\/ul>\n\n\n\n<p>By looking at this specification, we can start thinking&nbsp;on what are&nbsp;the test cases that will guarantee the proper behavior of the code.<\/p>\n\n\n\n<p>Let\u2019s&nbsp;think of some scenarios for these specs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Scenario 1 &#8211; Sum without carry:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Store a value in a register X (120\u00a0for\u00a0example).<\/li>\n\n\n\n<li>Store\u00a0a value in a register Y (10\u00a0for\u00a0example).<\/li>\n\n\n\n<li>The two values sum must not be higher than 255.<\/li>\n\n\n\n<li>Set the program counter to 0.<\/li>\n\n\n\n<li>Check if the sum result value in the register X is correct (in this\u00a0scenario\u00a0the sum will be 130).<\/li>\n\n\n\n<li>Check if the carry flag is 0.<\/li>\n\n\n\n<li>Check if\u00a0the register Y was not changed.<\/li>\n\n\n\n<li>Check if the program counter was incremented by two.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Scenario 2 &#8211; Sum with carry:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Store a value in a register X (235, for example).<\/li>\n\n\n\n<li>Store a value in a register Y (30, for example).<\/li>\n\n\n\n<li>Set the program counter to 0.<\/li>\n\n\n\n<li>The two values sum must be higher than 255.<\/li>\n\n\n\n<li>Check if the sum result value in the register X is correct (9 in this example).since\u00a0we had an overflow of the 255 max value, we store only the 8-bit part, since\u00a0265 is a 9-bit number (100001001), we store only the 8-bit part in X\u00a0which\u00a0will be 00001001 = 9.<\/li>\n\n\n\n<li>The 9th bit that overflows will\u00a0be set\u00a0in the carry flag.<\/li>\n\n\n\n<li>Check if the carry flag is 1.<\/li>\n\n\n\n<li>Check if\u00a0the register Y was not changed.<\/li>\n\n\n\n<li>Check if the program counter was incremented by two.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Scenario 3 &#8211; The other registers cant affect X and Y:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Store 0 in X and Y.<\/li>\n\n\n\n<li>Store values in registers that are not X and Y.<\/li>\n\n\n\n<li>Set the program counter to 0.<\/li>\n\n\n\n<li>Check if the X remains 0.<\/li>\n\n\n\n<li>Check if Y remains 0.Check if the carry is 0.<\/li>\n\n\n\n<li>Check if the program counter was updated by two.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Just by&nbsp;using the specification&nbsp;we&nbsp;already can&nbsp;create some test scenarios to guarantee the proper behavior of our code.<\/p>\n\n\n\n<p>Write down your&nbsp;specification,&nbsp;and the scenarios you can&nbsp;come up with&nbsp;using the specs as your guide. It will clear your head&nbsp;and&nbsp;your tests will&nbsp;be way more focused&nbsp;on the business rules.<\/p>\n\n\n\n<p>Also, if you have some doubts about the specs, you can always question and figure out what is missing before finishing development.<\/p>\n\n\n\n<p>In this blog post,&nbsp;I&nbsp;won\u2019t&nbsp;be writing the code for these test cases.&nbsp;Since this blog post is part of a series,&nbsp;I\u2019ll&nbsp;be writing all test scenarios when we&nbsp;are going to&nbsp;discuss the devise test case topics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Want to learn more about this topic?<\/strong><\/h3>\n\n\n\n<p>Read the&nbsp;<a href=\"https:\/\/springmasteryhub.com\/2024\/05\/14\/specification-based-testing-a-developers-secret-weapont\/\">first<\/a>&nbsp;article of this series.<\/p>\n\n\n\n<p>In the following days,&nbsp;I\u2019ll&nbsp;dive into each of those steps in more detail, follow&nbsp;me, and do not miss my next blog posts of this series when it comes out.<\/p>\n\n\n\n<p>In my next blog post, we&nbsp;are going to&nbsp;discuss how you can explore the&nbsp;code,&nbsp;so you get a better understanding of the code, especially the code you did not write.<\/p>\n\n\n\n<p>Stay tuned to learn more! Don&#8217;t miss out!<\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/WillianFMoya\">Willian Moya (@WillianFMoya) \/ X (twitter.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.linkedin.com\/in\/willianmoya\/\">Willian Ferreira Moya | LinkedIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding the requirements is an&nbsp;important&nbsp;part of testing your code.&nbsp;If you&nbsp;already&nbsp;know the business&nbsp;rules&nbsp;you can create&nbsp;tests to validate it. Also, you can create some that will prevent unwanted behavior too. When writing tests, consider the inputs, how they affect the code, and if the results meet your business rules.&nbsp;Do&nbsp;you have&nbsp;some&nbsp;doubts&nbsp;about it?&nbsp;What should be the result in some [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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":""},"categories":[2,3],"tags":[11,18,25,27],"class_list":["post-153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-pratices","category-java","tag-coding","tag-programming","tag-tdd","tag-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":0,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}