{"id":134,"date":"2009-09-20T19:52:46","date_gmt":"2009-09-21T03:52:46","guid":{"rendered":"http:\/\/zuill.us\/WoodyZuill\/?p=134"},"modified":"2009-09-26T05:46:22","modified_gmt":"2009-09-26T13:46:22","slug":"approval-tests-make-characterization-tests-easy","status":"publish","type":"post","link":"https:\/\/zuill.us\/WoodyZuill\/2009\/09\/20\/approval-tests-make-characterization-tests-easy\/","title":{"rendered":"Approval Tests make Characterization Tests Easy"},"content":{"rendered":"<p>I&#8217;ve had the great fortune lately to have a little extra &#8220;uncommitted&#8221; time to spend exploring things that I&#8217;m interested in.\u00a0 On the top of my &#8220;to be explored&#8221; list is Approval Tests.\u00a0 I&#8217;ve been using it a bit over the last few months, but haven&#8217;t had time (until now) to dig deeper.<\/p>\n<p><a title=\"ApprovalTests\" href=\"http:\/\/www.approvaltests.sourceforge.net\/\" target=\"_self\">Approval Tests<\/a> is an open source project with the purpose of providing a very simple way to solve some difficult testing problems.\u00a0 For example, one thing that Approval Tests makes easy is to provide characterization tests for legacy code.<\/p>\n<p>I use\u00a0characterization tests to lock down the existing behavior of code I need to work on &#8211; in other words, it is a test\u00a0that proves\u00a0the code does whatever it is currently doing so that I can\u00a0refactor\u00a0and clean it with confidence that I won&#8217;t accidentally change it&#8217;s behavior.<\/p>\n<p>I first came upon the term in Michael Feathers amazing book (Working Effectively with Legacy Code), and have commonly used mock object frameworks to implement my characterization tests.\u00a0 However, that can be a complicated and time consuming process, and can sometimes require some refactoring to modify the code so it can be tested.\u00a0 Usually these are very safe refactorings &#8211; but can still introduce bugs.\u00a0 Another issue is that coverage is typically very localized to the method of interest and perhaps a few depended-on objects.<\/p>\n<p>Enter Approval Tests:\u00a0 You get a lot of bang for the buck.\u00a0 For example, in one of the presentations I do (Code Excellence for the Average Programmer) I show how a real life project that had become impossible to maintain or enhance\u00a0was\u00a0revived using very simple code-cleaning techniques.\u00a0 It only took four unit test methods of a few lines each using Approval Tests to introduce sufficient code coverage to be able to confidently work on the project.<\/p>\n<p>The essential concept of using Approval Testing this way is to allow the code you want to test\u00a0to produce a result that you then capture by &#8220;Approving&#8221; it (which saves it).\u00a0 This approved result will then be used\u00a0when you run the test to verify the results.\u00a0 Whenever anything changes, the test will fail and you&#8217;ll be shown the differences.\u00a0 Sounds simple, doesn&#8217;t it?\u00a0 And it is!!!\u00a0 Approval testing has a number of other uses, and perhaps I&#8217;ll have a chance to write about those sometime soon.\u00a0\u00a0 I bet you can&#8217;t wait.<\/p>\n<p>I encourage you to take a look the\u00a0next time you get a chance: <a title=\"ApprovalTests\" href=\"http:\/\/approvaltests.sourceforge.net\/\" target=\"_self\">Approval Tests<\/a>.\u00a0\u00a0 The <a title=\"ApprovalTests\" href=\"http:\/\/approvaltests.sourceforge.net\/\" target=\"_blank\">Approval Tests<\/a> project was invented by Llewellyn Falco and Dan Gilkerson, and they invite your suggestions and help on expanding and improving it.\u00a0 Also, they have a talk they&#8217;ve been doing at various conferences and code camps.\u00a0 If you get a chance to see it I am sure you&#8217;ll find it an eye-opener.\u00a0 The next scheduled presentation is at the <a title=\"Silicon Valley Code Camp\" href=\"http:\/\/www.siliconvalley-codecamp.com\/Default.aspx\" target=\"_blank\">Silicon Valley Code Camp<\/a>, and it&#8217;s called &#8220;Unit Testing the Easy Way&#8221;.\u00a0\u00a0 I&#8217;ll be helping out on that one since Dan can&#8217;t make it, and I&#8217;m really looking forward to that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had the great fortune lately to have a little extra &#8220;uncommitted&#8221; time to spend exploring things that I&#8217;m interested in.\u00a0 On the top of my &#8220;to be explored&#8221; list is Approval Tests.\u00a0 I&#8217;ve been using it a bit over the last few months, but haven&#8217;t had time (until now) to dig deeper. Approval Tests [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,7,10],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-code-excellence","category-extreme-programming","category-unit-testing"],"_links":{"self":[{"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":0,"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zuill.us\/WoodyZuill\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}