Linking to a Test Script
In this workflow, you link a requirement to a MATLAB® script using the Outgoing Links Editor and the API. The verification status in the Requirements Editor reflects the test results. These illustrations follow the workflow for including external test results in the requirement verification status. For more information, see Include Results from External Sources in Verification Status.
Linking to a Test Script Using the Outgoing Links Editor
Create a requirement set called counter_req.slreqx
in the
Requirements Editor and save it in a writable location. This requirement
set has child requirements that have requirement IDs and descriptions.
You have a MATLAB script called runmytests.m
that runs a test for the
Counter
class in Counter.m
. The test script
contains custom methods that write results a TAP format to a file named
results.tap
. Assume that you have run the test and it has
produced the results.tap
file that contains the results of the test.
You want to link the results of the test to a requirement in
counter_req.slreqx
. Follow these steps to create and view the
verification status with a test case called counterStartsAtZero
in
runmytests.m
script:
Create the Register the Link Type
Open the template file at
matlabroot/toolbox/slrequirements/linktype_examples/linktype_TEMPLATE.m
.
Follow these steps:
Create a new MATLAB file.
Copy the contents of
linktype_TEMPLATE
into the new file. Save the file aslinktype_mymscripttap.m
.In
linktype_mymscripttap.m
Replace the function name
linktype_TEMPLATE
withlinktype_mymscripttap.m
.Set
linkType.Label
as'MScript TAP Results'
.Set
linkType.Extensions
as{'.M'}
.Uncomment the command for
GetResultFcn
in order to use it inlinktype_mymscripttap
and enter:linktype.GetResultFcn = @GetResultFcn; ......... function result = GetResultFcn(link) testID = link.destination.id; testFile = link.destination.artifact; resultFile = getResultFile(testFile); if ~isempty(resultFile) && isfile(resultFile) tapService = slreq.verification.services.TAP(); result = tapService.getResult(testID, resultFile); else result.status = slreq.verification.Status.Unknown; end end function resultfile = getResultFile(testFile) resultMap = ["runmytests.m", "results.tap";... "othertests.m", "results2.tap"]; resultfile = resultMap(resultMap(:,1) == testFile,2); end
GetResultFcn
uses the utilityslreq.verification.services.TAP
to interpret the result files for verification. Seeslreq.verification.services.TAP
for more details. For more information aboutGetResultFcn
, see Links and Link Types.
Save
linktype_mymscripttap.m
.Register the link type. At the command line, enter:
rmi register linktype_mymscripttap
Note
If the command returns a warning, then you must unregister the file and follow step 5 again. Unregister the file by entering:
rmi unregister linktype_mymscripttap
Create the Link
Follow these steps to add the link manually in the Outgoing Links Editor:
Open the Requirements Editor and, in the
counter_req.slreqx
requirement set, right-click the child requirement1.1
and select Open Outgoing Links dialog.In the Outgoing Links Editor dialog box, in the Requirements tab, click New.
Enter these details to establish the link:
Description:
runmytestscounterStartsAtZero
Document Type:
MScript TAP Results
Document:
runmytests.m
Location:
counterStartsAtZero
Click OK. The link is highlighted in the Links section of the Requirements Editor.
View the Verification Status
Update the verification status in the Requirements Editor. Click
Refresh to see the verification status for the requirements in
the Requirements Editor. This shows the verification status for entire
requirement set that passed or failed.
The requirements for counterStartsAtZero
are fully verified.
Here, the verification status shows that out of three tests, one test passed.
Linking to a Test Script Using the API
Create a requirement set called counter_req.slreqx
in the
Requirements Editor and save it in a writable location. This requirement
set has child requirements that have requirement IDs and descriptions.
You have a MATLAB script called runmytests.m
that runs a test for
Counter
class in Counter.m
. The test script
contains custom methods that write results in a TAP format to a file named
results.tap
. Assume that you have run the test and it has
produced the results.tap
file that contains the results of the test.
You want to link the results of the test to a requirement in
counter_req.slreqx
. Follow these steps to create and view the
verification status with a test case called counterStartsAtZero
in
runmytests.m
script:
Create and Register the Link Type
Open the template file at
matlabroot/toolbox/slrequirements/linktype_examples/linktype_TEMPLATE.m
.
Follow these steps:
Create a new MATLAB file.
Copy the contents of
linktype_TEMPLATE
into the new file. Save the file aslinktype_mymscripttap.m
.In
linktype_mymscripttap.m
:Replace the function name
linktype_TEMPLATE
withlinktype_mymscripttap.m
.Set
linkType.Label
as'MScript TAP Results'
.Set
linkType.Extensions
as{'.M'}
.Uncomment the command for
GetResultFcn
in order to use it inlinktype_mymscripttap
and enter:linktype.GetResultFcn = @GetResultFcn; ......... function result = GetResultFcn(link) testID = link.destination.id; testFile = link.destination.artifact; resultFile = getResultFile(testFile); if ~isempty(resultFile) && isfile(resultFile) tapService = slreq.verification.services.TAP(); result = tapService.getResult(testID, resultFile); else result.status = slreq.verification.Status.Unknown; end end function resultfile = getResultFile(testFile) resultMap = ["runmytests.m", "results.tap";... "othertests.m", "results2.tap"]; resultfile = resultMap(resultMap(:,1) == testFile,2); end
GetResultFcn
uses the utilityslreq.verification.services.TAP
to interpret the result files for verification. Seeslreq.verification.services.TAP
for more details. For more information aboutGetResultFcn
, see Links and Link Types.
Save
linktype_mymscripttap.m
.Register the link type. At the command line, enter:
rmi register linktype_mymscripttap
Note
If the command returns a warning, then you must unregister the file and follow step 5 again. Unregister the file by entering:
rmi unregister linktype_mymscripttap
Create the Link
Follow these steps to create the link:
From the MATLAB command prompt, enter:
externalSource.id = 'counterStartsAtZero'; externalSource.artifact = 'runmytests.m'; externalSource.domain = 'linktype_mymscripttap';
Find the requirement related to the link by typing:
requirement = reqSet.find('Type', 'Requirement', 'SID', 2);
Create the link by entering:
This creates the link as test caselink = slreq.createLink(requirement, externalSource);
counterStartsAtZero
for the requirementSID
. In Requirements Editor, the link appears in the Links > Confirmed By section.
View the Verification Status
Update the verification status. At the MATLAB command prompt, type:
reqSet.updateVerificationStatus
status = reqSet.getVerificationStatus

The requirements for counterStartsAtZero
are fully verified.
Here, the verification status shows that out of three tests, one test passed.
Integrating Results from a MATLAB Unit Test Case
You can also integrate the results from a MATLAB Unit Test case by linking to a test script. The test is run with a
customized test runner using a XML plugin that produces a JUnit output. The
XMLPlugin
class creates a plugin that writes test results to an
XML file. For more information, see matlab.unittest.plugins.XMLPlugin.producingJUnitFormat
.
You can register the domain and create the links in the same way as with the test script. The verification status for a set of requirements is shown in the Requirements Editor.