{"id":7838,"date":"2016-09-16T09:00:18","date_gmt":"2016-09-16T13:00:18","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7838"},"modified":"2017-08-11T13:25:07","modified_gmt":"2017-08-11T17:25:07","slug":"i-think-youre-just-trying-to-test-me","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/09\/16\/i-think-youre-just-trying-to-test-me\/","title":{"rendered":"I Think You&#8217;re Just Trying to Test Me!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift\">Unit Testing C Code using MATLAB and MATLAB Coder<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/652627-bill-chou\">Bill Chou<\/a>.\r\n      <\/p>\r\n      <p>In general I try to pick entries developed by those outside MathWorks, but Bill has provided a nice demonstration that combines\r\n         two topics I'm passionate about: formalize testing and code generation.\r\n      <\/p>\r\n<p><\/p>\r\n      <p>Using unit testing in conjunction with MATLAB Coder enables three capabilities:<\/p>\r\n      <li>Automatically define interface data types for generated C-code.<\/li>\r\n      <li>Verify the C-code generated from MATLAB produces the expected results.<\/li>\r\n      <li>Execute tests developed in MATLAB against C-code developed outside the MATLAB environment.<\/li>\r\n      <p>I recommend taking a look at Bill's videos that go along with this entry.<\/p>\r\n      <div>\r\n         <ul>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/videos\/unit-testing-your-generated-code-using-matlab-coder-115873.html\">Unit Testing Your Generated Code Using MATLAB Coder<\/a><\/li>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/videos\/unit-testing-c-code-using-matlab-and-matlab-coder-115878.html\">Unit Testing C Code Using MATLAB and MATLAB Coder<\/a><\/li>\r\n         <\/ul>\r\n      <\/div>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What is unit testing and why would I use it?<\/a><\/li>\r\n         <li><a href=\"#2\">Wait...There's unit testing in MATLAB?<\/a><\/li>\r\n         <li><a href=\"#3\">Why would I use MATLAB to run tests on my C-code?<\/a><\/li>\r\n         <li><a href=\"#4\">Side benefit that Bill doesn't mention<\/a><\/li>\r\n         <li><a href=\"#5\">Modification to make test failures obvious.<\/a><\/li>\r\n         <li><a href=\"#6\">Any thoughts on this entry?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What is unit testing and why would I use it?<a name=\"1\"><\/a><\/h3>\r\n   <p>Fundamentally, a unit test determines if a single feature of software produces expected results under specific conditions.\r\n       The theory is, by keeping the tests \"small\" and specific, it is easier to develop and maintain the software and tests.\r\n   <\/p>\r\n   <p>The primary benefit of having tests is it helps determine if a design meets its requirements.  This could apply to the initial\r\n      design of the software, or when you decide to make changes to the software.\r\n   <\/p>\r\n   <p>There are all sorts of additional philosophies and techniques that can apply unit testing such as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Behavior-driven_development\">Behavior Driven Development (BDD)<\/a>.  I won't get into that here, sufficed to say that how you use unit tests will inform how you write them.\r\n   <\/p>\r\n   <h3>Wait...There's unit testing in MATLAB?<a name=\"2\"><\/a><\/h3>\r\n   <p>More specifically, there is a unit testing framework available in MATLAB. It is based on the <a href=\"https:\/\/en.wikipedia.org\/wiki\/XUnit\">xUnit<\/a> framework.  For those of you who might be stuck on older versions of MATLAB, it's worth checking out this File Exchange <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47302-xunit4\">entry<\/a>.\r\n   <\/p>\r\n   <p>The thing I like about using this the Unit Test framework in MATLAB is it provides a consistent means of defining tests, as\r\n      well as a number of tools and functions to automate test execution. It also provides a number of levels of sophistication,\r\n      so if you're not into writing classes and methods to exercise your code, you can use simple <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/script-based-unit-tests.html\">scripts<\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/function-based-unit-tests.html\">functions<\/a> instead.\r\n   <\/p>\r\n   <p>For more about using this framework, I suggest checking out Andy's blog <a href=\"https:\/\/blogs.mathworks.com\/developer\/category\/testing\/?s_tid=Blog_developer_Category\">here<\/a>.\r\n   <\/p>\r\n   <h3>Why would I use MATLAB to run tests on my C-code?<a name=\"3\"><\/a><\/h3>\r\n   <p>It seems unnecessary and possibly just using a tool because you can.<\/p>\r\n   <p>Well use the right tool for the job.  If you want your tests to leverage other MATLAB features that aren't readily available\r\n      in the language you're using (plotting, filtering, data management), then it may very well make sense to develop your test\r\n      suite in MATLAB.\r\n   <\/p>\r\n   <h3>Side benefit that Bill doesn't mention<a name=\"4\"><\/a><\/h3>\r\n   <p>In the videos I linked above, Bill does a nice job of going through how to apply the unit tests to the C-code testing process.\r\n       However, there is an additional benefit for C-code generation from MATLAB that Bill doesn't address directly in the videos\r\n      for this example.\r\n   <\/p>\r\n   <p>If you look at the second step of the MATLAB Coder app called \"Define Input Types\" you will notice that <tt>run_unit_tests<\/tt> appears in the dialog to \"Automatically define input types\".\r\n   <\/p>\r\n   <p>By executing the code found in this dialog, MATLAB Coder can determine what datatype is being used on the inputs to the function\r\n      for which you are generating code, and apply the appropriate constraints.\r\n   <\/p>\r\n   <p>\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestTypeDefinitionScript.png\"><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"200\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestTypeDefinitionScript.png\" alt=\"unittesttypedefinitionscript\" class=\"alignnone size-full wp-image-7843\" \/><\/a><\/p>\r\n   <p>In this case you will see that the <tt>addOne<\/tt> function has an input variable <tt>x<\/tt> which is a scalar ( 1 x 1) of type <tt>double<\/tt>.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestTypeDefinitionResult.png\"><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"150\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestTypeDefinitionResult.png\" alt=\"unittesttypedefinitionresult\" class=\"alignnone size-full wp-image-7842\" \/><\/a><\/p>\r\n   <p>An example of how to use this feature can be viewed at minute 2:30 of the video <a href=\"https:\/\/www.mathworks.com\/videos\/generating-c-code-from-matlab-code-68964.html\">here<\/a>.\r\n   <\/p>\r\n   <p>Type definition of the inputs is required because C-code is a statically typed language, while MATLAB is a dynamically typed\r\n      language. (Check out this <a href=\"http:\/\/stackoverflow.com\/questions\/2690544\/what-is-the-difference-between-a-strongly-typed-language-and-a-statically-typed\">discussion<\/a> for more detail about how these type systems differ)\r\n   <\/p>\r\n   <h3>Modification to make test failures obvious.<a name=\"5\"><\/a><\/h3>\r\n   <p>When the unit tests fail, you actually have to look at the test result summary to determine if there was a failure.<\/p>\r\n   <p>To make it more prominent when there is a failure, I recommend throwing an error or assertion if there are any test failures.\r\n       I changed <tt>run_unit_tests.m<\/tt> to the following:\r\n   <\/p><pre>  results = runtests( pwd );\r\n  if any([results.Failed])\r\n     error('Unit test failure');\r\n  end<\/pre><p><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestErrorMessage.png\"><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"300\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestErrorMessage.png\" alt=\"unittesterrormessage\" class=\"alignnone size-full wp-image-7841\" \/><\/a> <\/p>\r\n   <p>That way, it becomes very obvious that there is an issue to be addressed.<\/p>\r\n   <h3>Any thoughts on this entry?<a name=\"6\"><\/a><\/h3>\r\n   <p>Let us know <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7838#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6cdd2a6fe9114f80a76ca973a04e6239() {\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='6cdd2a6fe9114f80a76ca973a04e6239 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6cdd2a6fe9114f80a76ca973a04e6239';\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 = 'Greg Wolff';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<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>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_6cdd2a6fe9114f80a76ca973a04e6239()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 9.0<br><\/p>\r\n<\/div>\r\n<!--\r\n6cdd2a6fe9114f80a76ca973a04e6239 ##### SOURCE BEGIN #####\r\n%% I Think You're Just Trying to Test Me!\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620 Greg's> \r\n% pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift \r\n% Unit Testing C Code using MATLAB and MATLAB Coder> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/652627-bill-chou \r\n% Bill Chou>.\r\n% \r\n% \r\n% \r\n% In general I try to pick entries developed by those outside MathWorks, \r\n% but Bill has provided a nice demonstration that combines two topics I'm passionate \r\n% about: formalize testing and code generation.\r\n% \r\n% Using unit testing in conjunction with MATLAB Coder enables three capabilities:\r\n% \r\n% # Automatically define interface data types for generated C-code.\r\n% # Verify the C-code generated from MATLAB produces the expected results.\r\n% # Execute tests developed in MATLAB against C-code developed outside the MATLAB \r\n% environment.\r\n% \r\n% I recommend taking a look at Bill's videos that go along with this entry.\r\n% \r\n% * <https:\/\/www.mathworks.com\/videos\/unit-testing-your-generated-code-using-matlab-coder-115873.html \r\n% Unit Testing Your Generated Code Using MATLAB Coder>\r\n% * <https:\/\/www.mathworks.com\/videos\/unit-testing-c-code-using-matlab-and-matlab-coder-115878.html \r\n% Unit Testing C Code Using MATLAB and MATLAB Coder>\r\n%% What is unit testing and why would I use it?\r\n% Fundamentally, a unit test determines if a single feature of software produces \r\n% expected results under specific conditions.  The theory is, by keeping the tests \r\n% \"small\" and specific, it is easier to develop and maintain the software and \r\n% tests.\r\n% \r\n% The primary benefit of having tests is it helps determine if a design meets \r\n% its requirements.  This could apply to the initial design of the software, or \r\n% when you decide to make changes to the software.\r\n% \r\n% There are all sorts of additional philosophies and techniques that can \r\n% apply unit testing such as <https:\/\/en.wikipedia.org\/wiki\/Behavior-driven_development \r\n% Behavior Driven Development (BDD)>.  I won't get into that here, sufficed to \r\n% say that how you use unit tests will inform how you write them.\r\n%% Wait...There's unit testing in MATLAB?\r\n% More specifically, there is a unit testing framework available in MATLAB.  \r\n% It is based on the <https:\/\/en.wikipedia.org\/wiki\/XUnit xUnit> framework.  For \r\n% those of you who might be stuck on older versions of MATLAB, it's worth checking \r\n% out this File Exchange <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47302-xunit4 \r\n% entry>.\r\n% \r\n% The thing I like about using this the Unit Test framework in MATLAB is \r\n% it provides a consistent means of defining tests, as well as a number of tools \r\n% and functions to automate test execution. It also provides a number of levels \r\n% of sophistication, so if you're not into writing classes and methods to exercise \r\n% your code, you can use simple <https:\/\/www.mathworks.com\/help\/matlab\/script-based-unit-tests.html \r\n% scripts> or <https:\/\/www.mathworks.com\/help\/matlab\/function-based-unit-tests.html \r\n% functions> instead. \r\n% \r\n% For more about using this framework, I suggest checking out Andy's blog \r\n% <https:\/\/blogs.mathworks.com\/developer\/category\/testing\/?s_tid=Blog_developer_Category \r\n% here>.\r\n%% Why would I use MATLAB to run tests on my C-code?\r\n% It seems unnecessary and possibly just using a tool because you can.\r\n% \r\n% Well use the right tool for the job.  If you want your tests leverage other \r\n% MATLAB features that aren't readily available in the language you're using (plotting, \r\n% filtering, data management), then it may very well make sense to develop your \r\n% test suite in MATLAB.\r\n% \r\n% \r\n%% Side benefit that Bill doesn't mention\r\n% In the videos I linked above, Bill does a nice job of going through how to \r\n% apply the unit tests to the C-code testing process.  However, there is an additional \r\n% benefit for C-code generation from MATLAB that Bill doesn't address directly \r\n% in the videos for this example.  \r\n% \r\n% If you look at the second step of the MATLAB Coder app called \"Define Input \r\n% Types\" you will notice that |run_unit_tests| appears in the dialog to \"Automatically \r\n% define input types\".\r\n% \r\n%  \r\n% \r\n% By executing the code found in this dialog, MATLAB Coder can determine \r\n% what datatype is being used on the inputs to the function for which you are \r\n% generating code, and apply the appropriate constraints.\r\n% \r\n% <<unitTestTypeDefinitionScript.png>>\r\n% \r\n% In this case you will see that the |addOne| function has an input variable \r\n% |x| which is a scalar ( 1 x 1) of type |double|.\r\n%\r\n% \r\n% <<unitTestTypeDefinitionResult.png>>\r\n% \r\n% An example of how to use this feature can be viewed in at minute 2:30 of \r\n% the video <https:\/\/www.mathworks.com\/videos\/generating-c-code-from-matlab-code-68964.html \r\n% here>.\r\n% \r\n% Type definition of the inputs is required because C-code is a statically \r\n% typed language, while MATLAB is a dynamically typed language. (Check out this \r\n% <http:\/\/stackoverflow.com\/questions\/2690544\/what-is-the-difference-between-a-strongly-typed-language-and-a-statically-typed \r\n% discussion> for more detail about how these type systems differ)\r\n% \r\n% \r\n% \r\n% \r\n% \r\n% \r\n%% Modification to make test failures obvious.\r\n% When the unit tests fail, you actually have to look at the test result summary \r\n% to determine if there was a failure. \r\n% \r\n% \r\n% \r\n% To make it more prominent when there is a failure, I recommend throwing \r\n% an error or assertion if there are any test failures.  I changed |run_unit_tests.m| to the following:\r\n% \r\n% \r\n%    results = runtests( pwd );\r\n%    if any([results.Failed])\r\n%       error('Unit test failure');\r\n%    end\r\n% \r\n% \r\n% \r\n% <<unitTestErrorMessage.png>>\r\n% \r\n% \r\n% That way, it becomes very obvious that there is an issue to be addressed.\r\n% \r\n%  \r\n%% Any thoughts on this entry?\r\n% Let us know <https:\/\/blogs.mathworks.com\/pick\/ here>.\r\n##### SOURCE END ##### 6cdd2a6fe9114f80a76ca973a04e6239\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/unitTestTypeDefinitionScript.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Greg's pick this week is Unit Testing C Code using MATLAB and MATLAB Coder by Bill Chou.\r\n      \r\n      In general I try to pick entries developed by those outside MathWorks,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/09\/16\/i-think-youre-just-trying-to-test-me\/\">read more >><\/a><\/p>","protected":false},"author":36,"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\/7838"}],"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\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7838"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7838\/revisions"}],"predecessor-version":[{"id":8812,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7838\/revisions\/8812"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}