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. For an example, see C Code Verification with Simulink Test.
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. Select Analysis > Test Harness > Push Component and Parameters to Main
Model, 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, in the Advanced Properties, select Update Configuration Parameters and Model Workspace data on rebuild.
For existing test harnesses, in the harness preview, click the Harness operations icon to open the harness properties. In the harness properties, select Update Configuration Parameters and Model Workspace data on rebuild.
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.
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.
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
+1 for cooling and
-1 for heating.
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|
Open the Test Manager by selecting Analysis > Test Manager from the Simulink menu.
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.
In the model, right-click the
Controller_sfcn subsystem and
select Test Harness > Create for
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_1 for the
Create inputs in the test harness, with a constant
Tset and a time-varying
Connect a Constant block to
Tset input and set the
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:
Select Interpret vector parameters as 1–D.
In the Solver
pane of the Simulink toolstrip, set Stop
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. 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.