{"id":1394,"date":"2014-03-11T02:59:45","date_gmt":"2014-03-11T02:59:45","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1394"},"modified":"2014-03-11T03:04:46","modified_gmt":"2014-03-11T03:04:46","slug":"designing-software-thats-easy-to-test","status":"publish","type":"post","link":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/designing-software-thats-easy-to-test\/","title":{"rendered":"Designing Software that&#8217;s Easy To Test"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdesigning-software-thats-easy-to-test%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p>As programmers, we desire to create well crafted software that delights our customers. \u00a0 Because we care about the long term maintainability of our code, we aspire to craft our code well. \u00a0 \u00a0In the ideal case, future programmers should find it easy to change our code while avoiding errors and regressions. \u00a0How do we accomplish this vision?<\/p>\n<p>I wanted to share one of my favorite agile software engineering techniques: test first design. \u00a0(TFD) \u00a0In a classical model of software development, special attention for testing comes at the end of the project. \u00a0 \u00a0In agile culture, we aspire to test our software early and often throughout the project. \u00a0 &#8220;Test first design&#8221; encourages programmers to write test cases for software units (&#8220;classes&#8221;) continuously through the life of the project. \u00a0 \u00a0For large software applications and games, it becomes more important to keep our software loosely coupled and easy to test. \u00a0 Think about the components of your software as Lego blocks. \u00a0 In the ideal case, the various components of your application should be designed to connect together. \u00a0 At the same time, we should have the ability to test the behavior of each class or &#8220;lego block&#8221; in isolation. \u00a0Does each block function well? \u00a0 If we snap all the blocks together, does the system behave as expected?<\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/lego.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1395\" title=\"lego\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/lego.jpg\" alt=\"Software as lego blocks\" width=\"600\" height=\"370\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/lego.jpg 600w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/lego-300x185.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>In the following <a href=\"https:\/\/qunitjs.com\/\" target=\"_blank\">JavaScript code sample<\/a>, I have presented a <a href=\"https:\/\/qunitjs.com\/\" target=\"_blank\">qunit test case<\/a> to help introduce the idea of &#8220;test first design.&#8221; \u00a0 \u00a0The &#8220;MathUtil&#8221; class has a method for calculating the distance between two points. \u00a0In the &#8220;arrange&#8221; section of the code, we construct the variables and objects of the test case. \u00a0 \u00a0After we execute the method under test, we assert that the method &#8220;GetDistanceBetweenPoints&#8221; returned the correct answer.<\/p>\n<p><code><br \/>\ntest( \"MathUtil__GetDistanceBetweenPoints__Success\", function() {<br \/>\n\/\/arrange<br \/>\nvar util = new MathUtil();<br \/>\nvar x1 = 0;<br \/>\nvar y1 = 0;<br \/>\nvar x2 = 5;<br \/>\nvar y2 = 5;<\/p>\n<p>\/\/act<br \/>\nvar result = util.GetDistanceBetweenPoints(x1,y1,x2,y2)<\/p>\n<p>\/\/assert<br \/>\nok( result == 7.0710678118654755 );<\/p>\n<p>});<br \/>\n<\/code><\/p>\n<p>Unit test frameworks exist <a href=\"http:\/\/en.wikipedia.org\/wiki\/List_of_unit_testing_frameworks\" target=\"_blank\">in every computer language<\/a>. \u00a0 The process for test first design is pretty simple. \u00a0 Write a test that fails. \u00a0 After the test case compiles\/parses correctly, write just enough code to make the failing test pass. \u00a0 \u00a0You are encouraged to write the most simple code that could possibly make the test case pass. \u00a0 \u00a0 With test cases passing, take a little bit of time to clean up or improve the code. \u00a0 That&#8217;s it! \u00a0 Repeat the process as much as you like.<\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/tddCycle.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1396\" title=\"tddCycle\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/tddCycle-300x146.jpg\" alt=\"TDD cycle\" width=\"300\" height=\"146\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/tddCycle-300x146.jpg 300w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2014\/03\/tddCycle.jpg 703w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>By collecting your test cases over time, you can build a suite of test that will help you discover regressions in the system. \u00a0 \u00a0I love that this coding methodology helps you find semantic errors in your code quickly. \u00a0 What are some other benefits of using &#8220;test first design?&#8221; \u00a0 Here are just a few:<\/p>\n<ul>\n<li><span style=\"font-size: 13px;\"><strong>Find bugs early:<\/strong> \u00a0In general, the longer a bug remains in a code base, the more complex and expensive it will be to remove it. \u00a0 \u00a0&#8220;Test first design&#8221; helps you discover and remove defects early. \u00a0 \u00a0I don&#8217;t want to over sell this methodology. \u00a0 \u00a0You still need to use manual, scenario based testing, demo sessions, and peer reviews to detect and remove other classes of defects. \u00a0<\/span><\/li>\n<li><span style=\"font-size: 13px;\"><strong>Designing Lego blocks of working software:<\/strong> In test driven development (TDD), we are trying to test each method of a class in isolation. \u00a0 This pattern of construction encourages us to design our software in baby steps. \u00a0 \u00a0The baby steps tend to be loosely coupled.<\/span><\/li>\n<li><span style=\"font-size: 13px;\"><strong>Documentation of behavior:<\/strong> TDD use cases can be used to document most of \u00a0the assumptions of a class. \u00a0 It&#8217;s awesome that your test framework can document these assumptions in code and inform you of problems when the test suite is executed.<\/span><\/li>\n<li><strong>TDD encourages programmers to set and achieve goals: \u00a0<\/strong>By crafting a test case before writing production code, you are encouraging lots of good programming behaviors. \u00a0 \u00a0By writing a test first, your are more likely to be thoughtful regarding the naming of your methods. \u00a0 You are more likely to be careful about the data getting passed into the method and how the method will return data. \u00a0 \u00a0TDD also encourages you to plan the behavior of the class.<\/li>\n<\/ul>\n<div>In this post, we have introduced the <span style=\"text-decoration: underline;\"><strong>basics<\/strong><\/span> of TDD and test first design. \u00a0 The following video and <a href=\"http:\/\/misko.hevery.com\/code-reviewers-guide\/\" target=\"_blank\">e-book from Misko Hevery<\/a> teaches you advanced concepts and tips for making your software more testable. \u00a0 \u00a0Misko is a thoughtful test professional from Google. \u00a0 \u00a0This is one of my favorite videos to help introduce TDD. \u00a0I hope you enjoy it.<\/div>\n<div><\/div>\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/wEhu57pih5w\" frameborder=\"0\" width=\"420\" height=\"315\"><\/iframe><\/p>\n<p><strong>InspiredToEducate.NET Posts<\/strong><\/p>\n<ul>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1285\" target=\"_blank\">10 Free Resources for Learning JavaScript and HTML5<\/a><\/li>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1319\" target=\"_blank\">Easy Data Visualization with Google Charts and JavaScript<\/a><\/li>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1238\" target=\"_blank\">17 Fun Tools To Teach Kids To Code by @ChrisBetcher<\/a><\/li>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=795\" target=\"_blank\">Learn to Build Your Own Conversational Bot using ChatScript<\/a><\/li>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=623\" target=\"_blank\">Benefits of Teaching Kids To Code That No One Is Talking About<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdesigning-software-thats-easy-to-test%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>As programmers, we desire to create well crafted software that delights our customers. \u00a0 Because we care about the long term maintainability of our code, we aspire to craft our code well. \u00a0 \u00a0In the ideal case, future programmers should find it easy to change our code while avoiding errors [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,23],"tags":[],"_links":{"self":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1394"}],"collection":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=1394"}],"version-history":[{"count":4,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"predecessor-version":[{"id":1398,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1394\/revisions\/1398"}],"wp:attachment":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}