If your model includes instances of blocks from a library, you can test both the source block in the library, and individual block instances in other models. First, create test harnesses for a library block to test your design. Once the library block meets your requirements, create test harnesses for linked blocks and test the subsystem instances. You can move test harnesses from the library to an instance and an instance to the library.
This procedure outlines an example workflow for testing library subsystems and linked subsystems.
Create a test case and a test harness for the library subsystem.
Test the library subsystem. If it fails your requirements, revise the design and test again.
Lock the library when your tests pass.
In your model, create a linked subsystem and retain the library test harnesses.
Compare the output of the linked instance to that of the library block using an equivalence test case.
Create additional test cases and test harnesses for the linked instance.
Promote a test harness from the linked subsystem to the library if you want to include the test harness with future linked subsystems.
A test harness for a library subsystem has specific properties:
Libraries do not compile, so a test harness for a library subsystem does not use compiled attributes such as data type or sample rate.
A test harness for a library subsystem does not generate conversion subsystems for the block inputs and outputs.
A library subsystem test harness does not use push or rebuild operations, because libraries do not use configuration parameters.
When you create a linked subsystem from a library subsystem, test harnesses copy to the linked instance. If you do not need the test harnesses, you can delete them. For instructions on deleting all test harnesses from a model, see Manage Test Harnesses.
When you create a test harness for a linked subsystem, the harness associates with the
linked subsystem, not the library subsystem. You can move a test harness from a linked
subsystem to the library subsystem. For example, this linked subsystem
Controller has three test harnesses. To move the
Requirements_Tests1 test harness to the library:
Click the harness badge on the linked subsystem.
Click the Harness Operations icon.
Select Move to Library.
A dialog box informs you that moving the harness removes it from the linked subsystem.
After confirmation, the harness appears with the library subsystem.
You can apply an iterative design and test workflow to libraries by testing a library block in a test harness and updating the component under test. Changes to the component under test synchronize to the library when you close the test harness.
If you have a library block whose design is complete, set your test harnesses to prevent changes to the component under test. You can set this property when you create the test harness or after harness creation. See Create Test Harnesses and Select Properties.
Verify a reusable subsystem in a library and in a larger system.
This example demonstrates a test case that confirms a library block meets a short set of requirements. After testing the library block, you execute a baseline test of a linked block and capture the baseline results. You then promote the baseline test harness to the library.
The library block controls a simple heatpump system by supplying on/off signals to a fan and compressor, and specifying the heatpump mode (heating or cooling).
Open the Test File
Enter the following to store paths and filenames for the example, and to open the test file. The test file contains a test case for the library block and for the block instance in a closed-loop model.
filePath = fullfile(matlabroot,'toolbox','simulinktest','simulinktestdemos'); testFile = 'sltestHeatpumpLibraryTests.mldatx'; library = 'sltestHeatpumpLibraryExample'; system = 'sltestHeatpumpLibraryLinkExample'; open(fullfile(filePath,testFile));
Expand the Library Block Test test suite, and highlight the Requirements Scenarios test case in the test browser. Expand the Test Harness section of System Under Test, and click the arrow to open the test harness for the library block.
open_system(library); sltest.harness.open([library '/Controller'],'Requirements_Tests');
The Test Sequence block sets three scenarios for the controller:
The controller at idle
The controller activating the fan only
The controller activating the heating anc cooling system
The Test Assessment block in the test harness checks the signals for each scenario. Since the test inputs and assessments are contained in the test harness, and no baseline data is being captured, the test case is a simulation test.
Run the Requirements-Based Test
In the test manager, run the Requirements Scenarios test case. The
verify statement results show that the
control_out signals pass.
Open the Linked Block Model
In the test manager, expand Instance Test. Highlight the Baseline Test test case. In the System Under Test, click the arrow next to the Model field to open the model.
sltest.harness.close([library '/Controller'], 'Requirements_Tests'); open_system(system); sim(system);
The controller is a linked block to the library. It is associated with a test harness Baseline Test that compares simulation results of the instance against baseline data. In your workflow, successful baseline testing for an instances of a library block can show that the linked block simulates correctly in the containing model. The test harness supplies a sine wave temperature and captures the controller output.
Run the Baseline Test and Observe Results
In the test manager, click Run to execute the test. The results show that the baseline test passes.
Move the Test Harness to the Library
If you develop a particularly useful test for a linked block, you can promote the test harness from a linked block to the source library block. The test harness then copies to all future instances of the library block.
Move the Baseline_controller_tests test harness to the library block:
1. In the sltestHeatpumpLibraryLinkExample model, click the harness badge and hover over the Baseline_controller_tests test harness.
2. Click the harness operations icon
3. Select Move to Library. A dialog informs you that the operation deletes the test harness from the instance and adds it to the library. Click Yes.
4. The test harness moves to the Controller library block.
close_system(library,0); close_system(system,0); clear(filePath,library,system,testFile); sltest.testmanager.clear; sltest.testmanager.clearResults;