Test Integrated Code
Test Integrated C Code
If you have a model that integrates C code with a C Caller block, you can test the C code with the Test Manager and a test harness.
The C Caller block uses configuration parameters to define the custom code. If you change the configuration parameters, synchronize the parameters between the test harness and the model. For more information, see Synchronize Changes Between Test Harness and Model and Create Test Harnesses and Select Properties.
If you change the test harness configuration parameters, you can push the configuration set to the main model. Click Push Changes, or use
If you change the main model configuration parameters in the main model, and you want to update the test harness parameters, the test harness must copy the configuration parameters on rebuild. You can set this property in two ways:
When you create the test harness, select Update Configuration Parameters and Model Workspace data on rebuild. You can also select Rebuild Harness on Open, which rebuilds every time the harness opens.
For existing test harnesses, in the harness preview, select one or more of Rebuild Harness > Rebuild on Open, , or Rebuild Harness without Compiling Model, and Update Harness Configuration Settings and Model Workspace. The Update Harness Configuration Settings and Model Workspace option updates the settings every time a rebuild occurs.
Test Code in S-Functions
S-Functions are computer language descriptions of Simulink® blocks written in MATLAB®, C, C++ or Fortran. You can test code wrapped in S-Functions using Simulink Test™ test harnesses. Testing code in S-Functions can be helpful for regression testing of legacy code and for testing your code in a system context.
S-Function Testing Example
In this example, you test code in an S-Function block using a test harness. The main model is a controller-plant model of an air conditioning/heat pump unit. Before you begin, change the default working folder to one with write permissions.
This example works only on a 64–bit Windows® platform.
Set Up the Working Environment
Add the example folder to the MATLAB path, and set the example file names.
ep = fullfile(docroot,'toolbox','sltest','examples'); addpath(ep); md = 'sltestHeatpumpSfunExample.slx' cb = 'sltestHeatpumpBusPostLoadFcn.mat' dt = 'PumpDirection.m'
Open the model.
In the example model:
The controller is an S-Function that accepts room temperature and specified temperature inputs.
The controller output is a bus with signals that control the fan, heat pump, and the direction of the heat pump (heat or cool).
The plant accepts the control bus. The heat pump and the fan signals are Boolean, and the heat pump direction is specified by
+1for cooling and
The test covers four temperature conditions. Each condition corresponds to one operating state with fan, pump, and pump direction signal outputs.
|Temperature Condition||System State||Fan Command||Pump Command||Pump Direction|
Create a Test Case
On the Apps tab, under Model Verification, Validation, and Test, click Simulink Test. Then, on the Tests tab, click Simulink Test Manager.
From the Test Manager toolstrip, click New to create a test file. Name and save the test file.
In the test case, under System Under Test, click the button to load the current model into the test case.
Create a Test Harness
In the model, right-click the
Controller_sfcnsubsystem and select Test Harness > Create for ‘Controller_sfcn’.
Set the harness properties.
In the Basic Properties tab:
Set Name to
Set Sources and Sinks to None and Scope
Click OK to create the test harness.
In the test case, under System Under Test, refresh the test harness list and select
test_harness_1for the Harness.
Add Inputs and Set Simulation Parameters
Create inputs in the test harness, with a constant
Tset and a time-varying
Connect a Constant block to the
Tsetinput and set the value to
Add a Sine Wave block to the harness model to simulate a temperature signal. Connect the Sine Wave block to the conversion subsystem input
Double-click the Sine Wave block and set the parameters:
Parameter Value Amplitude 15 Bias 75 Frequency 2*pi/3600 Phase (rad) 0 Sample time 1
Select Interpret vector parameters as 1–D.
In the Solver pane of the Simulink toolstrip, set Stop time to
Obtain Baseline Data
In the test case, in Simulation Outputs, click Add. Highlight the output bus from the controller S-Function.
In the Signal Selection dialog box, click the Add button.
Under Baseline Criteria, click Capture to record a baseline data set from simulating the test harness. Save the baseline data set to the working folder. The baseline signals appear in the table.
Run the Test Case and View Results
Run the test case. The test results appear in the Results and Artifacts pane.
Expand the results to view the baseline criteria result. The baseline test passes because the simulation output is identical to the baseline data.