주요 콘텐츠

build

Class: matlabtest.compiler.TestCase
Namespace: matlabtest.compiler

Generate deployed code artifacts in equivalence tests

Since R2023a

Description

buildResults = build(testCase,functionToBuild,artifactType) builds the deployed code artifact by using MATLAB® Compiler SDK™ with the type artifactType from the MATLAB function functionToBuild in the equivalence test case testCase.

example

buildResults = build(testCase,buildOptions) builds the deployed code artifact with the options and type specified by buildOptions.

example

buildResults = build(___,PreservingOnFailure=true) preserves the build folder and its contents after a test failure.

example

Input Arguments

expand all

Test case, specified as a matlabtest.compiler.TestCase object.

MATLAB function to build, specified as a string vector, character vector, cell vector of character vectors, function handle, or cell vector of function handles.

You can specify one or more functions using these forms:

  • Filename — The method uses the function in the specified file. You can specify the filename as a path relative to the current folder or an absolute path. The filename must have a .m extension. Specify multiple filenames as a string vector or cell vector of character vectors.

  • Function name — The method searches for the function along the MATLAB search path. Specify multiple function names as a string vector or cell vector of character vectors. (since R2026a)

  • Function handle — The method searches for the function along the MATLAB search path. Specify multiple function handles using a cell vector. (since R2026a)

Example: "foo.m" (name of a file in the current folder)

Example: ["foo.m" "C:\work\bar.m"] (two filenames)

Example: ["foo" "bar"] (two function names)

Example: {@foo,@bar} (two function handles)

Artifact type to build, specified as "dotNETAssembly", "javaPackage", "pythonPackage", "productionServerArchive", or "cppSharedLibrary".

Build options, specified as one of these objects:

Output Arguments

expand all

Build results, returned as a compiler.build.Results (MATLAB Compiler SDK) object.

Examples

expand all

This example shows how to generate a Python® package from MATLAB source code and test for equivalence by using MATLAB Compiler SDK.

The function makesquare generates an n-by-n matrix:

function y = makesquare(x)
y = magic(x);
end

This class definition file defines an equivalence test case that inherits from matlabtest.compiler.TestCase. The test case in the methods block defines a test case that:

  1. Builds the Python package from the makesquare function

  2. Executes the Python package with input set to 5

  3. Verifies the execution of the Python package against the execution of the MATLAB function makesquare with the same input

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)
            buildResults = build(testCase,@makesquare, ...
                "pythonPackage");
            executionResults = execute(testCase,buildResults,{5});
            verifyExecutionMatchesMATLAB(testCase,executionResults);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

This example shows how to generate a Python package with additional build options from MATLAB source code and test for equivalence by using MATLAB Compiler SDK.

The function makesquare generates an n-by-n matrix:

function y = makesquare(x)
y = magic(x);
end

This class definition file defines an equivalence test case that inherits from matlabtest.compiler.TestCase. The test case in the methods block defines a test case that:

  1. Defines a compiler.build.PythonPackageOptions (MATLAB Compiler SDK) object that specifies the function to build, the package name, and provides additional files to include in the package

  2. Builds the Python package from the build options object

  3. Executes the Python package with input set to 5

  4. Verifies the execution of the Python package against the execution of the MATLAB function makesquare with the same input

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)

            buildOpts = compiler.build.PythonPackageOptions( ...
                "makesquare.m");
            buildOpts.PackageName = "PackageUnderTest";
            buildOpts.AdditionalFiles = "makesquareData.mat";

            buildResults = build(testCase,buildOpts);
            executionResults = execute(testCase,buildResults,{5});
            verifyExecutionMatchesMATLAB(testCase,executionResults);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

This example shows how to generate a Python package from MATLAB source code and test for equivalence by using MATLAB Compiler SDK.

The function makesquare generates an n-by-n matrix and the function myAdd takes two inputs and adds them together:

function y = makesquare(x)
y = magic(x);
end
function y = myAdd(a,b)
y = a + b;
end

This class definition file:

  • Builds a packaged that contains makesquare and myAdd

  • Saves the build results object if the test fails

  • Verifies the result with an absolute tolerance

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)
            functionsToBuild = {@makesquare,@myAdd};
            buildResults = build(testCase,functionsToBuild, ...
                "pythonPackage",PreservingOnFailure=true);            
            preserveOnFailure=true;
            executionResults = execute(testCase,buildResults,{5}, ...
                "makesquare",PreservingOnFailure=preserveOnFailure);
            verifyExecutionMatchesMATLAB(testCase,executionResults, ...
                AbsTol=0.0001);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

Limitations

  • You cannot generate deployed code artifacts or test them for equivalence in MATLAB Online™.

Version History

Introduced in R2023a

expand all