In this example, 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 examples follow the workflow for including external test results in the requirement verification summary. For more information, see Include Results from External Sources in Verification Status.
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. For more details on
how to create requirement sets, see Work with Requirements in the Simulink Editor.
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:
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 as
linktype_mymscripttap.m
.
In linktype_mymscripttap.m
Replace the function name
linktype_TEMPLATE
with
linktype_mymscripttap.m
.
Set linkType.Label
as 'MScript
TAP Results'
.
Set linkType.Extensions
as
{'.M'}
.
Uncomment the command for GetResultFcn
in order to use it in
linktype_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 utility slreq.verification.services.TAP
to interpret the result files for verification. See slreq.verification.services.TAP
for more details. For more information about
GetResultFcn
, 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
Follow these steps to add the link manually in the Outgoing Links Editor:
Open the Requirements Editor and, in the
counter_req.slreqx
requirements set, right-click
on the child requirement 1.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.
Update the verification status in the Requirements Editor. Click on
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.
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. For more details on
how to create requirement sets, see Work with Requirements in the Simulink Editor.
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:
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 as
linktype_mymscripttap.m
.
In linktype_mymscripttap.m
:
Replace the function name
linktype_TEMPLATE
with
linktype_mymscripttap.m
.
Set linkType.Label
as 'MScript
TAP Results'
.
Set linkType.Extensions
as
{'.M'}
.
Uncomment the command for GetResultFcn
in order to use it in
linktype_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 utility slreq.verification.services.TAP
to interpret the result files for verification. See slreq.verification.services.TAP
for more details. For more information about
GetResultFcn
, 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
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:
link = slreq.createLink(requirement, externalSource);
counterStartsAtZero
for
the requirement SID
. In Requirements Editor, the link
appears in the Links > Confirmed By section.
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.
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 Simulink® Requirements™ Editor.
Include Results from External Sources in Verification Status