Main Content

matlabtest.coder.TestCase class

Package: matlabtest.coder
Superclasses: matlab.unittest.TestCase

Test case for generated C/C++ equivalence tests

Since R2023a


Use the matlabtest.coder.TestCase class to generate C/C++ code by using MATLAB® Coder™ and test the generated code for equivalence with MATLAB source code. You must have MATLAB Coder to run equivalence tests for generated C/C++ code for MEX targets and Embedded Coder® for LIB and DLL targets.

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

Class Attributes


For information on class attributes, see Class Attributes.


Create a class definition file that inherits from matlabtest.coder.TestCase. Author test methods in a methods block to define the test case.


expand all


collapse all

This example shows how to generate C code from a MATLAB function and test for equivalence by using MATLAB Coder.

The function myAdd takes two numbers as inputs, adds them together, and outputs the result.

function y = myAdd(a,b) %#codegen
y = a+b;

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

  1. Builds C code from the myAdd function with inputs set to (0,0)

  2. Executes the C code with inputs set to (1,2)

  3. Verifies the execution of the C code against the execution of the MATLAB function myAdd with the same inputs and an absolute tolerance of 0.01

classdef tEquivalence< matlabtest.coder.TestCase
    methods (Test)
        function tMyAdd(testCase)
            buildResults = build(testCase,"myAdd",Inputs={0,0});         
            executionResults = execute(testCase,buildResults,Inputs={1,2});

Run the tMyAdd test.

runtests("tEquivalence", ...
Running tMyAdd
Done tMyAdd

ans = 
  TestResult with properties:

          Name: 'tEquivalence/tMyAdd'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 2.6670
       Details: [1×1 struct]

   1 Passed, 0 Failed, 0 Incomplete.
   2.667 seconds testing time.

Version History

Introduced in R2023a