{"id":1140,"date":"2010-10-15T12:57:24","date_gmt":"2010-10-15T19:57:24","guid":{"rendered":"https:\/\/simpleprogrammer.com\/2010\/10\/15\/the-purpose-of-unit-testing\/"},"modified":"2017-08-07T19:42:30","modified_gmt":"2017-08-07T23:42:30","slug":"the-purpose-of-unit-testing","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/","title":{"rendered":"The Purpose of Unit Testing"},"content":{"rendered":"<p><em>Before you start learning up any new skill or concept, I suggest you take a look at my course <a href=\"https:\/\/simpleprogrammer.com\/store\/products\/learn-anything-quickly\/\">&#8220;10 Steps to Learn Anything Quickly&#8221;.<\/a><\/em>\\n\\nI was reminded yesterday that there are still many people out there who still don\u2019t really understand the purpose of unit testing.\\n\\nA funny shift happened in the last 5 or so years.\\n\\nAbout 5 years ago, when I would suggest TDD or just doing some unit testing when creating code, I would get horrible responses back.\u00a0 Many developers and managers didn\u2019t understand why unit testing was important and thought it was just extra work.\\n\\nMore recently when I have heard people talking about unit testing, almost everyone agrees unit testing is a good idea, but not because they understand why, but because it is now expected in the programming world.\\n\\nProgress without understanding is just moving forward in a random direction.\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/trashtime.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0; padding-right: 0; display: inline; padding-top: 0; border: 0; margin: 0;\" title=\"trashtime\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/trashtime_thumb.jpg\" alt=\"purpose of unit testing\" width=\"477\" height=\"366\" border=\"0\" \/><\/a>\\n\\n<\/p>\n<h2>Getting back to the basics<\/h2>\n<p>\\n\\nUnit testing isn\u2019t testing at all.\\n\\nUnit testing, especially test driven development, is a design or implementation activity, not a testing activity.\\n\\nYou get two primary benefits from unit testing, with a majority of the value going to the first:\\n\\n<\/p>\n<ol>\\n    <\/p>\n<li>Guides your design to be loosely coupled and well fleshed out.\u00a0 If doing test driven development, it limits the code you write to only what is needed and helps you to evolve that code in small steps.<\/li>\n<p>\\n    <\/p>\n<li>Provides fast automated regression for refactors and small changes to the code.<\/li>\n<p>\\n<\/ol>\n<p>\\n\\nI\u2019m not saying that is all the value, but those are the two most important.\\n\\n(Unit testing also gives you living documentation about how small pieces of the system work.)\\n\\n<strong>Unit testing forces you to actually use the class you are creating and punishes you if the class is too big and contains more than one responsibility.<\/strong>\\n\\nBy that pain, you change your design to be more cohesive and loosely coupled.\\n\\nYou consider more scenarios your class could face and determine the behavior of those, which drives the design and completeness of your class.\\n\\nWhen you are done, you end up with some automated tests that do not ensure the system works correctly, but do ensure the functionality does not change.\\n\\nIn reality, the majority of the value is in the act of creating the unit tests when creating the code.\u00a0 This is one of the main reasons why it makes no sense to go back and write unit tests after the code has been written.\\n\\n<\/p>\n<h2>The flawed thinking<\/h2>\n<p>\\n\\nHere are some bad no-nos that indicate you don\u2019t understand unit testing:\\n\\n<\/p>\n<ul>\\n    <\/p>\n<li>You are writing the unit tests after the code is written and not during or before.<\/li>\n<p>\\n    <\/p>\n<li>You are having someone else write unit tests for your code.<\/li>\n<p>\\n    <\/p>\n<li>You are writing integration or system tests and calling them unit tests just because they directly call methods in the code.<\/li>\n<p>\\n    <\/p>\n<li>You are having QA write unit tests because they are tests after all.<\/li>\n<p>\\n<\/ul>\n<p>\\n\\nUnit tests are a lot of work to write.\u00a0 If you wanted to cover an entire system with unit tests with a decent amount of code coverage, you are talking about a huge amount of work.\\n\\nIf you are not getting the first primary value of unit testing, improving your design, you are wasting a crap load of time and money writing unit tests.\\n\\nHonestly, what do you think taking a bunch of code you already wrote or someone else did and having everyone start writing unit tests for it will do?\\n\\nDo you think it will improve the code magically just by adding unit tests without even changing the code?\\n\\nPerhaps you think the value of having regression is so high that it will justify this kind of a cost?\\n\\nI\u2019m not saying not to add unit tests to legacy code.\u00a0 What I am saying is that when you add unit tests to legacy code, you better be getting your value out of it, because it is hard work and costs many hours.\\n\\n<strong>When you touch legacy code, refactor that code and use the unit tests to guide that refactored design.<\/strong>\\n\\nDon\u2019t assume unit tests are magic.\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/unicornmagic.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0; padding-right: 0; display: inline; padding-top: 0; border: 0;\" title=\"unicornMAGIC\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/unicornmagic_thumb.jpg\" alt=\"unicornMAGIC\" width=\"455\" height=\"374\" border=\"0\" \/><\/a>\\n\\nUnit tests are like guidelines that help you cut straight.\u00a0 It is ridiculous to try and add guidelines to a word-working project after you have already cut the wood.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before you start learning up any new skill or concept, I suggest you take a look at my course &#8220;10 Steps to Learn Anything Quickly&#8221;.\\n\\nI was reminded yesterday that there are still many people out there who still don\u2019t really understand the purpose of unit testing.\\n\\nA funny shift happened in the last 5 or so&#8230;<\/p>\n","protected":false},"author":2,"featured_media":24858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2185,10217,12,3882],"tags":[],"class_list":["post-1140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices","category-legacy-code","category-testing","category-unit-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The Purpose of Unit Testing<\/title>\n<meta name=\"description\" content=\"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Purpose of Unit Testing\" \/>\n<meta property=\"og:description\" content=\"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-10-15T19:57:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-08-07T23:42:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"The Purpose of Unit Testing\",\"datePublished\":\"2010-10-15T19:57:24+00:00\",\"dateModified\":\"2017-08-07T23:42:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/\"},\"wordCount\":763,\"commentCount\":17,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/10\\\/The-Purpose-Of-Unit-Testing.png\",\"articleSection\":[\"Best Practices\",\"Legacy Code\",\"Testing\",\"Unit Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/\",\"name\":\"The Purpose of Unit Testing\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/10\\\/The-Purpose-Of-Unit-Testing.png\",\"datePublished\":\"2010-10-15T19:57:24+00:00\",\"dateModified\":\"2017-08-07T23:42:30+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/10\\\/The-Purpose-Of-Unit-Testing.png\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/10\\\/The-Purpose-Of-Unit-Testing.png\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-purpose-of-unit-testing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Purpose of Unit Testing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Purpose of Unit Testing","description":"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/","og_locale":"en_US","og_type":"article","og_title":"The Purpose of Unit Testing","og_description":"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.","og_url":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/","og_site_name":"Simple Programmer","article_published_time":"2010-10-15T19:57:24+00:00","article_modified_time":"2017-08-07T23:42:30+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png","type":"image\/png"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"The Purpose of Unit Testing","datePublished":"2010-10-15T19:57:24+00:00","dateModified":"2017-08-07T23:42:30+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/"},"wordCount":763,"commentCount":17,"image":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png","articleSection":["Best Practices","Legacy Code","Testing","Unit Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/","url":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/","name":"The Purpose of Unit Testing","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png","datePublished":"2010-10-15T19:57:24+00:00","dateModified":"2017-08-07T23:42:30+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of the purpose of unit testing and the importance of it being a development activity that results in loosely coupled, well-designed code.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/10\/The-Purpose-Of-Unit-Testing.png","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/the-purpose-of-unit-testing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"The Purpose of Unit Testing"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/1140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=1140"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/24858"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}