{"id":7,"date":"2015-01-20T17:34:40","date_gmt":"2015-01-20T17:34:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=7"},"modified":"2015-01-21T15:39:49","modified_gmt":"2015-01-21T15:39:49","slug":"the-other-kind-of-continuous-integration","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2015\/01\/20\/the-other-kind-of-continuous-integration\/","title":{"rendered":"The Other Kind of Continuous Integration"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><!--introduction--><p>Hi there. I am super excited to introduce our new Developer Zone blog! I work in a group at MathWorks that is responsible for building the infrastructure and architecture that we require as a company to deliver software that is of high quality, on schedule, and meets customer requirements. In short, we develop the build, test, and integration tools needed to produce production grade software on multiple platforms and using many different technologies. My specific team is focused on test infrastructure and are the primary developers of MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab-unit-test-framework.html\">unit test framework<\/a>.<\/p><p>As you might imagine I am very interested in development approaches and what it takes to build production software. This blog is a recognition of the fact that we are not the only ones who have a passion for this. Indeed, we hear from many of you who are facing similar tasks building production grade software using MATLAB&reg; as a critical piece of the technology stack, and we have literally thousands of world class MATLAB developers in house doing the same thing. I am excited to share some of the building blocks we use to develop robust, extendable, flexible software that is written in MATLAB, and I am likewise excited to hear how you are doing the same.<\/p><p>To start, for our blog's first post I thought it would be fitting to describe how MATLAB can interface with a CI system which is a must have for collaborating with others to build high quality software.<\/p><!--\/introduction--><p><b>CI? C-who?<\/b><\/p><p>Wait, what am I talking about, CI system? Well, what do you think of when you hear the term \"Continuous Integration\"? Do you think of calculating anti-derivatives and definite integrals of continuous functions or interfacing with a system that helps build and test software? A quick google check shows that most people think the latter, although perhaps those involved in mathematics may skew differently.<\/p><p>Whatever the case may be, developing software using a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Continuous_integration\">continuous integration (CI) system<\/a> has proven to be essential for production grade software. The services and safety net that these systems provide enable the creation of software that can rise to high levels of safety and quality.<\/p><p>So how is this done using MATLAB?<\/p><p>There are a few common purposes of continuous integration systems. Three important purposes are:<\/p><div><ol><li>Interfacing with a source control system<\/li><li>Building software that requires a compilation step like C++ or Java&reg;<\/li><li>Testing software that has changed or been rebuilt<\/li><\/ol><\/div><p>Connecting your CI system with your source control system depends on both systems and this can be done independently from MATLAB.<\/p><p>As for building software, there are many features of MATLAB which do indeed require a build step, such as writing MEX files, writing Java classes that work with MATLAB, or p-coding MATLAB files for distribution. However, probably the most common MATLAB activity is writing code using the MATLAB language, which does not require a build step. For now, let's scope this discussion to these types of MATLAB activities that do not have any artifacts to build (i.e. the MATLAB code itself is the artifact).<\/p><p><b>Connect MATLAB<\/b><\/p><p>If you are producing and sharing MATLAB code files, and they are already configured in your source control system then great, we are almost there. We just need to make sure that when you change your MATLAB source code your tests get run in your CI System and trigger build failures. Well, that is what I want to explore in more depth today. Note that there are a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_continuous_integration_software\">variety of CI systems out there<\/a>, but let's look at connecting MATLAB tests to <a href=\"http:\/\/jenkins-ci.org\/\">Jenkins&#8482;<\/a>, which is popular and integrates with a variety of systems.<\/p><p>To do this we need to:<\/p><div><ol><li>Configure Jenkins to launch MATLAB, run the tests, and close MATLAB<\/li><li>Communicate the results of the test run from MATLAB to the Jenkins software<\/li><li>Ride out our days relaxing on the beach instead of manually integrating our code changes<\/li><\/ol><\/div><p>Launching MATLAB can be done by creating a new free-form project in Jenkins. This type of project will allow us to set up a simple build script to launch MATLAB and run the tests. In this case I've named the job <a href=\"https:\/\/blogs.mathworks.com\/developer\/files\/runALL_THE_TESTS.jpg\">\"Run ALL THE TESTS\".<\/a><\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015NewJob.png\" alt=\"\"> <\/p><p>Now we can create our build action to run a shell script.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015BuildExecuteShell.png\" alt=\"\"> <\/p><p>The shell command that we use needs to simply start MATLAB and run something. We can use this simple shell command to just confirm we can get this MATLAB running. Use this script and run a test build in Jenkins:<\/p><pre>  \/Applications\/MATLAB_R2014b.app\/bin\/matlab -nodisplay -r \"disp('Hello World!');exit\"<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015HelloWorldLog.png\" alt=\"\"> <\/p><p><b>Connect the Tests!<\/b><\/p><p>Great! We are connected. Next step is to actually run something in MATLAB. This is dependent upon on how you have your tests organized and precisely which tests you wish to run. This exercise demonstrates running tests that are located in a \"testcases\" package as well as its sub-packages. Here's the updated shell script:<\/p><pre class=\"language-matlab\">\/Applications\/MATLAB_R2014b.app\/bin\/matlab -nodisplay -r \"runALL_THE_TESTS\"\r\n<\/pre><p>and here is the content of the <tt>runAll_THE_TESTS<\/tt> script:<\/p><pre class=\"language-matlab\">import <span class=\"string\">matlab.unittest.TestSuite<\/span>;\r\n\r\n<span class=\"keyword\">try<\/span>\r\n    suite = TestSuite.fromPackage(<span class=\"string\">'testcases'<\/span>,<span class=\"string\">'IncludingSubpackages'<\/span>,true);\r\n    results = run(suite);\r\n    display(results);\r\n<span class=\"keyword\">catch<\/span> e\r\n    disp(getReport(e,<span class=\"string\">'extended'<\/span>));\r\n    exit(1);\r\n<span class=\"keyword\">end<\/span>\r\nexit;\r\n<\/pre><p>So in short we want to create a TestSuite from our package and its sub-packages and then just run the tests using the default TestRunner. The try-catch usage is to allow MATLAB to exit quickly in the event of some unforeseen problem, but notice that before doing so the code prints the extended error details and exits with a non-zero status code. This will ensure that the Jenkins task will cause a build failure if something goes terribly wrong.<\/p><p>Let's run another job and see our tests in action<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015FailingTestsPassingJob1.png\" alt=\"\"> <\/p><p>We are in the business of running tests! Unfortunately we are only running them and not reacting in any way to test failures. You can see from the blue job marker above that the Jenkins job actually passed despite many of the tests actually failing. This is no good! Looking at the bottom of the test log we see that 163 tests passed, 70 tests failed, and 80 tests did not complete, so why did the job pass?<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015FailingTestsPassingJob2.png\" alt=\"\"> <\/p><p>What is happening here is that the Jenkins CI system does not know the results of this test run. The job determined pass or failure by looking at the exit status code of the MATLAB process we kicked off. While the tests failed, MATLAB was entirely happy as it started, ran the failing test suite and then exited cleanly. We need to tell MATLAB to exit with a non-zero status code if any test failures were encountered. We can do this by simply changing the last line of our run script to do just that:<\/p><pre>  exit(any([results.Failed]));<\/pre><p>Running the job we can see that we now see the test failures as job failures<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015FailingTestsFailingJob1.png\" alt=\"\"> <\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/2015FailingTestsFailingJob2.png\" alt=\"\"> <\/p><p><b>For the win!<\/b><\/p><p>After just a few minutes we are now in business with MATLAB connected to our Jenkins CI system, running tests and failing jobs when those tests fail. Awesome! This will work in all releases that support the test framework (all the way back to R2013a), and now you can set up Jenkins run these builds on a regular basis, such as nightly or whenever a file is checked into the SCM system. If you have R2014a or later we can do even better, but I'll save that for the next blog post.<\/p><p>Do you automatically test your MATLAB code changes with a CI system? If so, which one? What benefits have you realized in your development process? What tips and tricks have you learned along the way? Let us know in the comments section below.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_8b16203af41c4519997d2794e50c19a0() {\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='8b16203af41c4519997d2794e50c19a0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8b16203af41c4519997d2794e50c19a0';\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        copyright = 'Copyright 2015 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_8b16203af41c4519997d2794e50c19a0()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014b<br><\/p><\/div><!--\r\n8b16203af41c4519997d2794e50c19a0 ##### SOURCE BEGIN #####\r\n%% The Other Kind of Continuous Integration\r\n% Hi there. I am super excited to introduce our new Developer Zone blog! I\r\n% work in a group at MathWorks that is responsible for building the\r\n% infrastructure and architecture that we require as a company to deliver\r\n% software that is of high quality, on schedule, and meets customer\r\n% requirements. In short, we develop the build, test, and integration tools\r\n% needed to produce production grade software on multiple platforms and\r\n% using many different technologies. My specific team is focused on test\r\n% infrastructure and are the primary developers of MATLAB's\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab-unit-test-framework.html\r\n% unit test framework>.\r\n%\r\n% As you might imagine I am very interested in development approaches and\r\n% what it takes to build production software. This blog is a recognition of\r\n% the fact that we are not the only ones who have a passion for this.\r\n% Indeed, we hear from many of you who are facing similar tasks building\r\n% production grade software using MATLAB(R) as a critical piece of the\r\n% technology stack, and we have literally thousands of world class MATLAB\r\n% developers in house doing the same thing. I am excited to share some of\r\n% the building blocks we use to develop robust, extendable, flexible\r\n% software that is written in MATLAB, and I am likewise excited to hear how\r\n% you are doing the same.\r\n%\r\n% To start, for our blog's first post I thought it would be fitting to\r\n% describe how MATLAB can interface with a CI system which is a must have\r\n% for collaborating with others to build high quality software.\r\n%\r\n%% \r\n% *CI? C-who?*\r\n%\r\n% Wait, what am I talking about, CI system? Well, what do you think of when\r\n% you hear the term \"Continuous Integration\"? Do you think of calculating\r\n% anti-derivatives and definite integrals of continuous functions or\r\n% interfacing with a system that helps build and test software? A quick\r\n% google check shows that most people think the latter, although perhaps\r\n% those involved in mathematics may skew differently.\r\n%\r\n% Whatever the case may be, developing software using a\r\n% <https:\/\/en.wikipedia.org\/wiki\/Continuous_integration continuous\r\n% integration (CI) system> has proven to be essential for production grade\r\n% software. The services and safety net that these systems provide\r\n% enable the creation of software that can rise to high levels of safety\r\n% and quality.\r\n%\r\n% So how is this done using MATLAB?\r\n\r\n\r\n%% \r\n% There are a few common purposes of continuous integration systems. Three\r\n% important purposes are:\r\n%\r\n% \r\n% # Interfacing with a source control system \r\n% # Building software that requires a compilation step like C++ or Java(R)\r\n% # Testing software that has changed or been rebuilt\r\n% \r\n% Connecting your CI system with your source control system depends on both\r\n% systems and this can be done independently from MATLAB.\r\n%\r\n% As for building software, there are many features of MATLAB which do\r\n% indeed require a build step, such as writing MEX files, writing Java\r\n% classes that work with MATLAB, or p-coding MATLAB files for distribution.\r\n% However, probably the most common MATLAB activity is writing code using\r\n% the MATLAB language, which does not require a build step. For now, let's\r\n% scope this discussion to these types of MATLAB activities that do not\r\n% have any artifacts to build (i.e. the MATLAB code itself is the\r\n% artifact).\r\n%\r\n\r\n%% \r\n% *Connect MATLAB*\r\n%\r\n% If you are producing and sharing MATLAB code files, and they are already\r\n% configured in your source control system then great, we are almost there.\r\n% We just need to make sure that when you change your MATLAB source code\r\n% your tests get run in your CI System and trigger build failures. Well,\r\n% that is what I want to explore in more depth today. Note that there are a\r\n% <https:\/\/en.wikipedia.org\/wiki\/Comparison_of_continuous_integration_software\r\n% variety of CI systems out there>, but let's look at connecting MATLAB\r\n% tests to <http:\/\/jenkins-ci.org\/ Jenkins(TM)>, which is popular and\r\n% integrates with a variety of systems.\r\n%\r\n%\r\n% To do this we need to:\r\n% \r\n% \r\n% # Configure Jenkins to launch MATLAB, run the tests, and close MATLAB\r\n% # Communicate the results of the test run from MATLAB to the Jenkins\r\n% software\r\n% # Ride out our days relaxing on the beach instead of manually integrating\r\n% our code changes\r\n%\r\n%\r\n% Launching MATLAB can be done by creating a new free-form project in\r\n% Jenkins. This type of project will allow us to set up a simple build\r\n% script to launch MATLAB and run the tests. In this case I've named the\r\n% job <https:\/\/blogs.mathworks.com\/developer\/files\/runALL_THE_TESTS.jpg \"Run\r\n% ALL THE TESTS\".>\r\n% \r\n% <<2015NewJob.png>>\r\n% \r\n% \r\n% Now we can create our build action to run a shell script.\r\n%\r\n% <<2015BuildExecuteShell.png>>\r\n%\r\n% The shell command that we use needs to simply start MATLAB and run\r\n% something. We can use this simple shell command to just confirm we can\r\n% get this MATLAB running. Use this script and run a test build in Jenkins:\r\n%\r\n%    \/Applications\/MATLAB_R2014b.app\/bin\/matlab -nodisplay -r \"disp('Hello World!');exit\"\r\n%\r\n% <<2015HelloWorldLog.png>>\r\n%\r\n%%\r\n% *Connect the Tests!*\r\n%\r\n% Great! We are connected. Next step is to actually run something in\r\n% MATLAB. This is dependent upon on how you have your tests organized and\r\n% precisely which tests you wish to run. This exercise demonstrates running\r\n% tests that are located in a \"testcases\" package as well as its\r\n% sub-packages. Here's the updated shell script:\r\n% \r\n%   \/Applications\/MATLAB_R2014b.app\/bin\/matlab -nodisplay -r \"runALL_THE_TESTS\"\r\n% \r\n% and here is the content of the |runAll_THE_TESTS| script:\r\n%\r\n% \r\n%   import matlab.unittest.TestSuite;\r\n%   \r\n%   try\r\n%       suite = TestSuite.fromPackage('testcases','IncludingSubpackages',true);\r\n%       results = run(suite);\r\n%       display(results);\r\n%   catch e\r\n%       disp(getReport(e,'extended'));\r\n%       exit(1);\r\n%   end\r\n%   exit;\r\n% \r\n% So in short we want to create a TestSuite from our package and its\r\n% sub-packages and then just run the tests using the default TestRunner.\r\n% The try-catch usage is to allow MATLAB to exit quickly in the event of\r\n% some unforeseen problem, but notice that before doing so the code prints\r\n% the extended error details and exits with a non-zero status code. This\r\n% will ensure that the Jenkins task will cause a build failure if something\r\n% goes terribly wrong.\r\n%\r\n% Let's run another job and see our tests in action\r\n%\r\n% <<2015FailingTestsPassingJob1.png>>\r\n%\r\n% We are in the business of running tests! Unfortunately we are only\r\n% running them and not reacting in any way to test failures. You can see\r\n% from the blue job marker above that the Jenkins job actually passed despite\r\n% many of the tests actually failing. This is no good! Looking at the\r\n% bottom of the test log we see that 163 tests passed, 70 tests failed, and\r\n% 80 tests did not complete, so why did the job pass?\r\n%\r\n% <<2015FailingTestsPassingJob2.png>>\r\n%\r\n% \r\n% What is happening here is that the Jenkins CI system does not know the\r\n% results of this test run. The job determined pass or failure by looking\r\n% at the exit status code of the MATLAB process we kicked off. While the\r\n% tests failed, MATLAB was entirely happy as it started, ran the failing\r\n% test suite and then exited cleanly. We need to tell MATLAB to exit with a\r\n% non-zero status code if any test failures were encountered. We can\r\n% do this by simply changing the last line of our run script to do just\r\n% that:\r\n%\r\n%    exit(any([results.Failed]));\r\n% \r\n% Running the job we can see that we now see the test failures as job\r\n% failures\r\n%\r\n% <<2015FailingTestsFailingJob1.png>>\r\n%\r\n% <<2015FailingTestsFailingJob2.png>>\r\n%\r\n%\r\n%% \r\n% *For the win!*\r\n%\r\n% After just a few minutes we are now in business with MATLAB connected to\r\n% our Jenkins CI system, running tests and failing jobs when those tests\r\n% fail. Awesome! This will work in all releases that support the test\r\n% framework (all the way back to R2013a), and now you can set up Jenkins\r\n% run these builds on a regular basis, such as nightly or whenever a file\r\n% is checked into the SCM system. If you have R2014a or later we can do\r\n% even better, but I'll save that for the next blog post.\r\n%\r\n% Do you automatically test your MATLAB code changes with\r\n% a CI system? If so, which one? What benefits have you realized in your\r\n% development process? What tips and tricks have you learned along the way?\r\n% Let us know in the comments section below.\r\n\r\n\r\n##### SOURCE END ##### 8b16203af41c4519997d2794e50c19a0\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/developer\/files\/runALL_THE_TESTS.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>Hi there. I am super excited to introduce our new Developer Zone blog! I work in a group at MathWorks that is responsible for building the infrastructure and architecture that we require as a company to deliver software that is of high quality, on schedule, and meets customer requirements. In short, we develop the build, test, and integration tools needed to produce production grade software on multiple platforms and using many different technologies. My specific team is focused on test infrastructure and are the primary developers of MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab-unit-test-framework.html\">unit test framework<\/a>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2015\/01\/20\/the-other-kind-of-continuous-integration\/\">read more >><\/a><\/p>","protected":false},"author":90,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":10,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/7\/revisions\/49"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}