Main Content

matlabtest.coder.MEXReplacementTestSuite.fromName

Class: matlabtest.coder.MEXReplacementTestSuite
Namespace: matlabtest.coder

Create MEX replacement test suite from test name

Since R2025a

Description

suite = matlabtest.coder.MEXReplacementTestSuite.fromName(testName,mexFcn) creates a MEX replacement test suite from the test, testName. The tests call the MEX function specified by mexFcn.

example

suite = matlabtest.coder.MEXReplacementTestSuite.fromName(___,Name=Value) specifies options using one or more name-value arguments in addition to the input argument combinations in previous syntaxes.

example

Input Arguments

expand all

Test name, specified as a string scalar or character vector. The name of a test uniquely identifies the smallest runnable portion of the test content. The test name includes the namespace name, filename excluding the extension, procedure name, and information about parameterization.

The testName argument corresponds to the Name property of the Test object.

Example: "myTestClass/myTestProcedure"

MEX function to call in tests, specified as a string scalar, character vector, string array, cell array of character vectors, or cell array of string scalars. Specify the relative or full path to the MEX function. You do not need to include the file extension.

Example: "myMexFcn"

Example: "myMexFcn.mexw64"

Example: ["myMexFcn1","myMexFcn2"]

Example: {'myMexFcn1','myMexFcn2'}

Selector, specified as a matlab.unittest.selectors.Selector object.

Example: matlab.unittest.selectors.HasTag("SystemTests")

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: suite = matlabtest.coder.MEXReplacemetTestSuite.fromFile("myTestFile.m","myMexFcn",ProcedureName="myTest")

Names of files and folders to find dependent tests for, specified as a string vector, character vector, or cell vector of character vectors. This argument filters the test suite by including only the tests that depend on the specified files and folders. If none of the tests depend on the files and folders, the function returns as empty test suite.

The specified value must represent at least one existing file. If you specify a folder, the framework extracts the paths to the files within the folder.

You must have a MATLAB® Test™ license to use DependsOn. For more information about selecting tests by source code dependency, see matlabtest.selectors.DependsOn.

Example: DependsOn=["myFile.m" "myFolder"]

Example: DependsOn=["folderA" "C:\work\folderB"]

External parameters to use in the tests, specified as an array of matlab.unittest.parameters.Parameter objects. Use this argument to specify external parameters outside of the parameterized test.. For more information, see Use External Parameters in Parameterized Test.

Output Arguments

expand all

MEX replacement test suite, returned as an array of matlab.unittest.Test objects.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Create MEX replacement test suites by using the matlabtest.coder.MEXReplacementTestSuite.fromName static method.

Open the ShortestPath project and generate a MEX function from the shortest_path function with the required input types and sizes.

openExample("matlabtest/ShortestPathExample")
cfg = coder.config("mex");
mtxType = coder.typeof(ones(100,100),[],1);
scalarDbl = coder.typeof(1);
codegen shortest_path -args {mtxType,scalarDbl,scalarDbl} -config cfg
Code generation successful.

Import the matlabtest.coder.MEXReplacementTestSuite class.

import matlabtest.coder.MEXReplacementTestSuite

Create a MEX replacement test suite from the check_invalid_start_1 test in the graph_unit_tests test class. Specify the MEX file to call in the test, shortest_path_mex.

testName = "graph_unit_tests/check_invalid_start_1";
mexFcn = which("shortest_path_mex");
suite1 = MEXReplacementTestSuite.fromName(testName,mexFcn);
disp({suite1.Name}')
    {'graph_unit_tests/check_invalid_start_1'}

Create a MEX replacement test suite from the check_invalid_start_1 test in the graph_unit_tests test class that includes only the tests that depend on the shortest_path.m file in the src folder.

suite2 = MEXReplacementTestSuite.fromName( ...
    testName,mexFcn,DependsOn="src/shortest_path.m");
disp({suite2.Name}')
    {'graph_unit_tests/check_invalid_start_1'}

Run the tests.

results = run([suite1 suite2]);
Setting up MEXReplacementFixture
Done setting up MEXReplacementFixture: Replacing MATLAB function calls with the specified MEX functions
__________

Running graph_unit_tests
.......... ....
Done graph_unit_tests
__________

Alternatives

You can also use the coder.runTest (MATLAB Coder) function to run existing tests that replace calls to MATLAB functions with calls to MEX functions. However, the matlabtest.coder.MEXReplacementTestSuite class provides these advantages over the coder.runTest (MATLAB Coder) function:

  • You can run tests from multiple test files.

  • You can collect coverage for the generated C/C++ code.

  • You can generate test and coverage reports from the results.

  • You can run MEX replacement test suites on CI platforms.

  • You can run MEX replacement test suites and MATLAB test suites in the same run by executing a single command.

For more information, see Verify Generated C/C++ Code by Running Existing MATLAB Tests.

Version History

Introduced in R2025a