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
folderA Microsoft® Word document that defines a test specification, located in the
documents
folderRequirement sets that contain test requirements imported from Microsoft Word and functional requirements, located in the
documents
folderMATLAB tests for the function, located in the
tests
folderLinks from the test requirements to the tests, located in the
tests
folderAdditional MATLAB functions, Microsoft Word documents, requirement sets, tests, and links in the
MyAdd
andMySubtract
subfolders
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.
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.
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.
In the drop-down list to the left of the Run button , select Manage Custom Test Suites.
In the Test Suite Manager, click New.
Set Name to
MyAddTests
.Specify the relative path of the
tests
folder in theMyAdd
project by setting Base Folders to../MyAdd/tests
.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 tests pass, but the statement coverage is not 100%.
Assess Quality Metrics
Assess the quality metrics for the project by opening the Code Quality Dashboard.
codeQualityDashboard
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 indicates that there is one warning in the source code, 100% function and decision coverage, 93.3% statement coverage, and all requirements are verified.