Main Content

Run Tests and Assess Quality in Referenced Projects

Separating a large project into referenced projects is called componentization. This example shows how to componentize a project that contains MATLAB® algorithms and requirements, and then run the tests and assess the quality metrics on those referenced projects.

Open the MyMath project.

openProject("MyMath");

The projects contain:

  • A MATLAB function, located in the source folder

  • A Microsoft® Word document that defines a test specification, located in the documents folder

  • Requirement sets that contain test requirements imported from Microsoft Word and functional requirements, located in the documents folder

  • MATLAB tests for the function, located in the tests folder

  • Links from the test requirements to the tests, located in the tests folder

  • Additional MATLAB functions, Microsoft Word documents, requirement sets, tests, and links in the MyAdd and MySubtract subfolders

The Project pane shows the MyMath project and its folders.

The myMath function takes two numeric inputs and an operation input that allows the user to specify which math operation to use. The function uses the myAdd and mySubtract functions for addition and subtraction. Examine the myMath function.

type myMath
function y = myMath(u,v,operation)
% myMath - Adds or subtracts inputs u and v depending on the operation
% specified

switch operation
    case "add"
        y = myAdd(u,v);
    case "subtract"
        y = mySubtract(u,v);
    otherwise
        error("Specify the operation as either add or subtract.")
        y = [];
end
end

Componentize the Project

The MyAdd and MySubtract folders contain the myAdd and mySubtract functions, as well as requirements, the Microsoft Word test specifications, tests, and links for those functions.

The Project pane shows the MyMath project with the MyAdd and MySubtract folders expanded to show the many files that they contain.

Componentize the project by extracting the MyAdd and MySubtract folders into referenced projects. Right-click the MyAdd folder and select Extract to Referenced Project.

In the Extract to Referenced Project dialog box, set New Project Name to MyAdd, then click Extract. In the Create Project Folder dialog box, click Yes. MATLAB opens the References view, which displays the referenced projects for the MyMath project.

The References view of the Project pane shows that the MyMath project references the MyAdd project.

Select the Files view, then extract the MySubtract folder to a referenced project. Name the new project MySubtract.

The MyAdd and MySubtract folders are removed from the project, but remain in the MyMath folder. Remove the MyAdd and MySubtract folders. In the Current Folder pane, right-click the MyAdd folder and select Delete. Repeat this process for the MySubtract folder.

Create Project-Based Test Suites

Open the MATLAB Test Manager.

matlabTestManager

The test manager includes the tests from the referenced projects. Organize the tests by creating test suites for each individual project, starting with the MyAdd project.

  1. In the drop-down list to the left of the Run button , select Manage Custom Test Suites.

  2. In the Test Suite Manager, click New.

  3. Set Name to MyAddTests.

  4. Specify the relative path of the tests folder in the MyAdd project by setting Base Folders to ../MyAdd/tests.

  5. Click Save.

Create additional test suites for the tests from the MySubtract and MyMath projects. To create the test suite that contains the tests in the MySubtract project, set Base Folders to ../MySubtract/tests. To create the test suite that contains the tests in the MyMath project, set Base Folders to tests.

Verify the Algorithms

Verify the algorithms by running the tests with coverage enabled. Click the Code Coverage button , then select Enable Code Coverage and set the metric level to MC/DC. In the drop-down list to the left of the Run button , select the MyMathTests test suite, then run the tests by clicking the Run button .

The MATLAB Test Manager displays the MyMathTests test suite with 15 passed tests and no failed, incomplete, and not run tests.

The tests pass.

Assess Quality Metrics

Assess the quality metrics for the project by opening the Code Quality Dashboard.

codeQualityDashboard

The Code Quality Dashboard displays the code analysis, test, code coverage, and requirement metrics.

The Code Quality Dashboard indicates that there is one warning in the source code, 42.9% of the tests pass, and 57.1% of the tests were not run. Similarly, the passed tests verified 28.6% of the requirements, and 71.4% of the requirements link to tests that have not run. There is no code coverage data because not all of the tests in the project ran.

Verify the remaining algorithms by clicking the Run button , which runs all tests in the project.

The Code Quality Dashboard shows the updated metrics.

The Code Quality Dashboard indicates that there is one warning in the source code, 100% function and decision coverage, 93.3% statement coverage, and all requirements are verified.

See Also

Apps

Related Examples

More About