Main Content

Compile MATLAB Unit Tests

When you write tests using the MATLAB® unit testing framework, you can create a standalone application (requires MATLAB Compiler™) to run your tests on target machines that do not have MATLAB installed:

MATLAB Compiler supports only class-based unit tests. (You cannot compile script-based or function-based unit tests.) In addition, MATLAB Compiler currently does not support tests authored using the performance testing framework.

Run Tests with Standalone Applications

This example shows how to create a standalone application from your unit tests and run the generated application from a terminal window (on a Microsoft® Windows® platform).

In a file named TestRand.m in your current folder, create a parameterized test class that tests the MATLAB random number generator (see TestRand Class Definition Summary).

In your current folder, create the runMyTests function. This function creates a test suite from the TestRand class, runs the tests, and displays the test results.

function runMyTests
suite = matlab.unittest.TestSuite.fromClass(?TestRand);
runner = matlab.unittest.TestRunner.withNoPlugins;
results =;

Compile the runMyTests function into a standalone application by running the mcc command in the Command Window. MATLAB Compiler generates an application in your current folder.

mcc -m runMyTests

Open a terminal window, navigate to the folder into which you packaged your standalone application, and run the application.

  1x1200 TestResult array with properties:


   1200 Passed, 0 Failed, 0 Incomplete.
   3.11 seconds testing time.

For more information on how to create and run standalone applications, see Create Standalone Application from MATLAB (MATLAB Compiler).

Run Tests in Parallel with Standalone Applications

Starting in R2020b, you can create standalone applications that support running tests in parallel (requires Parallel Computing Toolbox™). This feature requires you to use the directive %#function parallel.Pool in the file that triggers the test run. The %#function (MATLAB Compiler) pragma informs MATLAB Compiler that a parallel.Pool (Parallel Computing Toolbox) object must be included in the compilation to provide access to the parallel pool.

For example, consider the tests in the file TestRand.m. You can create a standalone application to run these tests in parallel by compiling this function.

function runMyTestsInParallel
%#function parallel.Pool
results = runtests('TestRand.m','UseParallel',true);

Compile the function into a standalone application using the mcc command. To instruct MATLAB Compiler to include the test file in the application, specify the file name using the -a option.

mcc -m runMyTestsInParallel -a TestRand.m

TestRand Class Definition Summary

This code provides the complete contents of the TestRand class.

classdef TestRand < matlab.unittest.TestCase    
    properties (TestParameter)
        dim1 = createDimensionSizes;
        dim2 = createDimensionSizes;
        dim3 = createDimensionSizes;
        type = {'single','double'};
    methods (Test)
        function testRepeatable(testCase,dim1,dim2,dim3)
            state = rng;
            firstRun = rand(dim1,dim2,dim3);
            secondRun = rand(dim1,dim2,dim3);
        function testClass(testCase,dim1,dim2,type)
function sizes = createDimensionSizes
% Create logarithmically spaced sizes up to 100
sizes = num2cell(round(logspace(0,2,10)));

See Also

| | | | (MATLAB Compiler) | (MATLAB Compiler)

Related Topics