## 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 = 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 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); 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 ```
{"ft_lang_label":"__label__en","ft_lang_prob":0.69419795,"math_prob":0.5046512,"size":4265,"snap":"2022-27-2022-33","text_gpt3_token_len":924,"char_repetition_ratio":0.18258625,"word_repetition_ratio":0.014362657,"special_character_ratio":0.19460727,"punctuation_ratio":0.14689265,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97271377,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-05T19:13:57Z\",\"WARC-Record-ID\":\"<urn:uuid:4867eb0f-5fec-40e9-a0ed-b135e2451970>\",\"Content-Length\":\"78756\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7b19f928-5d56-4d57-8c9d-63b6716ba32e>\",\"WARC-Concurrent-To\":\"<urn:uuid:c888b2e8-65b6-4104-bb05-dc1184d2cfa6>\",\"WARC-IP-Address\":\"23.35.66.81\",\"WARC-Target-URI\":\"https://in.mathworks.com/help/matlab/matlab_prog/compile-matlab-unit-tests.html\",\"WARC-Payload-Digest\":\"sha1:P6EUYGF5RNHLCG7WKDVQ5WKIBMPUNUYS\",\"WARC-Block-Digest\":\"sha1:WVAGPQYOSLFTQFHM6TGDNM5FQQZGUFCW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104597905.85_warc_CC-MAIN-20220705174927-20220705204927-00205.warc.gz\"}"}