Main Content

matlab.unittest.Test Class

Namespace: matlab.unittest
Superclasses: matlab.unittest.TestSuite

Specification of a single test

Description

The matlab.unittest.Test class holds all the information required by the test runner to run a single test. A Test object, often called a Test element, is the fundamental element contained in test suites.

When you create a test suite, for example, with the testsuite function, the elements of the resulting TestSuite array are Test elements. You cannot create an object of the matlab.unittest.Test class directly.

Properties

expand all

Name of the test, returned as a character vector. For a given test file, the name of a test uniquely identifies the smallest runnable portion of the test content. It includes the namespace name, filename (excluding the extension), procedure name, and information about parameterization.

Attributes:

GetAccess
public
SetAccess
immutable

Name of the test procedure that must run for the test, returned as a character vector. In a class-based test, the name of a test procedure is the name of a Test method that contains the test. In a function-based test, it is the name of a local function that contains the test. In a script-based test, it is the name generated from the test section title. Unlike the name of a test, the name of a test procedure does not include any namespace name, filename, or information about parameterization.

Attributes:

GetAccess
public
SetAccess
immutable

Name of the test class that defines the test, returned as a string scalar. If the test is not a class-based test, then the property contains an empty string array.

Attributes:

GetAccess
public
SetAccess
private

Full path to the folder that contains the test file, returned as a character vector. For tests defined in namespace, the base folder is the parent of the top-level namespace folder.

Attributes:

GetAccess
public
SetAccess
Restricts access

Parameterization data required to run the test, returned as an array of matlab.unittest.parameters.Parameter objects. This property contains information such as parameterization property names and parameter names and values that are relevant to the test.

Attributes:

GetAccess
public
SetAccess
private

Shared test fixtures required for the test, returned as an array of matlab.unittest.fixtures.Fixture objects. This property contains all the fixtures specified by the SharedTestFixtures class-level attribute of the TestCase class.

Attributes:

GetAccess
public
SetAccess
private

Tags for the test, returned as a cell array of character vectors. Test tags are specified with the TestTags class-level attribute or the TestTags method-level attribute of the TestCase class.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Create an array of Test elements from a test class.

In a file named ZerosTest.m in your current folder, create the ZerosTest class, which tests the zeros function.

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros,"double")
        end

        function testDefaultSize(testCase)
            testCase.verifySize(zeros,[1 1])
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0)
        end
    end
end

Create a test suite from the ZerosTest class. Each element of the returned TestSuite array is a matlab.unittest.Test object.

suite = testsuite("ZerosTest");
whos suite
  Name       Size            Bytes  Class                   Attributes

  suite      1x11            27442  matlab.unittest.Test              

Display the names of the Test elements. Each name includes the class name and the corresponding Test method name. For the parameterized tests, the names also include information about parameterization.

disp({suite.Name}')
    {'ZerosTest/testClass(size=s2d,type=single)'}
    {'ZerosTest/testClass(size=s2d,type=double)'}
    {'ZerosTest/testClass(size=s2d,type=uint16)'}
    {'ZerosTest/testClass(size=s3d,type=single)'}
    {'ZerosTest/testClass(size=s3d,type=double)'}
    {'ZerosTest/testClass(size=s3d,type=uint16)'}
    {'ZerosTest/testSize(size=s2d)'             }
    {'ZerosTest/testSize(size=s3d)'             }
    {'ZerosTest/testDefaultClass'               }
    {'ZerosTest/testDefaultSize'                }
    {'ZerosTest/testDefaultValue'               }

Version History

Introduced in R2013a