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.
Open the Example Files
Open the Integrating Results from a Custom-Authored MATLAB Script as a Test example.
openExample(['slrequirements/' ... 'IntegratingResultsFromACustomAuthoredMATLABScriptAsATestExample'])
Open the counter_req
requirement set in the Requirements
Editor. This requirement set has child requirements that have requirement IDs
and descriptions.
The MATLAB code file runmytests.m
runs a test for the
Counter
class in Counter.m
. The
runmytests.m
file contains custom methods that write test results
in TAP format to a file named results.tap
. In this example, you
create links between requirements in the counter_req
requirement set
and test cases in runmytests.m
by using the Outgoing Links Editor and
the Requirements Toolbox™ API.
Create and Register the Custom Document Interface
Before creating links to the test script, create and register the custom document interface to enable linking to and getting results from the test script:
Open the
myCustomDocInterface.m
file by executing this code at the MATLAB command line:open(fullfile(matlabroot,"toolbox","slrequirements","slrequirements","linktype_examples","myCustomDocInterface.m"))
Replace the function name
myCustomDocInterface
withlinktype_mymscripttap
.Save a copy of the
myCustomDocInterface.m
in the current folder. In MATLAB, in the Editor tab, click Save > Save Copy As. Save the file aslinktype_mymscripttap.m
Set
docInterface.Label
as'MScript TAP Results'
.Set
docInterface.Extensions
as{'.M'}
.Replace the existing
GetResultFcn
with this function: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. For more information aboutGetResultFcn
, see Define Custom Document Interface for Direct Linking to Requirements.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
Link Requirements to Test Cases in Test Script
You can create a link from a requirement to the test script that generates test results to confirm the requirement. You can create the link by using the Outgoing Links Editor, or by using the Requirements Toolbox API.
Create a Link by Using the Outgoing Links Editor
Create the link from a requirement to the test script by using 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.
Linking to a Test Script Using the API
Create the link from a requirement to the test script by using the API:
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 requirement withSID
set to2
. In Requirements Editor, the link appears in the Links > Confirmed By section.
View Verification Status
Update the verification information for the counterStartsAtZero
test case based on the Excel® status log by updating the verification status for the requirement
set.
You can update the verification status in the Requirements Editor by clicking Refresh . Ensure that Columns + > Verification Status is selected to view the verification status for entire requirement set.
The verification status shows that one of the three requirements is verified.
You can also update the verification status and fetch the current status by entering the following at the MATLAB command prompt:
updateVerificationStatus(reqSet) status = getVerificationStatus(reqSet)
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.