Main Content

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 Requirements Editor displays the counter_req requirement set, which has one parent requirement and three child requirements.

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:

  1. Open the myCustomDocInterface.m file by executing this code at the MATLAB command line:

    open(fullfile(matlabroot,"toolbox","slrequirements","slrequirements","linktype_examples","myCustomDocInterface.m"))

  2. Replace the function name myCustomDocInterface with linktype_mymscripttap.

  3. 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 as linktype_mymscripttap.m

  4. Set docInterface.Label as 'MScript TAP Results'.

  5. Set docInterface.Extensions as {'.M'}.

  6. 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 utility slreq.verification.services.TAP to interpret the result files for verification. For more information about GetResultFcn, see Define Custom Document Interface for Direct Linking to Requirements.

  7. Save linktype_mymscripttap.m.

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

  1. Open the Requirements Editor and, in the counter_req.slreqx requirement set, right-click the child requirement 1.1 and select Open Outgoing Links dialog.

  2. In the Outgoing Links Editor dialog box, in the Requirements tab, click New.

  3. Enter these details to establish the link:

    • Description: runmytestscounterStartsAtZero

    • Document Type: MScript TAP Results

    • Document: runmytests.m

    • Location: counterStartsAtZero

  4. Click OK. The link is highlighted in the Links section of the Requirements Editor.

    The Outgoing Links Editor shows the new link with the previously described properties.

Linking to a Test Script Using the API

Create the link from a requirement to the test script by using the API:

  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 case counterStartsAtZero for the requirement with SID set to 2. In Requirements Editor, the link appears in the Links > Confirmed By section.

    The link is shown in the Requirements Editor.

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 Requirements Editor shows the requirement set with 3 requirements and one is verified.

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.

See Also

Apps

Classes

Functions

Related Examples

More About