{"id":6543,"date":"2017-05-25T08:46:40","date_gmt":"2017-05-25T13:46:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=6543"},"modified":"2017-05-25T08:46:40","modified_gmt":"2017-05-25T13:46:40","slug":"simulation-based-testing-with-simulink-test-manager","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2017\/05\/25\/simulation-based-testing-with-simulink-test-manager\/","title":{"rendered":"Simulation Based Testing with Simulink Test Manager"},"content":{"rendered":"<p><em>Today, Ajay Puvvala is back to talk about testing.<\/em><\/p>\n<p><strong>Recapitulation<\/strong><\/p>\n<p>In <a href=\"https:\/\/blogs.mathworks.com\/simulink\/?p=6441\">last week's post<\/a>, we looked at how we could apply MATLAB Unit Testing Framework to Simulink context. We authored a scripted test to verify the output of the generated code of a simple model against normal mode simulation. In that test, we:<\/p>\n<ol>\n<li>Simulated the system under test in normal and software-in-the-loop to obtain expected and actual simulation results<\/li>\n<li>Leveraged the MATLAB Unit qualification API <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.unittest.qualifications.verifiable.verifyequal.html\">verifyEqual<\/a> to compare the numerics with and without tolerance<\/li>\n<li>Developed a simple SDI diagnostic to visualize the signal differences for failure investigation<\/li>\n<\/ol>\n<p>As you probably remember, steps 2 & 3 required writing a reasonable amount of code to extract the signals to compare and then defining a diagnostic that could help with failure analysis. That isn't unexpected, since the MATLAB Unit Testing Framework is a general purpose tool to test any software with MATLAB API. It doesn't specialize in a particular application domain.<\/p>\n<p>That's where <a href=\"https:\/\/www.mathworks.com\/products\/simulink-test.html\">Simulink Test<\/a> comes into picture, bringing in specialization for testing Simulink models. It provides tools for authoring, executing and managing simulation-based tests for models and generated code. It also provides the ability to create test harnesses that assist with testing selected components in a model independently. Its integration with products such as <a href=\"https:\/\/www.mathworks.com\/products\/simverification.html\">Simulink Verification and Validation<\/a> provides a great platform for model-based testing.<\/p>\n<p>Have not tried it yet? I strongly recommend you check out the <a href=\"https:\/\/www.mathworks.com\/products\/simulink-test.html\">product page<\/a>.<\/p>\n<p><strong>The Goal<\/strong><\/p>\n<p>In today's post, we use Simulink Test's <a href=\"https:\/\/www.mathworks.com\/help\/sltest\/ug\/introduction-to-the-test-manager.html\">Test Manager<\/a> to create and execute the same equivalence test we did in <a href=\"https:\/\/blogs.mathworks.com\/simulink\/?p=6441\">last week's post<\/a>.<\/p>\n<p>We want to validate the results of generated code, via Software-in-The-Loop simulation, against normal mode simulation of a simple model.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/challenge.png\" alt=\"Normal model versus SIL comparisin\" \/><\/p>\n<p><strong>Test Manager Setup<\/strong><\/p>\n<p>After launching the <a href=\"https:\/\/www.mathworks.com\/help\/sltest\/ug\/introduction-to-the-test-manager.html\">Test Manager<\/a> from the <strong>Analysis<\/strong> menu of the model, I select to create a new <strong>Test File From Model:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/newTestFileMenu.png\" alt=\"Creating a test File\" \/><\/p>\n<p>and I specify the type of test:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/newTestFile.png\" alt=\"Creating a test\" \/><\/p>\n<p><strong>Test Creation<\/strong><\/p>\n<p>When you select an equivalence test, it allows you to define simulation settings for two simulations. In this case, we will define Simulation 1 as normal mode simulation and Simulation 2 as software-in-the-loop simulation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/setupSIL.png\" alt=\"Creating equivalence test\" \/><\/p>\n<p><strong>Qualification Criteria<\/strong><\/p>\n<p>In the Equivalence Criteria section, you select which signal you want to compare. Using the Capture button, the Test Manager will analyze the model and list all the logged signals that could potentially be used in the comparison. In our case, it finds the Outport block, for which the logging is enabled.<\/p>\n<p>Once the signals are in the table, you can specify tolerances. As we did last week, we <a title=\"https:\/\/www.mathworks.com\/help\/sltest\/ug\/how-tolerances-are-applied-to-test-criteria.html (link no longer works)\">specify an absolute tolerance<\/a> to allow expected small differences between the two simulations.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/criteria.png\" alt=\"Equivalance Criteria\" \/><\/p>\n<p><strong>Running the Test<\/strong><\/p>\n<p>Now it's time to click the Run button:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/runTest.png\" alt=\"Run Test\" \/><\/p>\n<p>When the test terminates, we can inspect the results. The integration of the Simulation Data Inspector within the Test Manager makes it convenient to inspect the results without writing a single line of code. In our example, we can see the small difference within the specified tolerance.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/comparisonResult.png\" alt=\"Comparing Results\" \/><\/p>\n<p>Notice the <strong>Highlight in Model<\/strong> button. It is convenient to analyze failures in large models where many signals are logged.<\/p>\n<p><strong>What else?<\/strong><\/p>\n<p>I want to mention a few more items I find very useful in the Test Manager:<\/p>\n<ul>\n<li><strong>Debug:<\/strong> If you enable the Debug button, the Test Manager will setup everything and pause the simulation at t=0, allowing you to go step by step through the simulation to understand what is going wrong.<\/li>\n<li><strong>Parallel:<\/strong> With the Parallel Computing Toolbox, this button will run the tests in parallel, possibly saving you lots of time.<\/li>\n<li><strong>Time-Based Tolerances:<\/strong> When specifying the tolerances in the equivalence criteria, it is possible to specify leading and lagging tolerances. For example, if I am simulating a vehicle and testing in which gear the transmission is, I probably want to allow shifting to happen slightly before or after the baseline.\n<li><strong>Programmatic API:<\/strong> Once your tests are defined and saved, it is easy to run them programmatically. With just three lines of code, you can load the test file, run the tests, and view the results.<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/testAPI.png\" alt=\"Test API\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Let us know what you think of the Test Manager by leaving a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2017Q2\/testAPI.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Today, Ajay Puvvala is back to talk about testing.<br \/>\nRecapitulation<br \/>\nIn last week's post, we looked at how we could apply MATLAB Unit Testing Framework to Simulink context. We authored a scripted test... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/05\/25\/simulation-based-testing-with-simulink-test-manager\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,33,63,16],"tags":[496,425,497],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6543"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=6543"}],"version-history":[{"count":18,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6543\/revisions"}],"predecessor-version":[{"id":6591,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/6543\/revisions\/6591"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=6543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=6543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=6543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}