{"id":2986,"date":"2021-12-17T06:25:02","date_gmt":"2021-12-17T06:25:02","guid":{"rendered":"https:\/\/www.paytmmoney.com\/blog\/?p=2986"},"modified":"2021-12-17T07:35:06","modified_gmt":"2021-12-17T07:35:06","slug":"hermetic-test-pattern","status":"publish","type":"post","link":"https:\/\/www.paytmmoney.com\/blog\/hermetic-test-pattern\/","title":{"rendered":"Hermetic Test Pattern &#038; You"},"content":{"rendered":"<p><span data-preserver-spaces=\"true\">I\u2019ve never heard the Hermetic test pattern defined better than by the phrase \u201cTest is an Island\u201c, a play on the adage of no man is an island.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The gist of the Hermetic test pattern is to write tests in such a way that each test is completely self-sufficient and independent of others. Any dependency on other tests or a third-party service should be ruthlessly avoided.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To do that with any test, a test needs to do the following :<\/span><\/p>\n<ul>\n<li><span data-preserver-spaces=\"true\">Handle its Setup<\/span><\/li>\n<li><span data-preserver-spaces=\"true\">Handle its Teardown<\/span><\/li>\n<li><span data-preserver-spaces=\"true\">Handle its Environments<\/span><\/li>\n<\/ul>\n<p><span data-preserver-spaces=\"true\">In broad terms, the advantages of doing so are the following :<\/span><\/p>\n<h3><strong><span data-preserver-spaces=\"true\">1. Speed Up the Tests:\u00a0<\/span><\/strong><\/h3>\n<p><span data-preserver-spaces=\"true\">By removing the dependencies, you are removing the need to wait for those dependencies to return with a response. Admittedly this is more of a concern with UI tests than any other, but this quickly can either become a major headache or a major relief depending on the way you chose to go, as even small differences in test times add up.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">A simple back-of-the-napkin calculation can help us to get this point across.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Let\u2019s say you have a test harness of 200 test workflows, that make network calls. Let\u2019s say on average each network call takes 400 milliseconds and there are on average 10 such calls per test.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">If you were to remove these network calls by mocking them, you might only be reducing each test by 4 seconds, but overall you have shaved off over 13 minutes from your testing. This can be a godsend if you have placed the tests in your CI Pipeline (as you should), speeding up your build process.<\/span><\/p>\n<h3><strong><span data-preserver-spaces=\"true\">2. Prevent Test Environment Pollution:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0<\/span><\/h3>\n<p><span data-preserver-spaces=\"true\">You don\u2019t want the state of your unit\/app for one test, interfering with another. Let\u2019s see this with an example :<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Let\u2019s say, Joe, our automation guy working for the famous taxi sharing app, Super, has decided to test two workflows, one being when the user has not paid for the previous ride and is trying to book another ride, the other being the normal ride-booking workflow that the user goes through. Being the slick guy he is, he kept the tests in the CI pipeline and created a script to log bugs in the company\u2019s Slack channel whenever any of those two tests fails.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The only problem is Joe, due to his love for spaghetti, has gone with the Spaghetti Anti-Pattern and created end-to-end tests that have common setups and teardowns and share an environment.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">A short while later everybody in the company is very angry with Joe due to the deluge of bug messages in Slack everyone has been receiving.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">You see, he placed the Unpaid workflow before the normal one, and since he didn\u2019t make the test hermetic, the second workflow will always fail and the state of the app will indicate that the user has not paid for the previous ride, and prevent any booking before the user has done so.<\/span><\/p>\n<p><em>Don\u2019t be like Joe, keep your tests hermetic.<\/em><\/p>\n<h3><strong><span data-preserver-spaces=\"true\">3. Reduce Test Flakiness:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0<\/span><\/h3>\n<p><span data-preserver-spaces=\"true\">This is the most important point. The bedrock of good testing is\u00a0<\/span><strong><span data-preserver-spaces=\"true\">determinism<\/span><\/strong><span data-preserver-spaces=\"true\">, such that everything works when it meets functional requirements and if it does not, it is easy to debug why.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">However, whenever you have tests that are dependent on third-party dependencies, you are introducing more things that might lead to the scenario of a test failing even though the Unit\/App under test meets all the functional requirements, because something went wrong with those dependencies.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">By making the test hermetic you can be sure that the test fails only when the thing being tested fails to meet the test criteria.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Hopefully, this post will motivate you to start the journey towards making your tests hermetic, and avoid the pitfalls of the spaghetti anti-pattern that most people end up struggling with.<\/span><\/p>\n<p><em>This blog is a Guest Contribution from <a href=\"https:\/\/www.linkedin.com\/in\/atin-agnihotri\/\">Atin Agnihotri<\/a> (frontend QA).<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve never heard the Hermetic test pattern defined better than by the phrase \u201cTest is an Island\u201c, a play on the adage of no man is an island. The gist of the Hermetic test pattern is to write tests in such a way that each test is completely self-sufficient and independent of others. Any dependency<a href=\"https:\/\/www.paytmmoney.com\/blog\/hermetic-test-pattern\/\">Continue reading <span class=\"sr-only\">&#8220;Hermetic Test Pattern &#038; You&#8221;<\/span><\/a><\/p>\n","protected":false},"author":27,"featured_media":2988,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-classic-ns.php","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts\/2986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/comments?post=2986"}],"version-history":[{"count":0,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts\/2986\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/media\/2988"}],"wp:attachment":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/media?parent=2986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/categories?post=2986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/tags?post=2986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}