Main Content

Linking to a Test Script

In this workflow, you link a requirement to a MATLAB®script using theOutgoing Links Editorand the API. The verification status in theRequirements Editorreflects the test results. These illustrations follow the workflow for including external test results in the requirement verification status. For more information, seeInclude Results from External Sources in Verification Status.

Linking to a Test Script Using the Outgoing Links Editor

Create a requirement set calledcounter_req.slreqxin theRequirements Editorand 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, seeWork with Requirements in the Requirements Editor.

You have a MATLAB script calledrunmytests.mthat runs a test for theCounterclass inCounter.m. The test script contains custom methods that write results a TAP format to a file namedresults.tap. Assume that you have run the test and it has produced theresults.tapfile that contains the results of the test. You want to link the results of the test to a requirement incounter_req.slreqx. Follow these steps to create and view the verification status with a test case calledcounterStartsAtZeroinrunmytests.mscript:

Create the Register the Link Type

Open the template file atmatlabroot/toolbox/slrequirements/linktype_examples/linktype_TEMPLATE.m. Follow these steps:

  1. Create a new MATLAB file.

  2. Copy the contents oflinktype_TEMPLATEinto the new file. Save the file aslinktype_mymscripttap.m.

  3. Inlinktype_mymscripttap.m

    1. Replace the function namelinktype_TEMPLATEwithlinktype_mymscripttap.m.

    2. SetlinkType。标签as'MScript TAP Results'.

    3. SetlinkType。Extensionsas{'.M'}.

    4. Uncomment the command forGetResultFcnin order to use it inlinktype_mymscripttapand 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
      GetResultFcnuses the utilityslreq.verification.services.TAPto interpret the result files for verification. Seeslreq.verification.services.TAPfor more details. For more information aboutGetResultFcn, seeLinks and Link Types.

  4. Savelinktype_mymscripttap.m.

  5. 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:

  1. Open theRequirements Editorand, in thecounter_req.slreqxrequirement set, right-click the child requirement1.1and selectOpen Outgoing Links dialog.

  2. In the Outgoing Links Editor dialog box, in theRequirementstab, clickNew.

  3. Enter these details to establish the link:

    • Description:runmytestscounterStartsAtZero

    • Document Type:MScript TAP Results

    • Document:runmytests.m

    • Location:counterStartsAtZero

  4. ClickOK. The link is highlighted in theLinkssection of theRequirements Editor.

View the Verification Status

Update the verification status in theRequirements Editor. Click刷新to see the verification status for the requirements in theRequirements Editor. This shows the verification status for entire requirement set that passed or failed.

The requirements forcounterStartsAtZeroare 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 calledcounter_req.slreqxin theRequirements Editorand 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, seeWork with Requirements in the Requirements Editor.

You have a MATLAB script calledrunmytests.mthat runs a test forCounterclass inCounter.m. The test script contains custom methods that write results in a TAP format to a file namedresults.tap. Assume that you have run the test and it has produced theresults.tapfile that contains the results of the test. You want to link the results of the test to a requirement incounter_req.slreqx. Follow these steps to create and view the verification status with a test case calledcounterStartsAtZeroinrunmytests.mscript:

Create and Register the Link Type

Open the template file atmatlabroot/toolbox/slrequirements/linktype_examples/linktype_TEMPLATE.m. Follow these steps:

  1. Create a new MATLAB file.

  2. Copy the contents oflinktype_TEMPLATEinto the new file. Save the file aslinktype_mymscripttap.m.

  3. Inlinktype_mymscripttap.m:

    1. Replace the function namelinktype_TEMPLATEwithlinktype_mymscripttap.m.

    2. SetlinkType。标签as'MScript TAP Results'.

    3. SetlinkType。Extensionsas{'.M'}.

    4. Uncomment the command forGetResultFcnin order to use it inlinktype_mymscripttapand 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
      GetResultFcnuses the utilityslreq.verification.services.TAPto interpret the result files for verification. Seeslreq.verification.services.TAPfor more details. For more information aboutGetResultFcn, seeLinks and Link Types.

  4. Savelinktype_mymscripttap.m.

  5. 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:

  1. From the MATLAB command prompt, enter:

    externalSource.id = 'counterStartsAtZero'; externalSource.artifact = 'runmytests.m'; externalSource.domain = 'linktype_mymscripttap';

  2. Find the requirement related to the link by typing:

    requirement = reqSet.find('Type', 'Requirement', 'SID', 2);

  3. Create the link by entering:

    link = slreq.createLink(requirement, externalSource);
    This creates the link as test casecounterStartsAtZerofor the requirementSID. InRequirements Editor, the link appears in theLinks>Confirmed Bysection.

View the Verification Status

Update the verification status. At the MATLAB command prompt, type:

reqSet.updateVerificationStatus
Fetch the verification status for the requirement by entering :
status = reqSet.getVerificationStatus
This shows which of the requirements in the requirement set have passed or fail. Click on刷新 button to see the verification status for the requirements in theRequirements Editor.

The requirements forcounterStartsAtZeroare 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. TheXMLPluginclass creates a plugin that writes test results to an XML file. For more information, seematlab.unittest.plugins.XMLPlugin.producingJUnitFormat.

你可以注册域名,我创建的链接n the same way as with the test script. The verification status for a set of requirements is shown in theRequirements Editor.

Related Topics