{"id":249,"date":"2024-05-28T19:05:00","date_gmt":"2024-05-28T19:05:00","guid":{"rendered":"https:\/\/www.mitango.app\/?p=249"},"modified":"2025-01-07T22:32:33","modified_gmt":"2025-01-07T22:32:33","slug":"how-to-write-an-integration-test","status":"publish","type":"post","link":"https:\/\/www.mitango.app\/fr\/2024\/05\/28\/how-to-write-an-integration-test\/","title":{"rendered":"How to write an integration test?"},"content":{"rendered":"<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#44e3\">The cost of a bug<\/a><\/li><li><a href=\"#d928\">Test you done the task not you worked<\/a><\/li><li><a href=\"#d880\">Follow the guidelines<\/a><\/li><li><a href=\"#8c49\">Have some flow<\/a><\/li><li><a href=\"#aa15\">Only AAA quality<\/a><\/li><li><a href=\"#17e2\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Any product needs to keep constantly high quality on the long run and integration tests are the tool you need to go towards that path.<\/p>\n\n\n\n<p id=\"c5ba\">Inside that article I\u2019m gonna focus on the higher process rather than the tools themselves. The idea here is to focus on the questions you might ask yourself during the process of creating tests so then you can advance by your own.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"44e3\">The cost of a bug<\/h2>\n\n\n\n<p id=\"70ec\">First of all I would like to remind you what is a <a href=\"https:\/\/www.mitango.app\/fr\/definition\/bug\/\" data-type=\"definition\" data-id=\"316\">bug<\/a> and why it is important to test to avoid them.<\/p>\n\n\n\n<p id=\"7341\">We use to say that the devil is in the details and in software engineering this is more than true as most of the bugs reside there.<\/p>\n\n\n\n<p id=\"2231\">However, did you ever wondered what was a bug?<\/p>\n\n\n\n<p id=\"4f52\">You might be tempted to answer that it is an incorrect state within the application but you would be surprised as this definition is wrong and it is instead the definition from an incoherent behavior of the application.<\/p>\n\n\n\n<p id=\"b94a\">A bug is indeed an incoherent behavior but not all incoherent behaviors are bugs.<\/p>\n\n\n\n<p id=\"05c6\">What makes the difference is that the bug needs to be visible from outside and it is really important to understand that to prevent writing useless tests that would be asserting than an incorrect behavior not being a bug is not existing.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"522\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/bug-vs-incorrect-behavior.png\" alt=\"\" class=\"wp-image-251\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/bug-vs-incorrect-behavior.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/bug-vs-incorrect-behavior-300x224.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/bug-vs-incorrect-behavior-16x12.png 16w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p id=\"f11b\">Then once we are on the same basis about what is a bug the second question you might ask yourself is why letting them flow and let the users report them.<\/p>\n\n\n\n<p id=\"2619\">For a simple reason, have you ever had to comeback on a task you finished by the past? Isn\u2019t it annoy? You have to get back into it, try to remember things, in other terms you will be slower and that slowness has a cost.<\/p>\n\n\n\n<p id=\"f432\">This is a perfect example why the later a bug is discovered the highest is the cost to fix it.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"507\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/cost-of-a-bug.png\" alt=\"\" class=\"wp-image-252\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/cost-of-a-bug.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/cost-of-a-bug-300x217.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/cost-of-a-bug-18x12.png 18w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p id=\"597d\">This cost is why we want to catch bugs early while they are still cheap to fix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d928\">Test you done the task not you worked<\/h2>\n\n\n\n<p id=\"10c9\">One of the sentences I hate the most about testing is \u201cHave you tested your code?\u201d.<\/p>\n\n\n\n<p id=\"4bb6\">Most developers won\u2019t like this sentence cause they consider tests as a burden they are forced to handle but I don\u2019t like that sentence for another reason: this sentence is totally wrong&nbsp;!<\/p>\n\n\n\n<p id=\"416c\">Hell is paved with good intentions and this is one of them.<\/p>\n\n\n\n<p id=\"f6f5\">When you are saying this to another developer you mean good toward them by telling them to implement tests but what you don\u2019t see is that you are also leading them into a mistake.<\/p>\n\n\n\n<p id=\"66c5\">When testing you should never test your code but your expectations.<\/p>\n\n\n\n<p id=\"13ab\">This is due to how tests are actually working they aren\u2019t testing your program is exempt from <a href=\"https:\/\/www.mitango.app\/fr\/definition\/bug\/\" data-type=\"definition\" data-id=\"316\">bugs<\/a> but it is in fact checking if there is a gap between what the test expects and your code.<\/p>\n\n\n\n<p id=\"a4b5\">If you base the test on the code then you should find no bugs as they are both matching and that even if your code didn\u2019t meet the project expectations&nbsp;!<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"434\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/test-internal-working.png\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/test-internal-working.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/test-internal-working-300x186.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/test-internal-working-18x12.png 18w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p id=\"0eeb\">So what should we test instead?<\/p>\n\n\n\n<p id=\"d975\">We should test your project expectations and base your tests on them and never consider the implementation when testing.<\/p>\n\n\n\n<p id=\"9c63\">Now you might be confused on how to test the project expectations and what they are and this is why I would like to introduce you to a new friend, <a href=\"https:\/\/www.mitango.app\/fr\/definition\/acceptance-criteria\/\" data-type=\"definition\" data-id=\"294\">acceptance criteria<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d880\">Follow the guidelines<\/h2>\n\n\n\n<p id=\"4041\">Nothing is worse than being in front of a blank page without any idea of what comes next.<\/p>\n\n\n\n<p id=\"c229\">And be sure about one thing when you begin to learn testing you will face that feeling often.<\/p>\n\n\n\n<p id=\"90ff\">However, I got good news for you there is a way to reduce that feeling by writing specifications in a special way which is called acceptance criteria.<\/p>\n\n\n\n<p id=\"e24d\">An acceptance criteria is a simple sentence describing an atomic behavior from the application so that way it is easy to test as now we have precise instruction for each of your scenarios as there will be an acceptance criteria describing it precisely.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"469\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Tests-worflow.png\" alt=\"\" class=\"wp-image-255\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Tests-worflow.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Tests-worflow-300x201.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Tests-worflow-18x12.png 18w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8c49\">Have some flow<\/h2>\n\n\n\n<p id=\"be0e\">Unlike what beginners think there is an order for writing tests and code.<\/p>\n\n\n\n<p id=\"0455\">Not that it won\u2019t work if it is not done properly but it will be harder and more error prone.<\/p>\n\n\n\n<p id=\"a58a\">For that a good flow which is also really trendy is Test Driven Development.<\/p>\n\n\n\n<p id=\"bba9\">However, that practice is also dogmatic and as a beginner it is easy to get lost between rules to apply.<\/p>\n\n\n\n<p id=\"7dac\">This is why it is important for beginners to comeback to the essentials to make sure to understand the process.<\/p>\n\n\n\n<p id=\"b100\">It is the most important and there is no shame with not starting with a perfect process, Rome didn\u2019t build in one day and so will your testing skills.<\/p>\n\n\n\n<p id=\"0546\">Instead it is better to focus on the 3 rules from tests driven development and apply them the way that suits you the best.<\/p>\n\n\n\n<p id=\"35f3\">The first rule is to implement the test before writing any code.<\/p>\n\n\n\n<p id=\"26f7\">This is make sure you are not testing your code and that you are basing them on your acceptance criteria.<\/p>\n\n\n\n<p id=\"fccd\">The second rule is to make sure that the test fails before you wrote any code.<\/p>\n\n\n\n<p id=\"7e82\">In a small project that might won\u2019t make much sense but when the project state to scale then it is hard to track if an issue can be tackle by another. This check is here to make sure the job wasn\u2019t done previously and that implementing that code will be effective work for the company.<\/p>\n\n\n\n<p id=\"eee0\">The last rule is that all tests should pass once you finished writing code.<\/p>\n\n\n\n<p id=\"34ad\">Making sure every tests are passing allow keeping track of that matters for us inside the application and make sure that we won\u2019t break anything while refactoring or adding new features in the future.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"520\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Testing-workflow-correct-vs-incorrect.png\" alt=\"\" class=\"wp-image-256\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Testing-workflow-correct-vs-incorrect.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Testing-workflow-correct-vs-incorrect-300x223.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Testing-workflow-correct-vs-incorrect-16x12.png 16w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aa15\">Only AAA quality<\/h2>\n\n\n\n<p id=\"d7b8\">To implement a scenario we got <a href=\"https:\/\/www.mitango.app\/fr\/definition\/acceptance-criteria\/\" data-type=\"definition\" data-id=\"294\">acceptance criteria<\/a> describing each one of them but how to pass from a sentence to the actual code?<\/p>\n\n\n\n<p id=\"3a21\">The first step is to make sure that the acceptance criteria is complete and for that we will have to understand what are the 3 component of a test.<\/p>\n\n\n\n<p id=\"f99d\">First we will arrange the state from the application to make sure at each run we have a certain state precised inside the acceptance criteria. The idea here is to have the exact same state each time we run the test to be make sure we can reproduce it.<\/p>\n\n\n\n<p id=\"3092\">The next step is to act the logic we want to test.<\/p>\n\n\n\n<p id=\"3239\">Finally the last step is to assert that the new state is the one we expected otherwise we make the test fail.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"700\" height=\"394\" src=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Write-a-test-steps.png\" alt=\"\" class=\"wp-image-257\" srcset=\"https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Write-a-test-steps.png 700w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Write-a-test-steps-300x169.png 300w, https:\/\/www.mitango.app\/app\/uploads\/2024\/11\/Write-a-test-steps-18x10.png 18w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p id=\"aea7\">As you maybe guessed it if you know the <a href=\"https:\/\/www.mitango.app\/fr\/definition\/aaa-framework\/\" data-type=\"definition\" data-id=\"296\">framework AAA<\/a> we each step is following a step from the framework and to have an acceptance criteria well defined it needs to follow this 3 steps too.<\/p>\n\n\n\n<p id=\"d596\">It is not always simple to detect rapidly a pseudo-code language called Gherkin has been designed.<\/p>\n\n\n\n<p id=\"13ae\">In that language each acceptance criteria should include at least 1 time each of theses keywords:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Given:&nbsp;<\/strong>This keyword is linked to the arrange step and is here to define the initial state.<\/li>\n\n\n\n<li><strong>When:&nbsp;<\/strong>This keyword is here to define which logic needs to run.<\/li>\n\n\n\n<li><strong>Then:&nbsp;<\/strong>This keyword is linked to the assert step and make sure the test is asserting the final state is the one expected.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17e2\">Conclusion<\/h2>\n\n\n\n<p id=\"19e9\">Testing is complex however it is possible to achieve effective testing by following some principles and dividing steps well.<\/p>\n\n\n\n<p id=\"2122\">First make sure you are not testing your code and that you are testing potential <a href=\"https:\/\/www.mitango.app\/fr\/definition\/bug\/\" data-type=\"definition\" data-id=\"316\">bugs<\/a> by creating and using some <a href=\"https:\/\/www.mitango.app\/fr\/definition\/acceptance-criteria\/\" data-type=\"definition\" data-id=\"294\">acceptance criteria<\/a>.<\/p>\n\n\n\n<p id=\"cf1c\">Then respect the 3 rules from test driven development so you have actual make sure you are writing effective code for your company and that you can keep track of what is important inside the application.<\/p>\n\n\n\n<p>Finally to write each scenario you will have to follow the <a href=\"https:\/\/www.mitango.app\/fr\/definition\/aaa-framework\/\" data-type=\"definition\" data-id=\"296\">AAA framework<\/a> making sure you are not forgetting any of the three steps Arrange, Act, Assert.<a href=\"https:\/\/medium.com\/@crochetfeve0251?source=post_page---byline--55e53ee7bda8--------------------------------\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Any product needs to keep constantly high quality on the long run and integration tests are the tool you need to go towards that path.<\/p>\n<p>Inside that article I\u2019m gonna focus on the higher process rather than the tools themselves. The idea here is to focus on the questions you might ask yourself during the process of creating tests so then you can advance by your own.<\/p>","protected":false},"author":1,"featured_media":250,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[26],"tags":[12,62,63,61,22],"class_list":["post-249","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-testing","tag-architecture","tag-engineering","tag-quality-assurance","tag-software-development","tag-testing"],"jetpack_publicize_connections":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":5,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/posts\/249\/revisions\/412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/media\/250"}],"wp:attachment":[{"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mitango.app\/fr\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}