Main Content

matlabtest.coder.MEXReplacementTestSuite Class

Namespace: matlabtest.coder

Verify generated C/C++ code by using MEX replacement test suites

Since R2025a

Description

Use the matlabtest.coder.MEXReplacementTestSuite class to verify generated C/C++ code by creating and running MEX replacement test suites. These test suites replace calls to MATLAB® functions with calls to MEX functions in existing MATLAB tests. The test contains the tests as matlab.unittest.Test objects. You can only run the tests programmatically.

Running tests in matlabtest.coder.MEXReplacementTestSuite test suites is similar to running tests by using the coder.runTest (MATLAB Coder) function, but the tests run using the matlab.unittest framework and store the results in matlab.unittest.TestResult objects.

The matlabtest.coder.MEXReplacementTestSuite class is a handle class.

Creation

Create instances of matlabtest.coder.MEXReplacementTestSuite by using one of the public methods.

Methods

expand all

Examples

collapse all

Create MEX replacement test suites from a test file by using the matlabtest.coder.MEXReplacementTestSuite 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 and matlab.unittest.selectors.HasTag classes.

import matlabtest.coder.MEXReplacementTestSuite
import matlab.unittest.selectors.HasTag

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

testFile = "tests/graph_unit_tests.m";
mexFcn = which("shortest_path_mex");
suite1 = MEXReplacementTestSuite.fromFile(testFile,mexFcn);
disp({suite1.Name}')
    {'graph_unit_tests/check_longest_path'            }
    {'graph_unit_tests/check_unity_path'              }
    {'graph_unit_tests/check_non_unique'              }
    {'graph_unit_tests/check_no_path'                 }
    {'graph_unit_tests/check_start_end_same'          }
    {'graph_unit_tests/check_invalid_idx_empty_adj'   }
    {'graph_unit_tests/check_edgeless_graph'          }
    {'graph_unit_tests/check_edgeless_start'          }
    {'graph_unit_tests/check_edgeless_end'            }
    {'graph_unit_tests/check_edgeless_graph_self_loop'}
    {'graph_unit_tests/check_invalid_start_1'         }
    {'graph_unit_tests/check_invalid_start_2'         }
    {'graph_unit_tests/check_invalid_end_1'           }
    {'graph_unit_tests/check_invalid_end_2'           }

Create a test suite that includes only the tests that have the tag InputTests in the graph_unit_tests.m test file.

selector = HasTag("InputTests");
suite2 = MEXReplacementTestSuite.fromFile( ...
    testFile,mexFcn,selector);
disp({suite2.Name}')
    {'graph_unit_tests/check_invalid_start_1'}
    {'graph_unit_tests/check_invalid_start_2'}
    {'graph_unit_tests/check_invalid_end_1'  }
    {'graph_unit_tests/check_invalid_end_2'  }

Create a test suite that includes only the tests whose name contains edgeless from the graph_unit_tests.m file.

suite3 = MEXReplacementTestSuite.fromFile( ...
    testFile,mexFcn,Name="*edgeless*");
disp({suite3.Name}')
    {'graph_unit_tests/check_edgeless_graph'          }
    {'graph_unit_tests/check_edgeless_start'          }
    {'graph_unit_tests/check_edgeless_end'            }
    {'graph_unit_tests/check_edgeless_graph_self_loop'}

Run the tests.

results = run([suite1 suite2 suite3]);
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