{"id":470,"date":"2010-03-05T11:41:20","date_gmt":"2010-03-05T18:41:20","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=470"},"modified":"2018-04-12T01:26:35","modified_gmt":"2018-04-12T05:26:35","slug":"the-ego-test","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/the-ego-test\/","title":{"rendered":"The Ego Test &#8211; Test Cases That Just Won&#8217;t Fail"},"content":{"rendered":"<p>You know those tests you like to run? \u00a0You know what I mean, those test cases that always give you a green bar and never fail?<\/p>\n<p>If you are having a bad day, and you feel like nothing is going right, you can just &#8220;right-click&#8221; run and you get a nice 100% passing green bar. \u00a0It is easy to refactor the code those tests are testing, because you know in your heart that no matter how much you change the code, those tests will always pass. \u00a0What a great feeling. \u00a0Doesn&#8217;t that make you feel like the king of the world?<\/p>\n<p>I like to call those tests <strong>Ego Tests<\/strong>, because they are the\u00a0equivalent\u00a0of code flattery. \u00a0An Ego Test always validates you. \u00a0It tells you your code is pretty when it really has a fat butt. \u00a0The Ego Test says &#8220;you can do nothing wrong.&#8221; \u00a0The Ego Test says that &#8220;everything is going to be ok.&#8221; \u00a0That is until you roll the code out to production and everything breaks. \u00a0Then when you look for the Ego Test, you realize his message of &#8220;GREEN IS GOOD&#8221; is still blaring at full volume in an endless repeat cycle, like a mantra, or one of those self-hypnosis, &#8220;you can quit smoking&#8221; tapes. \u00a0He is not really your friend; he is just a recording stuck in an endless loop. \u00a0He won&#8217;t help you fix your bugs, he won&#8217;t even tell you they exist.<\/p>\n<p><strong>He must be stopped!<\/strong><\/p>\n<p><strong><span style=\"font-weight: normal;\"><a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-474\" title=\"39-ego\" alt=\"\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg?w=300\" width=\"300\" height=\"220\" srcset=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg 500w, https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego-300x220.jpg 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/span><\/strong><\/p>\n<h2>Identifying the Ego Test<\/h2>\n<p>The Ego Test can be any kind of test from unit test to automated functional test and more.<\/p>\n<p>The Ego Test is identifiable by one or more of the following characteristics:<\/p>\n<ol>\n<li>Does not contain asserts.<\/li>\n<li>Contains asserts which only assert on conditions which are always true such as: \u00a0<em>assertTrue(isJohnSonmezCool?true:true);<\/em><\/li>\n<li>Asserts on things which are not related to what you are testing.<\/li>\n<li>Catches exceptions and eats them so the test will not error out.<\/li>\n<li>Tests things through a mock, but then does not assert the mocking expectations are satisfied.<\/li>\n<li>Never fails, no matter what you do to the code.<\/li>\n<\/ol>\n<p>You would think from this list it would be pretty easy to stop and catch an Ego Test. \u00a0It is not always so easy. \u00a0Many Ego Tests appear to be testing the right thing, or seem like they could fail even though they can&#8217;t.<\/p>\n<p>One quick way to figure out if you have an Ego Test is to change the input to the test and see if it fails. \u00a0If you think you are looking for the word &#8220;Blue&#8221; in a drop down, and you change the HTML being tested to not contain the word &#8220;Blue&#8221; in the drop down, and if the test still passes, you have a problem.<\/p>\n<h2>Avoiding the Ego Test<\/h2>\n<p>The Ego Test is fairly easy to avoid. \u00a0The best way is to make sure your test will fail when you change the input. \u00a0Most of the time you can do this by changing the source code temporarily to produce a result which should be a failure. \u00a0Sometimes though, you don&#8217;t have the ability to change the source code or doing so would be more work than it is worth. \u00a0In those cases, you can change the test instead, to test for a condition which should not be true. \u00a0Be very careful here though, many people are tripped up trying to validate tests the second way because they simply invert an assert statement.<\/p>\n<p>For example, if you have an assert that is<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/55ece804bdfe8c2c10f997f03ba96e05.js\"><\/script><\/p>\n<p>and you try to verify the test does not always pass by inverting the assert to produce<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/2d60e2b4a8a23f49bbe7dfc9f41f6cbf.js\"><\/script><\/p>\n<p>Well, that is just plain dumb. But I have seen it before. Of course it will now fail.<br \/>\nWhat <em>is<\/em> valid is to change the assert to be something like:<\/p>\n<p>https:\/\/gist.github.com\/simpleprogrammer-shared\/391994ae4665377874755ab8fd2f0a6b<\/p>\n<p>When you know that &#8220;Green&#8221; should not be in the drop down. If the test still passes in that case, there is something wrong with the dropDownContains method, or some other assumption you are making about the contents of the data you are testing. Perhaps you are testing the wrong drop down control? Perhaps something else is wrong.<\/p>\n<p>One more tip on avoiding the Ego Test: know what you are testing and test only one thing. \u00a0If you are writing some code to increase your code coverage and the best name you can come with for the test is the name of the method itself followed by the word &#8220;test&#8221;, you have no idea what you are actually testing. \u00a0100% code coverage is pretty easy to get in unit tests when you aren&#8217;t actually testing anything.<\/p>\n<p><strong>Mutation Testing<\/strong><\/p>\n<p>By the way, the official name of this kind of testing is called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mutation_testing\">Mutation Testing<\/a>. \u00a0I am not sure if it is completely\u00a0necessary\u00a0to do full mutation testing on your tests if you actually follow the advice I give to avoid the Ego Test in the first place, but there are actually some pretty cool mutation testing tools out there. \u00a0One I know of for Java is called <a href=\"http:\/\/jester.sourceforge.net\/\">Jester<\/a>. \u00a0It will randomly change your source code and run all your unit tests. \u00a0Jester then reports which tests still passed after changing your source. \u00a0Looks like there is one for C# called <a href=\"http:\/\/nester.sourceforge.net\/\">Nester<\/a>. \u00a0If you know of a good updated one in either language, be sure to let me know.<\/p>\n<p><strong><em>Disclaimer: <span style=\"font-weight: normal;\">This post doesn&#8217;t apply to Chuck Norris. \u00a0Chuck Norris&#8217;s tests look like ego tests in that they never fail, but they don&#8217;t fail because the code actually changes to conform to the perfect will of Chuck Norris as indicated in his tests, not because his tests are not testing the right thing.<\/span><\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You know those tests you like to run? \u00a0You know what I mean, those test cases that always give you a green bar and never fail? If you are having a bad day, and you feel like nothing is going right, you can just &#8220;right-click&#8221; run and you get a nice 100% passing green bar&#8230;.<\/p>\n","protected":false},"author":2,"featured_media":474,"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":[44070,70886,12],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-debugging","category-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The Ego Test<\/title>\n<meta name=\"description\" content=\"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.\" \/>\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\/products\/careerguide\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Ego Test\" \/>\n<meta property=\"og:description\" content=\"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/products\/careerguide\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-05T18:41:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-12T05:26:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"367\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-ego-test\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"The Ego Test &#8211; Test Cases That Just Won&#8217;t Fail\",\"datePublished\":\"2010-03-05T18:41:20+00:00\",\"dateModified\":\"2018-04-12T05:26:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-ego-test\\\/\"},\"wordCount\":972,\"commentCount\":2,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/39-ego.jpg\",\"articleSection\":[\"Automation\",\"Debugging\",\"Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/the-ego-test\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\",\"name\":\"The Ego Test\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/39-ego.jpg\",\"datePublished\":\"2010-03-05T18:41:20+00:00\",\"dateModified\":\"2018-04-12T05:26:35+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/39-ego.jpg\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/39-ego.jpg\",\"width\":500,\"height\":367,\"caption\":\"ego test case\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Ego Test &#8211; Test Cases That Just Won&#8217;t Fail\"}]},{\"@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 Ego Test","description":"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.","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\/products\/careerguide","og_locale":"en_US","og_type":"article","og_title":"The Ego Test","og_description":"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.","og_url":"https:\/\/simpleprogrammer.com\/products\/careerguide","og_site_name":"Simple Programmer","article_published_time":"2010-03-05T18:41:20+00:00","article_modified_time":"2018-04-12T05:26:35+00:00","og_image":[{"width":500,"height":367,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg","type":"image\/jpeg"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/the-ego-test\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"The Ego Test &#8211; Test Cases That Just Won&#8217;t Fail","datePublished":"2010-03-05T18:41:20+00:00","dateModified":"2018-04-12T05:26:35+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/the-ego-test\/"},"wordCount":972,"commentCount":2,"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg","articleSection":["Automation","Debugging","Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/products\/careerguide#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/the-ego-test\/","url":"https:\/\/simpleprogrammer.com\/products\/careerguide","name":"The Ego Test","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg","datePublished":"2010-03-05T18:41:20+00:00","dateModified":"2018-04-12T05:26:35+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of how to avoid test cases that never fail by using mutation testing and writing a test case to fail before it passes.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/products\/careerguide"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/39-ego.jpg","width":500,"height":367,"caption":"ego test case"},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"The Ego Test &#8211; Test Cases That Just Won&#8217;t Fail"}]},{"@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\/470","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=470"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/474"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}