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.

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:

  1. Create a new MATLAB file.

  2. Copy the contents of linktype_TEMPLATE into the new file. Save the file as linktype_mymscripttap.m.

  3. In linktype_mymscripttap.m

    1. Replace the function name linktype_TEMPLATE with linktype_mymscripttap.m.

    2. Set linkType.Label as 'MScript TAP Results'.

    3. Set linkType.Extensions as {'.M'}.

    4. 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.

  4. Save linktype_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 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.

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:

  1. Create a new MATLAB file.

  2. Copy the contents of linktype_TEMPLATE into the new file. Save the file as linktype_mymscripttap.m.

  3. In linktype_mymscripttap.m:

    1. Replace the function name linktype_TEMPLATE with linktype_mymscripttap.m.

    2. Set linkType.Label as 'MScript TAP Results'.

    3. Set linkType.Extensions as {'.M'}.

    4. 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.

  4. Save linktype_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 case counterStartsAtZero for the requirement SID. 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
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 Refresh button to see the verification status for the requirements in the Requirements Editor.

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.

Related Topics