{"id":2573,"date":"2010-06-25T11:57:07","date_gmt":"2010-06-25T11:57:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/25\/matlab-xunit-test-framework\/"},"modified":"2016-07-22T12:53:23","modified_gmt":"2016-07-22T16:53:23","slug":"matlab-xunit-test-framework","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/25\/matlab-xunit-test-framework\/","title":{"rendered":"MATLAB xUnit Test Framework"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<hr>\r\n<br>\r\n<b><font color=\"red\">NOTE!<\/font><\/b> As of R2013a, MATLAB has a fully supported <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab-unit-test-framework.html\">built-in unit testing framework<\/a>.  If you are just getting started, please investigate it first!<\/br><hr><\/br>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021\">Bob<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47302-xunit4\">MATLAB xUnit Test Framework<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22204\">Steve Eddins<\/a> and now maintained by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1380672-paul-sexton\">Paul Sexton<\/a>.\r\n\r\n&nbsp;\r\n\r\nDo you use MATLAB to develop algorithms? Does your code rely on functions created by others? What kind of testing do you do?\r\nFor many users the answer may be something like this. \"I tried a problem where I knew the solution. Since the right answer\r\nwas returned, I trust it.\" For some, that may be good enough. For others, a more systematic and comprehensive approach may\r\nbe required.\r\n\r\nI know customers in some industries need to follow a strict process, and automated test suites allow a battery of test cases\r\nto be exercised. So for example when any changes need to be made, the tests can be repeated to ensure no inadvertent bugs\r\nwere introduced in your code while adding a new feature. Now think beyond your own code. Maybe you just received a new version\r\nof your favorite third party toolbox. Your boss wants you to check it out before deploying across the organization. Maybe\r\nyour software compliance or certification procedures dictate that new releases of all computing platforms, including MATLAB,\r\nmust be qualified before you can upgrade. This is where automated testing pays dividends.\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47302-xunit4\">Steve's test harness<\/a> delivers! The detailed documentation is well organized from getting started to advanced usage. To illustrate, I used <tt>xunit<\/tt> to test one of my submissions, <tt><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4261-round2\">round2<\/a><\/tt>. The function help includes some example problems.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">help <span style=\"color: #a020f0;\">round2<\/span><\/pre>\r\n<pre style=\"font-style: oblique;\"> ROUND2 rounds number to nearest multiple of arbitrary precision.\r\n    Z = ROUND2(X,Y) rounds X to nearest multiple of Y.\r\n \r\n Example 1: round PI to 2 decimal places\r\n    &gt;&gt; round2(pi,0.01)\r\n    ans =\r\n          3.14\r\n \r\n Example 2: round PI to 4 decimal places\r\n    &gt;&gt; round2(pi,1e-4)\r\n    ans =\r\n          3.1416\r\n \r\n Example 3: round PI to 8-bit fraction\r\n    &gt;&gt; round2(pi,2^-8)\r\n    ans =\r\n          3.1406\r\n \r\n Examples 4-6: round PI to other multiples\r\n    &gt;&gt; round2(pi,0.05)\r\n    ans =\r\n          3.15\r\n    &gt;&gt; round2(pi,2)\r\n    ans =\r\n          4\r\n    &gt;&gt; round2(pi,5)\r\n    ans =\r\n          5 \r\n \r\n  See also ROUND.\r\n\r\n<\/pre>\r\nExamples usually make excellent test cases. Turning them into test points was a simple matter of codifying function calls\r\nto return actual values, and providing expected values for comparison.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dbtype <span style=\"color: #a020f0;\">test_round2<\/span><\/pre>\r\n<pre style=\"font-style: oblique;\">1     function test_suite = test_round2\r\n2     initTestSuite\r\n3     \r\n4     function testExample1\r\n5     assertEqual(round2(pi,0.01),3.14)\r\n6     \r\n7     function testExample2\r\n8     assertEqual(round2(pi,1e-4),3.1416)\r\n9     \r\n10    function testExample3\r\n11    assertElementsAlmostEqual(round2(pi,2^-8),3.1406,'absolute',0.0001)\r\n12    \r\n13    function testExample4\r\n14    assertElementsAlmostEqual(round2(pi,0.05),3.15,'absolute',0.01)\r\n15    \r\n16    function testExample5\r\n17    assertEqual(round2(pi,2),4)\r\n18    \r\n19    function testExample6\r\n20    assertEqual(round2(pi,5),5)\r\n\r\n<\/pre>\r\nNote that I also specified tolerances where appropriate. Now, running this test suite is straightforward.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">test_round2<\/pre>\r\n<pre style=\"font-style: oblique;\">Starting test run with 6 test cases.\r\n......\r\nPASSED in 0.010 seconds.\r\n<\/pre>\r\nBe sure to check out his blog, <a href=\"https:\/\/blogs.mathworks.com\/steve\">Steve on Image Processing<\/a> in general, and two posts in particular.\r\n<div>\r\n<ul>\r\n\t<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/16\/matlab-xunit-new-version-available\/\">MATLAB xUnit: new version available<\/a> (2010-06-16)<\/li>\r\n\t<li><a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/03\/mtest-a-unit-test-harness-for-matlab-code\/\">MTEST - A unit test harness for MATLAB code<\/a> (2009-02-03)<\/li>\r\n<\/ul>\r\n<\/div>\r\nYou can also read his <i>IEEE Computing in Science and Engineering<\/i> journal article, <a href=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/Automated%20Software%20Testing%20for%20MATLAB%20(2009).pdf\">Automated Software Testing for MATLAB<\/a> for more background. That touches on many facets of testing such as choosing good test cases to ensure code coverage.\r\n\r\nAs one of the Quality Engineers that Steve mentioned, I found <tt>xunit<\/tt> both powerful and easy to use. Since learning some things about Agile and Test-Driven development processes I see <tt>xunit<\/tt> being useful from small projects to large. From first learning MATLAB as an EE student in 1985 until I left the world of\r\napplied R&amp;D to join The MathWorks in 2001, I wrote a lot of MATLAB code, and I used what I knew: quick and dirty development.\r\nAs an application engineer, like Brett and Jiro, I met customers in many industries, and saw a wide user spectrum from novice\r\nnonprogrammers to seasoned CS experts. Having moved along that spectrum myself, I really appreciate how much the bar has been\r\nlowered. With MATLAB and <tt>xunit<\/tt> automated testing is not just for specialists. Thanks, Steve!\r\n\r\nIn this day and age software can change rapidly. This is the new normal. Enhancements may be demanded. Some bug fixes can\r\nbreak prior workarounds. Programs can be really complex and rely on code from many different programmers. Maybe a colleague\r\nchanges a network shared file. Programs that worked once upon a time can break someday. These are modern facts of life. Automated\r\ntesting provides regression insurance in a world of change. How prepared are you?\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_55daccb2e41442b4856449bc6c20c8b5() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='55daccb2e41442b4856449bc6c20c8b5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 55daccb2e41442b4856449bc6c20c8b5';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Robert Bemis';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.10<\/p>\r\n\r\n<\/div>\r\n<!--\r\n55daccb2e41442b4856449bc6c20c8b5 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021 Bob>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22846-matlab-xunit-test-framework MATLAB xUnit Test Framework>\r\n% by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22204 Steve Eddins>.\r\n%%\r\n% Do you use MATLAB to develop algorithms? Does your code rely on functions\r\n% created by others? What kind of testing do you do? For many users the\r\n% answer may be something like this. \"I tried a problem where I knew the\r\n% solution. Since the right answer was returned, I trust it.\" For some,\r\n% that may be good enough. For others, a more systematic and comprehensive\r\n% approach may be required.\r\n%%\r\n% I know customers in some industries need to follow a strict process, and\r\n% automated test suites allow a battery of test cases to be exercised. So\r\n% for example when any changes need to be made, the tests can be repeated\r\n% to ensure no inadvertent bugs were introduced in your code while adding a\r\n% new feature. Now think beyond your own code. Maybe you just received a\r\n% new version of your favorite third party toolbox. Your boss wants you to\r\n% check it out before deploying across the organization. Maybe your\r\n% software compliance or certification procedures dictate that new releases\r\n% of all computing platforms, including MATLAB, must be qualified before\r\n% you can upgrade. This is where automated testing pays dividends.\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22846-matlab-xunit-test-framework Steve's test harness>\r\n% delivers! The detailed\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fx_files\/22846\/8\/content\/matlab_xunit_3.0\/doc\/xunit_product_page.html documentation>\r\n% is well organized from getting started to advanced usage. To illustrate,\r\n% I used |xunit| to test one of my submissions,\r\n% |<https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4261-round2 round2>|.\r\n% The function help includes some example problems.\r\nhelp round2\r\n%%\r\n% Examples usually make excellent test cases. Turning them into test points\r\n% was a simple matter of codifying function calls to return actual values,\r\n% and providing expected values for comparison.\r\ndbtype test_round2\r\n%%\r\n% Note that I also specified tolerances where appropriate. Now, running\r\n% this test suite is straightforward.\r\ntest_round2\r\n%%\r\n% Be sure to check out his blog,\r\n% <https:\/\/blogs.mathworks.com\/steve Steve on Image Processing>\r\n% in general, and two posts in particular.\r\n%%\r\n% * <https:\/\/blogs.mathworks.com\/steve\/2010\/06\/16\/matlab-xunit-new-version-available\/ MATLAB xUnit: new version available> (2010-06-16)\r\n% * <https:\/\/blogs.mathworks.com\/steve\/2009\/02\/03\/mtest-a-unit-test-harness-for-matlab-code\/ MTEST - A unit test harness for MATLAB code> (2009-02-03)\r\n%%\r\n% You can also read his _IEEE Computing in Science and Engineering_ journal article,\r\n% <https:\/\/blogs.mathworks.com\/images\/steve\/2010\/Automated%20Software%20Testing%20for%20MATLAB%20(2009).pdf Automated Software Testing for MATLAB>\r\n% for more background. That touches on many facets of testing such as\r\n% choosing good test cases to ensure code coverage.\r\n%%\r\n% As one of the Quality Engineers that Steve mentioned, I found |xunit|\r\n% both powerful and easy to use. Since learning some things about Agile and\r\n% Test-Driven development processes I see |xunit| being useful from small\r\n% projects to large. From first learning MATLAB as an EE student in 1985\r\n% until I left the world of applied R&D to join The MathWorks in 2001, I\r\n% wrote a lot of MATLAB code, and I used what I knew: quick and dirty\r\n% development. As an application engineer, like Brett and Jiro, I met\r\n% customers in many industries, and saw a wide user spectrum from novice\r\n% nonprogrammers to seasoned CS experts. Having moved along that spectrum\r\n% myself, I really appreciate how much the bar has been lowered. With\r\n% MATLAB and |xunit| automated testing is not just for specialists. Thanks,\r\n% Steve!\r\n%%\r\n% In this day and age software can change rapidly. This is the new normal.\r\n% Enhancements may be demanded. Some bug fixes can break prior workarounds.\r\n% Programs can be really complex and rely on code from many different\r\n% programmers. Maybe a colleague changes a network shared file. Programs\r\n% that worked once upon a time can break someday. These are modern facts of\r\n% life. Automated testing provides regression insurance in a world of\r\n% change. How prepared are you?\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2573#respond Comments?>\r\n\r\n##### SOURCE END ##### 55daccb2e41442b4856449bc6c20c8b5\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\n\r\n\r\nNOTE! As of R2013a, MATLAB has a fully supported built-in unit testing framework.  If you are just getting started, please investigate it first!\r\n\r\nBob's pick this week is MATLAB xUnit Test... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/25\/matlab-xunit-test-framework\/\">read more >><\/a><\/p>","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2573"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2573"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2573\/revisions"}],"predecessor-version":[{"id":7729,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2573\/revisions\/7729"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}