主要内容

matlab.unittest.parameters.NullParameter Class

Namespace: matlab.unittest.parameters
Superclasses: matlab.unittest.parameters.Parameter

Parameter that represents no test data

Since R2026a

Description

The matlab.unittest.parameters.NullParameter class represents a parameter that corresponds to a parameterization property containing no data values, such as when the property is assigned an empty cell array or a scalar structure with no fields. When creating a test suite, the testing framework automatically excludes all the tests associated with a NullParameter object.

The testing framework instantiates this class. You are not required to create an object of the matlab.unittest.parameters.NullParameter class directly.

Examples

collapse all

Create and run a test suite after assigning an empty cell array to a parameterization property.

First, import the classes used in this example.

import matlab.unittest.parameters.Parameter
import matlab.unittest.TestSuite

In a file named ZerosTest.m in your current folder, create the ZerosTest test 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 and display the test names. The test suite contains 11 Test elements, including 8 parameterized tests. Of the parameterized tests, 6 tests are associated with the type parameterization property.

suite1 = testsuite("ZerosTest");
disp({suite1.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'               }

For illustrative purposes, redefine existing parameters by assigning an empty cell array to the type parameterization property from outside the test class. The fromData static method returns a matlab.unittest.parameters.NullParameter object.

newType = {};
param = Parameter.fromData("type",newType)
param = 

  NullParameter with properties:

    Property: 'type'
        Name: ''
       Value: {1×0 cell}

Create a test suite from the test class by injecting the new parameter, and then display the test names. The testing framework does not include any tests associated with a NullParameter object in the test suite. Compared to the original test suite (suite1), the new test suite excludes any tests that use the type parameterization property.

suite2 = TestSuite.fromClass(?ZerosTest,ExternalParameters=param);
disp({suite2.Name}')
    {'ZerosTest/testSize(size=s2d)'}
    {'ZerosTest/testSize(size=s3d)'}
    {'ZerosTest/testDefaultClass'  }
    {'ZerosTest/testDefaultSize'   }
    {'ZerosTest/testDefaultValue'  }

Run the tests in the test suite. In this example, all the tests pass.

results = suite2.run;
Running ZerosTest
.....
Done ZerosTest
__________

Version History

Introduced in R2026a