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:

  • To compile your MATLAB code, use the Application Compiler (MATLAB Compiler) app or run the mcc (MATLAB Compiler) command.

  • To run standalone applications, install the MATLAB Runtime. (If you use the Application Compiler app, you can decide whether to include the MATLAB Runtime installer in the generated application.) For more information, see Install and Configure the MATLAB Runtime (MATLAB Compiler).

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 = runner.run(suite);
disp(results)
end

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.

C:\work>runMyTests
  1x1200 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   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 Applications from the Command Line (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);
disp(results)
end

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'};
    end
    
    methods (Test)
        function testRepeatable(testCase,dim1,dim2,dim3)
            state = rng;
            firstRun = rand(dim1,dim2,dim3);
            rng(state)
            secondRun = rand(dim1,dim2,dim3);
            testCase.verifyEqual(firstRun,secondRun);
        end
        function testClass(testCase,dim1,dim2,type)
            testCase.verifyClass(rand(dim1,dim2,type),type)
        end
    end
end
 
function sizes = createDimensionSizes
% Create logarithmically spaced sizes up to 100
sizes = num2cell(round(logspace(0,2,10)));
end

See Also

| | | | (MATLAB Compiler)

Related Topics