www.gusucode.com > simulinktest 案例源码程序 matlab代码 > simulinktest/TestCaseMATLABUnitTestExample.m
%% Test a Model for Continuous Integration Systems % This example shows how to use MATLAB(R) Unit Test to % test a model, and use the TAPPlugin to create TAP results. You can use % TAP with CI systems. The model is a controller-plant system of a flight % controller, aircraft model, and environment model. % % Create a test suite and a test runner, and % customize the runner with the plugin that creates the % TAP file. When you run the test, it fails on several % iterations. The results are written to the TAP file. % % Before performing this example, set the working directory to a writable % location on the path. %% % 1. Open the Model open_system(fullfile(matlabroot,'examples','simulinktest','sltestF14ParameterSweep.slx')) %% % 2. Open the Test File % % The test case creates a square wave input to the controller, and sweeps % through 25 iterations of the parameters |a| and % |b|. It compares the |alpha| output to a baseline with a tolerance of % |0.0046| and fails any output which exceeds this tolerance. sltest.testmanager.view; sltest.testmanager.load(fullfile(matlabroot,'examples','simulinktest',... 'f14ParameterSweepTest.mldatx')); %% % 3. Import the |TestRunner|, |TestSuite|, |TAPPlugin|, and |ToFile| % classes. import matlab.unittest.TestRunner import matlab.unittest.TestSuite import matlab.unittest.plugins.TAPPlugin import matlab.unittest.plugins.ToFile %% % 4. Create the test suite object. suite = testsuite(fullfile(matlabroot,'examples','simulinktest',... 'f14ParameterSweepTest.mldatx')) %% % 5. Create the test runner object, and set it to display output to the % command window. f14runner = TestRunner.withTextOutput; %% % 6. Create a TAP plugin that sends output to the file |F14TapOutput.tap|. tapFile = 'F14TapOutput.tap'; plugin = TAPPlugin.producingVersion13(ToFile(tapFile)); %% % 7. Add the plugin to the test runner. addPlugin(f14runner,plugin) %% % 8. Run the test. The test fails several iterations in which % the delta between the signal output and the baseline exceeds the % tolerance. result = run(f14runner,suite); %% % 9. Display the results from the TAP file. disp(fileread(tapFile)) %% sltest.testmanager.clearResults sltest.testmanager.clear sltest.testmanager.close %% close_system('sltestF14ParameterSweep',0)